Unique Snowflakes

题意:给一序列的整数求元素不同的最长子序列

#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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值