Codeforces Round #527 (Div. 3) D1&D2(思维)

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 (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.

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.

D2题意:给你n(n<=2e5)个数字,你每次只能把相邻的两个相同的数都加1。问你最后是否能使所有数相同。

D1题意:在D2的基础上,还可以每次使任意一个数加2,问你最后是否能使所有数相同。

你可以进行无限次操作。如果可以,就输出YES,否则输出NO。

思路:感觉不好想。。。对于D2,显然一直是把最小的且偶数个相同的最小的数往上加,一直加到全变成最大值的数。

但是要直接模拟这个过程非常麻烦,而且有很多细节。

一个很好的思路:用

可以将本问题等价为一个相邻的相同的数两两相消的游戏。。。对于每个输入x,若

1、当前栈为空,则x入栈

2、当前栈非空且栈顶元素==x 则栈顶出栈(相当于相消)

3、当前栈非空且栈顶元素>x 则x入栈(因为x可能被后面的消掉,那么当前栈顶是有机会出栈的)

4、当前栈非空且栈顶元素<x 则答案为NO。因为只能往上加而不能往下减,所以当前栈顶是永远都消不掉的。

5、最后栈中元素数>1或者最后栈中元素数==1且栈中元素小于已出栈元素(实际上是所有其他元素)的最大值,则无法消除。否则一定可以。

如果你理解了D2的解法,对于D1,只需要对每个x&1,再稍微修改一下就行了。

D1:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int n;
int a[maxn],cnt;
int ans,x,ma,flag;
int main()
{
    scanf("%d",&n);
    cnt=0;ma=0;
    flag=1;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        x&=1;
        if(!cnt) a[++cnt]=x;
        else if(a[cnt]==x) cnt--;
        else
        {
            a[++cnt]=x;
        }
    }
    if(cnt>1) puts("NO");
    else puts("YES");
    return 0;
}

D2:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int n;
int a[maxn],cnt;
int ans,x,ma,flag;
int main()
{
    scanf("%d",&n);
    cnt=0;ma=0;
    flag=1;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        if(!cnt) a[++cnt]=x;
        else if(a[cnt]==x) {cnt--;ma=max(ma,x);}
        else
        {
            if(x<a[cnt]) a[++cnt]=x;
            else flag=0;
        }
    }
    if(!flag||cnt>1) puts("NO");
    else if(cnt==1&&a[cnt]<ma) puts("NO");
    else puts("YES");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值