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.
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); }
代码在超时的边缘,汗!!