题意:给一序列的整数求元素不同的最长子序列
#include<iostream>
#include<string.h>
using namespace std;
int a[1000005];
int temp[105];
int n,m,k;
bool judge(int x){
memset(temp,0,sizeof(temp));
temp[0] = 1;
int count = k;
for(int i = 1;i <= x;i++){
if(temp[a[i]] == 0)
count--;
temp[a[i]]++;
}
if(count == 0)
return true;
for(int i = 2;i <= n - x + 1;i++){
temp[a[i - 1]]--;
if(temp[a[i - 1]] == 0)
count++;
if(temp[a[i + x - 1]] == 0)
count--;
temp[a[i + x - 1]]++;
if(count == 0)
return true;
}
return false;
}
int main(){
int t,kase = 0;
cin >> t;
while(t--){
memset(a,0,sizeof(a));
a[1] = 1;
a[2] = 2;
a[3] = 3;
cin >> n >> m >> k;
cout << "Case " << ++kase << ": ";
if(k <= 3){
cout << k << endl;
continue;
}
if(k > m || n < k){
cout << "sequence nai" << endl;
continue;
}
memset(temp,0,sizeof(temp));
int count = k;
temp[0] = 1;
for(int i = 4;i <= n;i++){
a[i] = (a[i - 1] + a[i - 2] + a[i - 3])
%m + 1;
}
for(int i = 1;i <= n;i++){
if(a[i] > k)
a[i] = 0;
if(temp[a[i]] == 0){
count--;
}
temp[a[i]]++;
}
if(count != 0){
cout << "sequence nai" << endl;
continue;
}
int min = k,max = n,mid;
while(min < max){
mid = (max + min)/2;
if(judge(mid))
max = mid;
else
min = mid + 1;
}
cout << max << endl;
}
return 0;
}