Codeforces Round #753 (Div. 3)

D. Blue-Red Permutation

思路:

贪心,先把blue颜色的num放到前面,因为它们只能变小或者不变而不能变大,我们要得到的是一个1 ~ n的排列,也就是说得到的数组的值中1 ~ n这些数必然出现并且一个只出现一次。
那么我们把可以变小的num先放到前面(同颜色的按值大小排序,显然值越小越排到前面)。因为变小可以确保能先遍历到小的num
然后就是定义一个num来表示当前要造的数,如果我们遍历数组时,它比num大而且它只能变大,那肯定就是不对的、如果它比num小并且它只能变小,也是不对的。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <map>
#include <unordered_set>
#include <unordered_map>

#define IOS ios::sync_with_stdio(false);cin.tie(0);

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 200010;
const double PI = acos(-1);

struct Node{
    int w;
    char c;
    bool operator<(const Node &t) const {
        if(c == t.c)
            return w < t.w;
        return c < t.c;
    }
} a[N];

int main()
{
    IOS;
    int T = 1;
    cin >> T;
    while(T -- )
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ )
            cin >> a[i].w;
        for (int i = 0; i < n; i ++ )
            cin >> a[i].c;
        sort(a, a + n);

        int num = 1;
        bool flag = true;
        for (int i = 0; i < n; i ++ )
        {
            if(a[i].w > num && a[i].c == 'R') 
            {
                flag = false;
                break;
            }
            else if(a[i].w < num && a[i].c == 'B')
            {
                flag = false;
                break;
            }
            else
                num++;
        }
        if(flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

 

posted @ 2021-11-09 21:34  彦辰kkkkk  阅读(36)  评论(0)    收藏  举报