A. A Prank
生活随笔
收集整理的這篇文章主要介紹了
A. A Prank
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題鏈接: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。
?
純模擬
#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; }?
總結
以上是生活随笔為你收集整理的A. A Prank的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转载文章,感觉真的很心酸
- 下一篇: 检查是否有负循环(Bellman For