Codeforces Round #527 (Div. 3) D2.Great Vova Wall (Version 2)

D2. Great Vova Wall (Version 2)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Input

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

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤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.

Examples
Input
Copy
5
2 1 1 2 5
Output
Copy
YES
Input
Copy
3
4 5 3
Output
Copy
NO
Input
Copy
2
10 10
Output
Copy
YES
Note

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.

题意

  给出一组数,可以让连续的两个同时加一,能用这种操作使得最后数组的数相同。

分析

  用栈一个一个判断就行了。

  1、当前元素小于栈顶元素肯定不行。

  2、最后栈为空肯定可以。

  3、最后栈不空并且有一个元素,栈顶元素小于数组最大值,并且不是最后一个元素肯定不行,否则可以

  4、最后栈里有大于一个的元素。

  

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=2e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
stack<int>s;
int n;
int a[MAX],maxn;
int main()
{
    while(!s.empty()) s.pop();

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)
    {
        maxn=max(maxn,a[i]);
        if(s.empty())
        {
            s.push(a[i]);
            continue;
        }
        //printf("%d %d
",s.top(),a[i]);
        if(s.top()==a[i])
            s.pop();
        else if(s.top()<a[i])
        {
            printf("NO
");
            return 0;
        }
        else
            s.push(a[i]);
    }
    if(s.empty())
        printf("YES
");
    else if(s.size()==1)
    {
        if(s.top()<maxn && maxn!=a[n])
            printf("NO
");
        else
            printf("YES
");
    }
    else
        printf("NO
");
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Kissheart/p/10156428.html