Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2)

 

B - Pillars

There are n pillars aligned in a row and numbered from 1 to n.

Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai.

You can move these disks from one pillar to another. You can take a disk from pillar i and place it on top of pillar j if all these conditions are met:

  1. there is no other pillar between pillars i and j. Formally, it means that |i−j|=1;
  2. pillar i contains exactly one disk;
  3. either pillar j contains no disks, or the topmost disk on pillar j has radius strictly greater than the radius of the disk you move.

When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.

You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all n disks on the same pillar simultaneously?

Input

The first line contains one integer n (3≤n≤2⋅105) — the number of pillars.

The second line contains n integers a1, a2, ..., ai (1≤ai≤n), where ai is the radius of the disk initially placed on the i-th pillar. All numbers ai are distinct.

Output

Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

Examples

input

4

1 3 4 2

output

YES

input

3

3 1 2

output

NO

Note

In the first case it is possible to place all disks on pillar 3 using the following sequence of actions:

  1. take the disk with radius 3 from pillar 2 and place it on top of pillar 3;
  2. take the disk with radius 1 from pillar 1 and place it on top of pillar 2;
  3. take the disk with radius 2 from pillar 4 and place it on top of pillar 3;
  4. take the disk with radius 1 from pillar 2 and place it on top of pillar 3.

 

题意:题目意思大概是有n个柱子,每个柱子上有ai个 磁盘,你可以进行无限次操作,把 第i个柱子的磁盘ai 移到 有aj磁盘的第j个柱子上,

当且仅当  1.  i,j柱子相邻 。2. i柱子上只有一个磁盘ai。 3. j柱子上没有磁盘 或 磁盘半径ai < aj 。

然后问你 最后能不能把所有磁盘移动到一个柱子上。

思路:既然要移到同一个柱子上,那磁盘半径从上到下肯定是从小到大,最下面的磁盘半径就要是最大的,

那我们可以先遍历一遍找到最大值下标,再从这里开始模拟放磁盘,左右的磁盘优先放大的,放不了就break。

最后判断是否叠了n个磁盘。

另外一种写法可以说是推论,也就是从这个最大值的位置开始,向左要全递减,向右也全递减,然后就是YES,否则就是NO。

因为向两边都递减,放磁盘一定是可以全放完的,可以仔细想一下。

 

写法一:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<map>
  7 #include<set>
  8 #include<vector>
  9 #include<queue>
 10 using namespace std;
 11 #define ll long long 
 12 const int mod=1e9+7;
 13 const int inf=1e9+7;
 14  
 15 const int maxn=2e5+5;
 16 int num[maxn];
 17  
 18 int main()
 19 {
 20     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 21     int n;
 22     while(cin>>n)
 23     {
 24         int maxx=-1,mark_i;//标记最大值位置 
 25 
 26         for(int i=0;i<n;i++)
 27         {
 28             cin>>num[i];    
 29             if(num[i]>maxx)
 30             {
 31                 maxx=num[i];
 32                 mark_i=i;
 33             }
 34         }
 35         
 36         int left=mark_i-1;
 37         int right=mark_i+1;
 38         int now=num[mark_i];
 39         
 40         int cnt=1;//记录叠了几个磁盘 
 41         
 42         while(left>=0&&right<=n-1)//边界 
 43         {
 44             if(num[left]>=num[right])//左边大 
 45             {
 46                 if(num[left]<=now)
 47                 {
 48                     now=num[left];//向左延伸 
 49                     cnt++;
 50                     left--;
 51                 }
 52                 else if(num[left]>now)//不符合退出 
 53                 {
 54                     break;
 55                 }
 56             }
 57             else if(num[left]<num[right])//右边大 
 58             {
 59                 if(num[right]<=now)
 60                 {
 61                     now=num[right];//向右延伸 
 62                     right++;
 63                     cnt++;
 64                 }
 65                 else//不符合退出 
 66                 {
 67                     break;
 68                 }
 69             }
 70         }
 71         
 72         //可能两边有哪边没有延伸完,继续延伸 
 73         while(left>=0)
 74         {
 75             if(num[left]<=now)
 76             {
 77                 now=num[left];
 78                 cnt++;
 79                 left--;
 80             }
 81             else if(num[left]>now)
 82             {
 83                 break;
 84             }
 85         }
 86         
 87         while(right<=n-1)
 88         {
 89             if(num[right]<=now)
 90             {
 91                 now=num[right];
 92                 right++;
 93                 cnt++;
 94             }
 95             else
 96             {
 97                 break;
 98             }
 99         }
100         
101         if(cnt==n)//叠了n个磁盘 
102             cout<<"YES"<<endl;
103         else
104             cout<<"NO"<<endl;
105         
106     }
107     
108     return 0;
109 }

 

写法二:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 using namespace std;
11 #define ll long long 
12 const int mod=1e9+7;
13 const int inf=1e9+7;
14  
15 const int maxn=2e5+5;
16 int num[maxn];
17  
18 int main()
19 {
20     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
21     int n;
22     
23     while(cin>>n)
24     {
25         int maxx=-1,mark_i;
26         
27         for(int i=0;i<n;i++)
28         {
29             cin>>num[i];
30             
31             if(num[i]>maxx)
32             {
33                 maxx=num[i];
34                 mark_i=i;
35             }
36         }
37         
38         int flag=0;
39         
40         for(int i=mark_i;i>=1;i--)
41         {
42             if(num[i]<num[i-1])
43             {
44                 flag=1;
45                 break;
46             }
47         }
48         
49         if(flag==0)
50         {
51             for(int i=mark_i;i<n-1;i++)
52             {
53                 if(num[i]<num[i+1])
54                 {
55                     flag=1;
56                     break;
57                 }
58             }
59         }
60         
61         if(flag)
62             cout<<"NO"<<endl;
63         else
64             cout<<"YES"<<endl;
65     
66     }
67     
68     return 0;
69 }

 

转载于:https://www.cnblogs.com/xwl3109377858/p/11240118.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值