D - Wave
Avin is studying series. A series is called “wave” if the following conditions are satisfied:
- It contains at least two elements;
- All elements at odd positions are the same;
- All elements at even positions are the same;
- 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
题意:这一题的题意就是让你在一个给定的序列里边找出其中的子序列组成一个新的序列,其中新序列的偶数位的数字和奇数位的数字都相同但是偶数位跟奇数位的数字不相同 比如例题12132 找出来121 和2 然后组成一个新的序列就是1212 长度为4;
题解,这一题看题解是暴力解题,然后网上也有用dp解出来的 目前没有看懂 等以后有机会继续补题
思路就是定义一个pair类型的vector,读入的时候对这个数的位置进行存储 pair的第一个值是位置,第二个值得存贮的数字 然后暴力一个数一个数的对照存进一个新的vector里边然后对这两个数进行排序,然后进行判定如果这个数的下一个数跟这个数不同 那么数量就加一。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> p;
vector<p> G[105];
int cmp(p a,p b)
{
return a.first<b.first;
}
int main()
{
std::ios::sync_with_stdio(false);
int n,c;
scanf("%d%d",&n,&c);
int num=0;
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
G[x].push_back(p(i,x));//记录这个数的位置
}
for(int i=1;i<=c;i++)//双重for循环进行一个数对一个数的比较暴力寻找
{
for(int j=i+1;j<=c;j++)
{
if(G[i].size()&&G[j].size())
{
vector<p> v;//新开一个vector为了存贮找到的一对一的数
for(int k=0;k<G[i].size();k++)
{
v.push_back(G[i][k]);
}
for(int k=0;k<G[j].size();k++)
{
v.push_back(G[j][k]);
}
sort(v.begin(),v.end(),cmp);//进行位置排序
int xx,sum=0;
for(int k=0;k<v.size();k++)
{
if(k==0)
{
sum++;
xx=v[k].second;
}
else if(xx!=v[k].second)//这个数跟下一个数不同那么数量就加一
{
sum++;
xx=v[k].second;
}
}
num=max(num,sum);
}
}
}
printf("%d\n",num);
}