乐逍遥xwl

导航

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 }

 

posted on 2019-07-24 18:43  乐逍遥xwl  阅读(298)  评论(0编辑  收藏  举报