1245B - Restricted RPS - CodeForces Solution


constructive algorithms dp greedy *1200

Please click on ads to support us..

Python Code:

import math
for i in range(int(input())):
    n = int(input())
    r ,p , S = map(int , input().split())
    s = input()
    ans = []
    cnt = 0
    for j in range(n):
        if s[j]=="R":
            if p>0:
                ans.append("P")
                p-=1
                cnt+=1
            else:
                ans.append(" ")
        elif s[j]=="P":
            if S>0:
                ans.append("S")
                S-=1
                cnt+=1
            else:
                ans.append(" ")
        elif s[j]=="S":
            if r>0:
                ans.append("R")
                r-=1
                cnt+=1
            else:
                ans.append(" ")
        
        for j in range(len(ans)):
        if ans[j] == " ":
            if r>0:
                ans[j] = "R"
                r-=1
            elif p>0:
                ans[j] = "P"
                p-=1
            elif S>0:
                ans[j] = "S"
                S-=1
    if cnt >= (n+1)//2:
        print("YES")
                for j in range(n):
            print(ans[j] , end="")
        print()
    else:
        print("NO")

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0,r,p,s,flag=0;
        cin>>n>>r>>p>>s;
        string s1;
        cin>>s1;
        int x=(n/2)+(n%2);
        int arr[n];
        memset(arr,0,sizeof(arr));
         for(int i = 0; i < n; i++)
        {
            if(s1[i] == 'R')
            {
                if(p)
                    count++, p--, arr[i] = 2;
            }

            else if(s1[i] == 'P')
            {
                if(s)
                    count++, s--, arr[i] = 3;
            }

            else
            {
                if(r)
                    count++, r--, arr[i] = 1;
            }
        }
       if(count >= x)
        {
            cout<<"YES"<<endl;
            for(int i = 0; i < n; i++)
            {
                if(!arr[i])
                {
                    if(r)
                        arr[i] = 1, r--;
                    else if(p)
                        arr[i] = 2, p--;
                    else
                        arr[i] = 3, s--;
                }

                if(arr[i] == 1)
                    cout<<"R";
                else if(arr[i] == 2)
                    cout<<"P";
                else
                    cout<<"S";
            }
            cout<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
}


Comments

Submit
0 Comments
More Questions

991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language