1427B - Chess Cheater - CodeForces Solution


greedy implementation sortings *1400

Please click on ads to support us..

Python Code:

output=[]
for _ in range(int(input())):
	n,k=map(int,input().split())
	s=input()
	L=[]
	i=1
	t=0
	count=1
	while i<=len(s)-1:
		if i==1 and s[i]=='W' and s[0]=='L':
			L.append(0)
			t=1
		if s[i]=='W':
			if i-t-1>0:
				L.append(i-t-1)
			t=i
			if i>=1:
				if s[i-1]=='W':
					count+=2
				elif s[i-1]=='L':
					count+=1
		i+=1
	E=[]
	if s=='W':
		output.append('1')
	elif 'W' not in s:
		if n<=k:
			output.append(str(1+2*(n-1)))
		else:
			if k>=1:
				output.append(str(1+2*(k-1)))
			elif k==0:
				output.append(str(0))
	else:
		if s[-1]=='L':
			E.append(n-1-t)
		if s[0]=='L':
			E.append(L.pop(0)+1)
			count-=1		
		c=sum(L)+sum(E)
		if k>=c:
			output.append(str(2*n-1))
		else:
			L=sorted(L)
			E=sorted(E)
			m=0
			for j in range(len(L)):
				if L[j]<=k-m:
					count+=2*(L[j]-1)+3
					m+=L[j]
				else:
					count+=2*(k-m)
					break
			else:
				for p in range(len(E)):
					if E[p]<=k-m:
						count+=2*E[p]
						m+=E[p]
					else:
						count+=2*(k-m)
						break
			output.append(str(count))
	
print('\n'.join(output))
			

C++ Code:

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

// Macros definition
#define ll long long
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define ln cout<<"\n";
#define print(a) cout<< a << ' '

// Predefined values
ll mod = 1e9+7;

void solve(){
    ll n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    ll w=0;
    for(char c : s){
        w += (c=='W');
    }
    if(w==0){
        k=min(k,n);
        print(max(0ll,2*k-1));
        ln;
        return;
    }
    w += k;
    if(w>=n){
        print(2*n-1);
        ln;
        return;
    }
    vector<ll>v;
    int l=0;
    for(int i=0; i<n; i++){
        if(s[i]=='L') l++;
        else{
            v.push_back(l);
            l=0;
        }
    }
    if(l>0) v.push_back(l);
    if(s[0]=='L'){
        v.erase(v.begin());
    }
    if(s.back()=='L') v.pop_back();
    sort(v.begin(), v.end());
    ll cnt=0, i=0;
    while(i<v.size()){
        if(cnt+v[i]>k) break;
        cnt += v[i];
        i++;
    }
    int x=v.size()-i;
    print(2*w-1-x);
    ln;
}

int main(){

    // Deepu Yadav
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions