Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation

还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com)

传送门  Problem - D - Codeforces

题意

n个数字,n个字符, 数字与字符一一对应, 其中字符R红色代表数字可以上升, B蓝色代表可以下降, 问一顿操作下来能否使n个数含有1~n的所有数

 

题解

当时真的没想到, 蓝色一定在数组前面, 红色一定在数组后面,( 反正就算有方案使得蓝不在前面, 红不在后面,蓝红也是可以交换的)。 排序后, 前面的蓝色数字如果>=i+1 或者红色的数字<=i+1 即可, 不符合条件的数最后都没有去处

 

AC代码

// #include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;

typedef long long LL;
typedef pair<char,int> PII;
const int N = 2e5+10;
PII a[N];

int main(void)
{
    int t;
    cin >> t;
    while(t --)
    {
        int n; 
        string s;
        cin >> n;
        for(int i = 0; i <n; i ++)    cin >> a[i].second;
        for(int i = 0; i <n; i ++)    cin >> a[i].first;
        
        sort(a, a+n);
        bool flag = 1;
        for(int i = 0; i < n; i ++)
        {
            if(a[i].first == 'B'){
                if(a[i].second < i+1)
                {
                    flag = 0;
                    break;
                }
            }
            else{
                if(a[i].second > i+1)
                {
                    flag = 0;
                    break;
                }
            }
        }
        if(flag)puts("YES");
        else    puts("NO");
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/la-la-wanf/p/15510622.html