【CF977F】sequence 序列

【简要题面】给定一个含n个正整数的序列。选取任意个不重复的子序列,将其按顺序拼接起来,保证有a[i+1]=a[i]+1。求最长子序列,含有spj,n<=2e5

【分析】
一个结论,同样的一个数字:比如说是3,后面的3一定不比前面的3差。所以直接用map记录每个数字最后出现的位置,然后dp值从上一个位置转移过来就可以了。

【code】

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2e5+1000;
int kase,n;
int a[maxn];
int f[maxn],opt[maxn];
map<int,int>mp;
inline void read(int &x){
	x=0;char tmp=getchar();int fl=1;
	while(tmp<'0'||tmp>'9'){if(tmp=='-')fl=-fl;tmp=getchar();}
	while(tmp>='0'&&tmp<='9') x=(x<<1)+(x<<3)+tmp-'0',tmp=getchar();
	x=x*fl;
}
void prt(int x){
	if(!x) return ;
	prt(opt[x]);
	printf("%d ",x);
}
int main(){
	freopen("sequence.in","r",stdin);
	freopen("sequence.out","w",stdout);
	cin>>kase>>n;
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=1;i<=n;i++){
		f[i]=f[mp[a[i]-1]]+1,opt[i]=mp[a[i]-1];
		mp[a[i]]=i;
	}
	int pos=0;
	for(int i=1;i<=n;i++)
		if(f[i]>=f[pos]) pos=i;
	cout<<f[pos]<<endl;
	prt(pos);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值