传送门:http://codeforces.com/contest/1092/problem/D2
D2. Great Vova Wall (Version 2)
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.
The current state of the wall can be respresented by a sequence aa of nn integers, with aiai being the height of the ii-th part of the wall.
Vova can only use 2×12×1 bricks to put in the wall (he has infinite supply of them, however).
Vova can put bricks only horizontally on the neighbouring parts of the wall of equal height. It means that if for some ii the current height of part ii is the same as for part i+1i+1, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part 11 of the wall or to the right of part nn of it).
Note that Vova can't put bricks vertically.
Vova is a perfectionist, so he considers the wall completed when:
- all parts of the wall has the same height;
- the wall has no empty spaces inside it.
Can Vova complete the wall using any amount of bricks (possibly zero)?
The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of parts in the wall.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial heights of the parts of the wall.
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero).
Print "NO" otherwise.
5 2 1 1 2 5
YES
3 4 5 3
NO
2 10 10
YES
In the first example Vova can put a brick on parts 2 and 3 to make the wall [2,2,2,2,5][2,2,2,2,5] and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it [5,5,5,5,5][5,5,5,5,5].
In the second example Vova can put no bricks in the wall.
In the third example the wall is already complete.
题意概括:
给出 N 个墙的高度,只能用 2*1 的方块去填,判断是否能把所有的墙变成同一高度。
解题思路:
按顺序遍历,只有当两个相邻的墙高度相同时才能相互抵消。
细节就是如果出现有一个超长的墙出现阻隔,需要进行判断这个超长的墙是否在端点,如果在端点则没有影响,如果在中间则有影响。
AC code:
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #define INF 0x3f3f3f3f 7 #define LL long long 8 #define FOR(x, maxx) for(i = 0; i < maxx; i++) 9 using namespace std; 10 11 const int MAXN = 2e5+10; 12 13 LL stak[MAXN]; 14 int top; 15 16 int main() 17 { 18 int N, i; 19 bool flag = true; 20 LL x; 21 top = 0; 22 scanf("%d", &N); 23 scanf("%I64d", &x); 24 stak[++top] = x; 25 LL maa = x; 26 FOR(i, N-1){ 27 scanf("%I64d", &x); 28 if(x > stak[top] && top > 0) flag = false; 29 else if(x == stak[top] && flag) top--; 30 else stak[++top] = x; 31 maa = max(maa, x); 32 } 33 34 if(top > 1 || !flag) puts("NO"); 35 else{ 36 if(top == 1 && stak[top] != maa) puts("NO"); 37 else puts("YES"); 38 } 39 40 return 0; 41 }