D. Blue-Red Permutation

D. Blue-Red Permutation

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given an array of integers a of length n. The elements of the array can be either different or the same.

Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step:

either you can select any blue element and decrease its value by 1;
or you can select any red element and increase its value by 1.
Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.

Determine whether it is possible to make 0 or more steps such that the resulting array is a permutation of numbers from 1 to n?

In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array a contains in some order all numbers from 1 to n (inclusive), each exactly once.

Input
The first line contains an integer t (1≤t≤104) — the number of input data sets in the test.

The description of each set of input data consists of three lines. The first line contains an integer n (1≤n≤2⋅105) — the length of the original array a. The second line contains n integers a1, a2, …, an (−109≤ai≤109) — the array elements themselves.

The third line has length n and consists exclusively of the letters ‘B’ and/or ‘R’: ith character is ‘B’ if ai is colored blue, and is ‘R’ if colored red.

It is guaranteed that the sum of n over all input sets does not exceed 2⋅105.

Output
Print t lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise.

You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).

题目大意:

给一组数,数量为n,每个数有颜色 ‘B’ / ‘R’ , 若是’B’ 则可以对它的值减一,‘R’ 加一
问是否可以构造出一个序列 即一组数里包含 1 到 n ;

思路:

用蓝色的数字去构造前面的数,红色构造后面的数
比如 蓝色有 k k k个数 则用蓝色构造 1 到 k 1 到 k 1k,红色构造 k + 1 到 n k+1 到 n k+1n

因为 蓝色都是减值 故 如果它不在这个构造范围内通过减可以进入这个范围 ,红色同理;
这样我们把蓝色和红色分开存储排序,蓝色从小到大,红色从大到小
然后考虑蓝色组 第 i 个数要大于等于i 否则无法构造;
红色组 第i个数要小于等于 n 否则无法构造;

std:

/*Nqvqr st0p Th1nk1ng
我的代码很大,评测姬你忍一下~*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a; i <= b;i++)
const int INF = 0x3f3f3f3f;
const long long mod = 1e9+7;
const int N = 2e5+10;
int n;
pair<int,char> p[N];
void so1ve()
{
    
    
    int n; cin >> n;
    vector<int> a(n);
    forr(i,0,n-1) cin >> a[i];
    vector<int> l,r;
    string s;cin >> s;
    forr(i,0,n-1) (s[i] == 'B'?l:r).push_back(a[i]);
    sort(l.begin(),l.end());
    sort(r.begin(),r.end(),greater<int>());

    bool lab = true;
    int len1 = l.size();
    forr(i,0,len1 -1){
    
    
        if(l[i] < i + 1) lab = false;
    }
    len1 = r.size();
    forr(i,0,len1-1){
    
    
        if(r[i] > n - i) lab = false;
    }
    cout <<(lab?"YES":"NO") << endl;
}
int main()
{
    
    
    int t;cin>>t;
    while(t--) so1ve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51687628/article/details/121159582