c++没过用G++就过了 。。。。。
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
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100010];
int d[110];
int b[110];
int main()
{
int n,c;
scanf("%d%d",&n,&c);
memset(b,0,sizeof(b));
int num=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(!b[a[i]])
{
b[a[i]]=1;
d[num++]=a[i];
}
}
int ans=0;
for(int i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
{
int t=0,v=0;
for(int k=0;k<n;k++)
{
if(a[k]==d[i]&&t%2==0)
{
t++;
}
else if(a[k]==d[j]&&t%2==1)
{
t++;
}
if(a[k]==d[i]&&v%2==1)
{
v++;
}
else if(d[j]==a[k]&&v%2==0)
{
v++;
}
}
ans=max(ans,max(t,v));
t=0,v=0;
}
}
printf("%d\n",ans);
return 0;
}
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int>p;
vector<p>G[110];
bool cmp(p a1,p a2)
{
return a1.first<a2.first;
}
int main()
{
int n,c,x;
scanf("%d%d",&n,&c);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
G[x].push_back(p(i,x));
}
int num=0;
for(int i=1;i<=c;i++)
{
for(int j=i+1;j<=c;j++)
{
if(G[i].size()&&G[j].size())
{
vector<p>pi;
for(int k=0;k<G[i].size();k++)
{
pi.push_back(G[i][k]);
}
for(int k=0;k<G[j].size();k++)
{
pi.push_back(G[j][k]);
}
sort(pi.begin() ,pi.end(),cmp);
int sum=0,flag;
for(int k=0;k<pi.size();k++)
{
if(k==0) sum++,flag=pi[k].second;
else if(pi[k].second!=flag) sum++,flag=pi[k].second;
}
num=max(num,sum);
}
}
}
printf("%d\n",num);
return 0;
}