1607D - Blue-Red Permutation - CodeForces Solution


greedy math sortings *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long 
using namespace std;

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        string s;
        cin>>s;
        int red=0,blue=0;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='R')
            {
                red++;
            }
            else
            {
                blue++;
            }
        }
        vector<pair<char,int>>vec(n);
        for(int i=0;i<n;i++)
        {
            vec[i]={s[i],a[i]};
        }
        bool b=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]>n && s[i]=='R')
            {
                b=1;
                break;
            }
            else if(a[i]<=0 && s[i]=='B')
            {
                b=1;
                break;
            }
        }
        if(b==1)
        {
            cout<<"NO"<<'\n';
        }
        else
        {
            sort(vec.begin(),vec.end());
        
            for(int j=0;j<blue;j++)
            {
                if(vec[j].second<j+1)
                {
                    b=1;
                    break;
                }
            }
            for(int j=blue;j<n;j++)
            {
                if(vec[j].second>j+1)
                {
                    b=1;
                    break;
                }
            }
            if(b==0)
            {
                cout<<"YES"<<'\n';
            }
            else
            {
                cout<<"NO"<<'\n';
            }
        }
    }
}


Comments

Submit
0 Comments
More Questions

Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum