Codeforces Round #616 (Div. 2) B. Array Sharpening

t題目連接:http://codeforces.com/contest/1291/problem/Bios

思路:spa

用極端的狀況去考慮問題,會變得很簡單。code

不管是單調遞增,單調遞減,或者中間高兩邊低的狀況均可以變爲三種模型。blog

(1)0,1,2,3,4........n-3,n-2,n-1ci

(2)n-1,n-2,n-3.....3,2,1,0get

(3)0,1,2,3,4,.....n.......4,3,2,1,0io

那麼,咱們只須要查看當前位置是否大於等於極端模型(3)在這個位置的數值,若是當前位置不知足了,class

那麼咱們就讓當前位置和以後的數值去和極端模型(3)n後面遞減的數值去比較,若是還有不知足的狀況說明就是「NO」了。test

注意一種狀況 0 1 1 0須要特殊處理一下。stream

 1 #include <iostream>
 2 using namespace std;
 3 
 4 const int N = (int)3e5+100;
 5 int a[N];
 6 
 7 int main(){
 8 
 9     int T,n;
10     while(cin >> T){
11         while(T--){
12             cin >> n;
13             for(int i = 1; i <= n; ++i) cin >> a[i];
14             int i;
15             bool ok = 1;
16             //遞增區間判斷
17             for(i = 1; i <= n; ++i){
18                 if(a[i] >= i-1) continue;
19                 break;
20             }
21             // 0 1 1 0 這種狀況斷定
22             if(i <= n){
23                 if(a[i] == a[i-1] && !(a[i] >= n-i+1)) ok = 0;
24             }
25             //遞減區間斷定
26             for(; i <= n; ++i){
27                 if(a[i] >= n-i) continue;
28                 ok = 0; break;
29             }
30             //if(ok) cout << "----------Yes" << endl;
31             //else cout << "----------No" << endl;
32             if(ok) cout << "----------Yes" << endl;
33             else cout << "----------No" << endl;
34         }
35     }
36 
37 }
相關文章
相關標籤/搜索