题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数。从中选出若干个数,使构成一个连续的序列。问能构成的最长序列的长度为多少?
题目分析:枚举连续序列的起点,二分枚举二分序列的终点。
代码如下;
# include<iostream> # include<cstdio> # include<queue> # include<vector> # include<list> # include<map> # include<set> # include<cstdlib> # include<string> # include<cstring> # include<algorithm> using namespace std; # define LL long long const int N=1000; const int INF=1<<30; const LL oo=1e15; const double eps=1e-20; int n,m; int sum[N*100+5]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(sum,0,sizeof(sum)); int a,k=0; for(int i=0;i<n;++i){ scanf("%d",&a); if(a) sum[a]=1; else ++k; } for(int i=1;i<=m;++i) sum[i]+=sum[i-1]; int ans=0; for(int i=1;i<=m;++i){ int l=i,r=m; while(l<=r){ int x=(l+r)>>1; if(sum[x]-sum[i-1]+k>=x-i+1){ ans=max(ans,x-i+1); l=x+1; }else{ r=x-1; } } } printf("%d\n",ans); } return 0; }
时间: 2024-12-15 18:54:53