CodeForces#520 div2 1062A - A Prank

题目大意:
一个递增的数组,每个数字$a_{i}\in\left [ 1,10^3 \right ]$,问最多可以擦掉几个数字,仍可以让数组恢复成原来的样子。
分析:
比较容易想到,如果一个数字满足$a_{i-1}+1= a_{i},a_{i}+1=a_{i+1}$,那么这个$a_{i}$是可以删除的,为了让头尾也能符合这个普遍规律,我们只需要将$a_{0}=0,a_{n+1}=1001$,然后$i=0$开始,遍历判断是否满足上述条件,如果成立$cnt+1$,否则$cnt=0$,最后再取最大值。
code:
#define frp
#include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
#include <iomanip>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const int maxn = 2e6;
const int MAXN = 100000 + 5;
const int MOD = 1e9 + 7;
int a[maxn];
void solve() {
int n;
cin>>n;
for (int i = 1; i < n+1; ++i) {
cin>>a[i];
}
a[0]=0;a[n+1]=1001;
int cnt=0,ans=0;
for (int i = 1; i < n+1; ++i) {
if(a[i-1]+1==a[i]&&a[i]+1==a[i+1]){
cnt++;
}else{
cnt=0;
}
// cout<<i<<": "<<cnt<<endl;
ans=max(ans,cnt);
}
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef frp
freopen("D:\\coding\\c_coding\\in.txt", "r", stdin);
// freopen("D:\\coding\\c_coding\\out.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}

浙公网安备 33010602011771号