ZOJ 3790Consecutive Blocks 最多删k个数求最长连续相同序列长度

时间:2022-02-19 19:10:54

题解:http://blog.csdn.net/csuhoward/article/details/38366737

#include<bits/stdc++.h>  
using namespace std;
map<int,int> rr;
vector<int> v[100005];
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
rr.clear();
int a,c=-1;
for(int i=0;i<n;++i){
v[i].clear();
scanf("%d",&a);
if(!rr.count(a))
rr[a]=++c;
v[rr[a]].push_back(i);
}
int maxx=0;
for(int i=0;i<=c;++i){
int l=0,r=0,num=0,sum=0; //num是已经用了多少次特效,sum是已经得到多少个连续值
for(;r<v[i].size();){
if(r==0||num+(v[i][r]-v[i][r-1]-1)<=m){
if(r==0){
sum++;
r++;
}
else{
num+=v[i][r]-v[i][r-1]-1;
sum++;
r++;
}
}
else{
sum--;
num-=v[i][l+1]-v[i][l]-1;
l++;
}
maxx=max(maxx,sum);
}
}
printf("%d\n",maxx);
}
return 0;
}
#include<bits/stdc++.h>    using namespace std;    map<int,int> rr;  vector<int> v[100005];  int main(){      int n,m;      while(scanf("%d%d",&n,&m)!=EOF){          rr.clear();          int a,c=-1;          for(int i=0;i<n;++i){              v[i].clear();              scanf("%d",&a);              if(!rr.count(a))                  rr[a]=++c;              v[rr[a]].push_back(i);          }          int maxx=0;          for(int i=0;i<=c;++i){              int l=0,r=0,num=0,sum=1; //num是已经用了多少次特效,sum是已经得到多少个连续值               while(r<v[i].size()){                  if(num>m){                    num-=v[i][l+1]-v[i][l]-1;                    sum--;                    l++;                    continue;                }                 maxx=max(maxx,sum);                r++;num+=v[i][r]-v[i][r-1]-1; //这里有越界,但是过了                 sum++;            }          }          printf("%d\n",maxx);      }      return 0;  } 
#include<bits/stdc++.h>    using namespace std;    map<int,int> rr;  vector<int> v[100005];  int main(){      int n,m;      while(scanf("%d%d",&n,&m)!=EOF){          rr.clear();          int a,c=-1;          for(int i=0;i<n;++i){              v[i].clear();              scanf("%d",&a);              if(!rr.count(a))                  rr[a]=++c;              v[rr[a]].push_back(i);          }          int maxx=0;          for(int i=0;i<=c;++i){              int l=0,r=0;             while(r<v[i].size()){                  if((v[i][r]-v[i][l]+1)-(r-l+1)>m){                    l++;                    continue;                }                 maxx=max(maxx,r-l+1);                r++;            }          }          printf("%d\n",maxx);      }      return 0;  }