1622B - Berland Music - CodeForces Solution


data structures greedy math sortings *1000

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
	n = int(input())
	p = [int(x) for x in input().split()]
	s = input()
	l = sorted([[s[i], p[i], i] for i in range(n)])
	q = [-1 for i in range(n)]
	for i in range(n):
		q[l[i][2]] = i + 1
	print(*q)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long int
#define pb push_back
const int M=1e9+7;
const int N=1e6+1;
void solve(){
    int n;
    cin>>n;
    int arr[n+1];
    for(int i=1;i<=n;i++){
        cin>>arr[i];
    }
    string s;
    cin>>s;
    map<int,int> zero,one;
    for(int i=0;i<n;i++){
        if(s[i]=='0'){
            zero[arr[i+1]] += i+1;
        }else{
            one[arr[i+1]] += i+1;
        }
    }
    int x=zero.size();
    vector<int> zeroswap,oneswap;
    for(auto it1=zero.begin();it1 != zero.end();it1++){
        if(it1->first > x){
            zeroswap.pb(it1->second);
        }
    }
    for(auto it2 = one.begin(); it2 != one.end(); it2++){
        if(it2->first <= x){
            oneswap.pb(it2->second);
        }
    }
    // cout<<oneswap.size()<<endl;
    for(int i=0;i<oneswap.size();i++){
        swap(arr[zeroswap[i]],arr[oneswap[i]]);
    }
    for(int i=1;i<=n;i++){
        cout<<arr[i]<<" ";
    }cout<<endl;
}
signed main()
{
int t;
cin>>t;
while(t--){
solve();
}
 return 0;
}


Comments

Submit
0 Comments
More Questions

84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship