codeforces 1197 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 nn disks on the same pillar simultaneously?

题意:

相邻的元素大的在下,小的在上。判断是否可能最后叠在一个柱子上

题解:

模拟,如果左边和右边都小于中间的,那么取稍大的元素,这样可以放下更多的碟子

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
   int n, m;
   while(cin >> n){
        int a[500005], maxx = 0, k = 0, L = 0, R = 0, f = 1;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            if(a[i] > maxx){
                maxx = a[i];
                k = i;            // 记录最大值的位置
            }
        }
        L = k - 1;  // 记录左边取到的位置
        R = k + 1;  // 记录右边取到的位置
        a[0] = 0;
        a[n+1] = 0;
        while(1){
            //cout << L <<  "  " << R  << " " << k<< endl;
            if(L == 0 && R == n+1){  // 两边同时出界退出循环
                break;
            }
            if(a[L] >= a[k] || a[R] >= a[k]){  // 两边有任何一边大于中间值,不符合条件
                f = 0;
                break;
            }
            else{
                if(a[L] > a[R] && L > 0){   // 左右两边都小于中间值的情况下,选取大的
                    k = L;
                    L--;
                }
                else{
                    k = R;
                    R++;
                }	
            }
        }
        if(f == 1 && L == 0 && R == n+1){  // 没有不符合条件的并且取完所有元素
            cout << "YES" << endl;
        }
        else{
            cout << "NO" << endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值