版权声明:wjwjwjwjwj https://blog.csdn.net/qq_41818544/article/details/84187320
题链接:A. A Prank
题意:给定一个长度为n的递增序列数组,你可以进行删除任意连续序列的操作,要求删除之后还可以还原该数组,输出最大删除数。
例如:n = 6
数组 = {1,3,4,5,6,9}
可以删除数字4,5,此时可以还原
数据范围: n <= 100 1 <= a[i] <= 1000
自我理解:此题很坑,细节也很多,如果只用纯模拟的话,必须考虑很多种情况。有结果为0的情况,结果为连续数组个数 - 2,
结果为连续数组个数 - 1的情况
题解:如果三个三个为一组判断是否连续,就可避免很多种判断。
数组从1位置开始到位置。a[0] = 0, a[n+1] = 1000, 循环从0到n+1。
扫描二维码关注公众号,回复:
4119846 查看本文章
纯模拟
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int a[110];
int n;
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
int l = 0, r;
int t = 0;
int res = 0;
int ans = 0;
for(int i = 1; i <= n; i++){
if(a[i] - a[i-1] == 1){
ans++;
}
else{
res = ans + 1;
int temp;
r = i - 1;
l = r - res + 1;
if(res <= 1||(res == 2&&a[n-1] < 1000&&a[0] > 1)) temp = 0;
else if((l == 0&&r == n-1&&a[r] == n)||(l == 0&&r == n-1&&a[r] == 1000)||(l == 0&&r < n-1&&l != r&&a[r] == res)||(l > 0&&r == n-1&&a[r] == 1000)) temp = res - 1;
else temp = res - 2;
t = max(t, temp);
ans = 0;
}
}
printf("%d\n", t);
return 0;
}
思维,技巧(三个三个判断模拟)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int a[110];
int n;
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
a[n+1] = 1001;
int c = 0;
int ans = 0;
for(int i = 1; i <= n+1; i++){
if(a[i+1] - a[i-1] == 2){
c++;
//printf("******* %d\n", c);
}
else{
ans = max(ans, c);
c = 0;
}
}
printf("%d\n", ans);
return 0;
}