HDU重現賽之2019CCPC-江西省賽:1004 Wave


Problem Description
Avin is studying series. A series is called "wave" if the following conditions are satisfied:
1) It contains at least two elements;
2) All elements at odd positions are the same;
3) All elements at even positions are the same;
4) Elements at odd positions are NOT the same as the elements at even positions.
You are given a series with length n. Avin asks you to find the longest "wave" subseries. A subseries is a subsequence of a series.
Input
The first line contains two numbers n, c (1 ≤ n ≤ 100, 000, 1 ≤ c ≤ 100). The second line contains n integers whose range is [1, c], which represents the series. It is guaranteed that there is always a "wave" subseries.
Output
Print the length of the longest "wave" subseries.
Sample Input
5 3 1 2 1 3 2
Sample Output
4
題意
給一個n個整數的序列,整數范圍為1~c,求一個子序列滿足偶數位上數字相同,奇數位上數字相同,且奇偶位上的數字不同。
輸出滿足要求的最長子序列的長度。
題解
由題意可知所求子序列僅由兩個數字組成,序列范圍僅為1~c( 1 ≤ c ≤ 100),可以枚舉兩個數字,進行兩兩配對,從原序列中提取出來這兩個數字的子序列,將該子序列中相鄰的相同元素並為一個元素。
如(2 1 1 1 2 2)->(2 1 2) 合並后的序列長度為能兩元素能組成子序列的最長長度,枚舉完所有數字組合后得到的最長長度即為所求。
代碼
#include<bits/stdc++.h>
using namespace std;
int n,m;
typedef pair<int,int> p;
vector<p> G[105];
int cmp(p a,p b)
{
    return a.first<b.first;
}
int main()
{
     scanf("%d%d",&n,&m);
     for(int i=1;i<=n;i++)
     {
         int x;
         scanf("%d",&x);
         G[x].push_back(p(i,x));
     }
     int maxn=0;
     for(int i=1;i<=m;i++)
     {
         for(int j=i+1;j<=m;j++)
         {
             if(G[i].size()&&G[j].size())
             {
                vector<p> v;
                 for(int z=0;z<G[i].size();z++)
                 {
                     v.push_back(G[i][z]);
                 }
                 for(int z=0;z<G[j].size();z++)
                 {
                     v.push_back(G[j][z]);
                 }
                 sort(v.begin(),v.end(),cmp);
                 int sum=0,flag;
                 for(int z=0;z<v.size();z++)
                 {
                     if(z==0)sum++,flag=v[z].second;
                     else if(v[z].second!=flag)sum++,flag=v[z].second;
                 }
                 maxn=max(maxn,sum);
             }
         }
     }
     printf("%d\n",maxn);
}
View Code

 代碼在超時的邊緣,汗!!

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM