hdu6570Wave (暴力求解)

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
 
Source
 
中文题意:给你两个数n,c;接下来会再给你n个数在[1,c]之间;
问你从其中找出一个子序列,使得奇数的位置所有的数都相等,所有偶数的位置也相等,但奇数与偶数位置的数不能相等;
这个奇数位置,偶数位置是相对于你选出来的子序列,在子序列中的位置
 
思路:暴力求解:记下n个数中重复出现数的个数,然后每两个组合,查找这两个数可以组成的最长子序列;
 
AC624ms:

#include<iostream>
#include<cstdio>
using namespace std;
int num[100005];
struct node{
int number,quanlity;//第一个存储表示的是哪一个数,第二个表示这个数的个数
}d[105];
int cmp(struct node x,struct node y){
return x.quanlity>y.quanlity;
}
int main(){
int n,c;
scanf("%d%d",&n,&c);
for(int i=0;i<=100;i++)
d[i].quanlity=0,d[i].number=i;
for(int i=0;i<n;i++)
scanf("%d",&num[i]),d[num[i]].quanlity++;
/* for(int i=0;i<c;i++)
printf("%d %d\n",d[i].number,d[i].quanlity);*/
int maxn=0;
for(int i=0;i<c-1;i++){
for(int ii = i+1;ii<c;ii++){
int s = 1 , x = d[i].number , y = d[ii].number , mm , j;
for(j=0;j<n;j++){if(num[j]==x||num[j]==y) {mm=num[j];break;}}
for(;j<n;j++){
if(num[j]==x||num[j]==y){
if(num[j]!=mm) s++,mm=num[j];
}
}
if(s>maxn) maxn=s;
}
}
printf("%d\n",maxn);
return 0;
}

//10 4
//1 4 3 1 3 1 2 2 1 2

猜你喜欢

转载自www.cnblogs.com/sunjianzhao/p/11674523.html