Codeforces1062A. A Prank(暴力)
题目链接:传送门
题目:

A. A Prank time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a1 , a2, ..., an of integers, such that 1≤a1<a2<…<an≤103 , and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1,103] . JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1≤n≤100 ) — the number of elements in the array. The second line of the input contains n integers ai (1≤a1<a2<⋯<an≤103 ) — the array written by Giraffe. Output Print a single integer — the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0 . Examples Input Copy 6 1 3 4 5 6 9 Output Copy 2 Input Copy 3 998 999 1000 Output Copy 2 Input Copy 5 1 2 3 4 5 Output Copy 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1,3,_,_,6,9] . As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998,_,_] . Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
题目大意:
一个长度为n(1 ≤ n ≤ 100)的,在1-1000范围内的严格递增序列a[n]。
问最多在序列里删掉多少个元素,可以使得填回去的方法唯一(要求仍为严格递增序列)。
比如123,就可以删掉2变成1_3,要求填进去的数比1大比3小,那么只能填2。
思路:
直接跑一边数组,求最长的连续整数序列的长度就好了。但是要注意细节:
①:n=1时,不能删去,答案为0。。好像有很多人fst在了这个上,据说输出了负值?。
②:a1 = 1,a2 = 2时,a1可以删去,而a1 = 2,a2 = 3时不能删去a1。
③:an-1 = 999,an = 1000时,an可以删去,而an-1 = 998,an = 999时不能删去an。
对于①,初始化答案为0,不断求最大的答案就可以;
对于②③,不妨令a[0] = 0,a[n+1] = 1001,就可以直接求最长的连续序列长度了,答案为长度-2。
代码:

#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e2 + 5; int a[MAX_N]; int main() { int N; cin >> N; for (int i = 1; i <= N; i++) { scanf("%d", a+i); } a[++N] = 1001; int pre = 0; int len = 1; int ans = 0; for (int i = 1; i <= N; i++) { if (a[i] == pre+1) { len++; pre++; } else { ans = max(ans, len-2); len = 1; pre = a[i]; } } ans = max(ans, len-2); cout << ans << endl; return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步