## 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 a of n integers, with ai being the height of the i-th part of the wall.

Vova can only use 2×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 i the current height of part i is the same as for part i+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 1 of the wall or to the right of part n 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)?

Input
The first line contains a single integer n (1≤n≤2⋅105) — the number of parts in the wall.

The second line contains n integers a1,a2,…,an (1≤ai≤109) — the initial heights of the parts of the wall.

Output
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero).

Print "NO" otherwise.

#include <iostream>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
int n,arr[2000000],res,mmax;
stack<int> s;
int main() {
cin >> n;
for(int i = 1; i<=n; i++){
cin >> arr[i];
mmax = max(arr[i], mmax);
if(s.empty()){
s.push(arr[i]);
}else{
if(s.top() < arr[i]){
cout << "NO" << endl;
return 0;
}
if(s.top() == arr[i]){
s.pop();
}
else if(s.top() > arr[i]){
s.push(arr[i]);
}
}
}
if(s.size()> 1){
cout << "NO" << endl;
}else if(s.size() == 1){
if(s.top() < mmax){
cout << "NO" << endl;
}else{
cout << "YES" << endl;
}
}else{
cout << "YES" << endl;
}
return 0;
}

## CodeForces - 1077B 思维

There is a house with n flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of n integer numbers a1,a2,…,an, where ai=1 if in the i-th flat the light is on and ai=0 otherwise.

Vova thinks that people in the i-th flats are disturbed and cannot sleep if and only if 1<i<n and ai−1=ai+1=1 and ai=0.

Vova is concerned by the following question: what is the minimum number k such that if people from exactly k pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number k.

Input
The first line of the input contains one integer n (3≤n≤100) — the number of flats in the house.

The second line of the input contains n integers a1,a2,…,an (ai∈{0,1}), where ai is the state of light in the i-th flat.

Output
Print only one integer — the minimum number k such that if people from exactly k pairwise distinct flats will turn off the light then nobody will be disturbed.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n, arr[1005];
int res;
int main() {
cin >> n;
for(int i = 1; i<=n; i++){
cin >> arr[i];
}
for(int i = 2; i<n; i++){
if(arr[i+1] == 1 && arr[i-1] == 1 && arr[i] == 0){
arr[i+1] = 0;
res ++;
}
}
cout << res << endl;
return 0;
}

## CodeForces - 1092D1 Great Vova Wall (Version 1) 思维

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 a of n integers, with ai being the height of the i-th part of the wall.

Vova can only use 2×1 bricks to put in the wall (he has infinite supply of them, however).

Vova can put bricks horizontally on the neighboring parts of the wall of equal height. It means that if for some i the current height of part i is the same as for part i+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 1 of the wall or to the right of part n of it).

The next paragraph is specific to the version 1 of the problem.

Vova can also put bricks vertically. That means increasing height of any part of the wall by 2.

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)?

Input
The first line contains a single integer n (1≤n≤2⋅105) — the number of parts in the wall.

The second line contains n integers a1,a2,…,an (1≤ai≤109) — the initial heights of the parts of the wall.

Output
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero).

Print "NO" otherwise.

#include <iostream>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
int n,arr[2000000],res;
stack<int> s;
int main() {
cin >> n;
for(int i = 1; i<=n; i++){
cin >> arr[i];
if(s.empty()){
s.push(arr[i]);
}else{
if(abs(arr[i] - s.top()) % 2 == 1){
s.push(arr[i]);
}else{
s.pop();
}
}
}
if(s.size()> 1){
cout << "NO" << endl;
}else{
cout << "YES" << endl;
}
return 0;
}

## #801. 结账

#include <iostream>
#include <cstring>
using namespace std;
int dp[2000005], a[4000], b[4000], e, n,w[200005],v[200005],g;
int main(int argc, char** argv) {
cin >> n;
for(int i = 1; i<=n; i++){
cin >> a[i];
}
for(int i = 1; i<=n; i++){
cin >> b[i];
}
for(int i = 1; i<=n; i++){
for(int k = 1; k<=b[i]; k=k<<1){
w[++g] = k*a[i];
v[g] = k;
b[i] -= k;
}
if(b[i]){
w[++g] = b[i]*a[i];
v[g] = b[i];
}
}
cin >> e;
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for(int i = 1; i<=g; i++){
for(int j = e; j >= w[i]; j--){
dp[j] = min(dp[j], dp[j - w[i]] + v[i]);
}
}
cout << dp[e] << endl;
return 0;
}

## #800. 转圈圈

http://oj.ldyzz.com.cn/problem/800

#include <iostream>
#include <cstring>
#include <math.h>
#include <cstdio>
#include <stack>
using namespace std;
int vis[200005],n,arr[200005],ans,now,c[200005];
const int inf = 0x3f3f3f;
inline int dfs(int v,int dep){
if(vis[v] == now){
return dep - c[v]; //之前走过 vis值等于now 所以直接dep - c[v] c[v] 表示之前走过的层级的数量
}
if(vis[v]){
return inf; //有环 但不是终点 证明此点永远不会到达自身
}
vis[v] = now;
c[v] = dep;
dfs(arr[v], dep+1);
}
int main(int argc, char** argv) {
scanf("%d", &n);
ans = inf;
for(int i = 1; i<=n; i++){
scanf("%d", &arr[i]);
}
for(now = 1; now<=n; now++){
ans = min(ans, dfs(now, 0));
}
cout << ans << endl;
return 0;
}