Educational Codeforces Round 120 B. Berland Music

原题链接:Problem - B - Codeforces

题目大意:输入一个数n,给出n首歌曲评分的排列。给出一个长度为n且只为1或0的字符串s,每一个0或1对应一首歌曲的评价。要求根据s对歌曲重新评分并重新输出排列。重新评分的要求为:1.重新评分后得到的也需要是一个排列。2.评价为1的歌曲评分一定要比评价为0的歌曲高。3.使得新排列的次序尽可能接近原排列。

解题思路:结构体排序,对排序过后的歌曲重新赋值并输出即可。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define TL ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define mms(a,b) memset(a,b,sizeof(a))

const int maxn = 200009;

int t,n,a[maxn];
string s;

struct node
{
	int p,q,r;
}songs[maxn];

bool cmp(node a,node b)
{
	if(a.r == b.r) return a.p < b.p;
    else return a.r < b.r;
}

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

int main()
{
	cin >> t;
	while(t--)
	{
		cin >> n;
		for(int i = 0;i < n;i++)
		{
			cin >> songs[i].p;
            songs[i].q = i;
		}
		cin >> s;
		for(int i = 0;i < n;i++)
		{
			if(s[i] == '0') songs[i].r = 0;
			else songs[i].r = 1;
		}
		sort(songs,songs+n,cmp);
		for(int i = 0;i < n;i++) songs[i].p = i+1;
		sort(songs,songs+n,cmp1);
		for(int i = 0;i < n;i++) cout << songs[i].p << ' ';
		cout << '\n';
	}
}
posted @ 2021-12-28 14:07  wbw1537  阅读(219)  评论(0)    收藏  举报