Educational Codeforces Round 120 (Rated for Div. 2) B. Berland Music

题目链接:Problem - B - Codeforces

Berland Music is a music streaming service built specifically to support Berland local artist. Its developers are currently working on a song recommendation module.

So imagine Monocarp got recommended nn songs, numbered from 1 to n. The i-th song had its predicted rating equal to pi, where 1≤pi≤n and every integer from 1 to nn appears exactly once. In other words, p is a permutation.

After listening to each of them, Monocarp pressed either a like or a dislike button. Let his vote sequence be represented with a string s, such that si=0 means that he disliked the i-th song, and si=1 means that he liked it.

Now the service has to re-evaluate the song ratings in such a way that:

  • the new ratings q1,q2,…,qn still form a permutation (1≤qi≤n; each integer from 1 to n appears exactly once);
  • every song that Monocarp liked should have a greater rating than every song that Monocarp disliked (formally, for all i,j such that si=1 and sj=0, qi>qj should hold).

Among all valid permutations qq find the one that has the smallest value of ∑i=1n|pi−qi|∑i=1n|pi−qi|, where |x| is an absolute value of x.

Print the permutation q1,q2,…,qn. If there are multiple answers, you can print any of them.

Input

The first line contains a single integer tt (1≤t≤104) — the number of testcases.

The first line of each testcase contains a single integer nn (1≤n≤2⋅105) — the number of songs.

The second line of each testcase contains nn integers p1,p2,…,pn (1≤pi≤n) — the permutation of the predicted ratings.

The third line contains a single string ss, consisting of nn characters. Each character is either a 0 or a 1. 0 means that Monocarp disliked the song, and 1 means that he liked it.

The sum of nn over all testcases doesn't exceed 2⋅105.

Output

For each testcase, print a permutation q — the re-evaluated ratings of the songs. If there are multiple answers such that ∑i=1n|pi−qi|∑i=1n|pi−qi| is minimum possible, you can print any of them.

Example

input

3
2
1 2
10
3
3 1 2
111
8
2 3 1 8 5 4 7 6
01110001

output

2 1
3 1 2
1 6 5 8 3 2 4 7

Note

In the first testcase, there exists only one permutation qq such that each liked song is rating higher than each disliked song: song 11 gets rating 22 and song 22 gets rating 11. ∑i=1n|pi−qi|=|1−2|+|2−1|=2∑i=1n|pi−qi|=|1−2|+|2−1|=2.

In the second testcase, Monocarp liked all songs, so all permutations could work. The permutation with the minimum sum of absolute differences is the permutation equal to pp. Its cost is 00.

题意:有n首歌,每首歌有一个分数,然后给出一个对每首歌的态度,如果喜欢就是1,不喜欢就是0,然后喜欢的歌不管之前的分数有多低,都会比不喜欢的歌高,现在请你给这些新的分数,要求该变量最小

思路:按照喜欢值从小到大排序,然后,相同的按照原本分数小到打排序,然后按顺序给分数就好了

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

struct node{
	int num;
	int i;
	int ans;
	int a;
}arr[200005];

bool cmp(node a, node b){
	if(a.a != b.a){
		return a.a < b.a;
	}
	return a.num < b.num;
}

bool cmp1(node a, node b){
	return a.i < b.i;
}

int main(){
	ios::sync_with_stdio(false);
	int t;
	int n;
	cin >> t;
	string s;
	while(t--){
		cin >> n; 
		for(int i = 1; i <= n; i++){
			cin >> arr[i].num;
			arr[i].i = i;
		}
		cin >> s;
		for(int i = 0; i < s.length(); i++){
			if(s[i] == '1'){
				arr[i + 1].num++;
				arr[i + 1].a = 1;
			}else{
				arr[i + 1].num--;
				arr[i + 1].a = 0; 
			}
		}
		sort(arr + 1, arr + 1 + n ,cmp);
		for(int i = 1; i <= n; i++){
			arr[i].ans = i;
		}
		sort(arr + 1, arr + 1 +n, cmp1);
		for(int i = 1; i <= n; i++){
			if(i != 1){
				cout << " ";
			}
			cout << arr[i].ans;
		}
		cout << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值