2021-07-29 AcWing 3784. 交换相邻元素

输入样例1:

6
1 2 5 3 4 6
01110

输出样例1:

YES

输入样例2:

6
1 2 5 3 4 6
01010

输出样例2:

NO

 思路:如果当前数字比前面数的最大值小,且前一位不可与之调换,那么就无法得到升序序列;反之可以

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 200010;
int a[N],ret[N];
string b;
int main()
{
    int n;
    cin>>n;
    int maxx;
    scanf("%d", &a[0]);
    maxx=a[0];
    for (int i = 1; i < n; i ++ ){
        scanf("%d", &a[i]);
        if(a[i]<maxx) {
            ret[i]=1;
        }else{
            maxx=a[i];
        }
    }
    cin >> b;
    
    for (int i = 1; i < n; i ++ ){
        if(ret[i]&&b[i-1]=='0'){
            cout<<"NO";
            return 0;
        }
    }
    cout << "YES";
}

posted @ 2021-07-29 20:06  泥烟  阅读(33)  评论(0编辑  收藏  举报