ZOJ - 4102 Array in the Pocket

Array in the Pocket

部分公式复制不过来http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4102
Time Limit: 2 Seconds Memory Limit: 65536 KB
BaoBao has just found an array of integers in his left pocket. As BaoBao is bored, he decides to rearrange it into another array of integers such that is a permutation of , and for all . Please help BaoBao finish the rearragement.

If multiple valid rearrangements exist, find the smallest one among them.

Consider two arrays and , we say is smaller than if there exists an integer such that , for all , and .

Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer (), indicating the length of the array.

The second line contains integers (), indicating the given array.

Output
For each test case output one line containing integers separated by a space, indicating the answer. If there are multiple valid answers, output the smallest one. If no valid answer exists, print “Impossible” (without quotes) instead.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

Sample Input
3
4
4 1 3 2
4
1 1 2 3
3
1 1 1
Sample Output
1 2 4 3
2 3 1 1
Impossible
Author: WENG, Caizhi
Source: The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

数据排序,然后从小到大填入,每次维护 当前还有要填的num个数 比 剩下的空位多,即 当前要填的num个数 + 后面的num个数 > n - i;

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#define ll long long 
using namespace std;

int d[100010] = {},m[100010] = {},c[100010] = {},ans[100010] = {};

struct vn{
	int num; 
	int val;//当前还有要填的num个数 比 剩下的空位多,即 当前要填的num个数 + 后面的num个数大于 n - i; 
	vn(int x = 0,int y = 0):num(x),val(y){}
	friend bool operator < (vn x,vn y)
	{
		return x.val < y.val;
	}
};

int main()
{
	int p;
	cin>>p;
	while (p--)
	{
		priority_queue<vn> q;
		set<int> s;//数值从小到大排序 
		int n;
		cin>>n;
		for (int i = 1;i<=n;i++)
		{
			scanf("%d",&m[i]);
			s.insert(m[i]);
			d[m[i]]++;//存还剩下几个值要填 
		}
		set<int>::iterator it;
		for (it = s.begin();it != s.end();it++)
		{
			int num = *it;
			q.push(vn(num,d[num] * 2));
		}
		bool t = true;
		int change[100001] = {};//存改变的 val
		for (int i = 1;i<=n;i++)
		{
			change[m[i]]++;
			bool check = false;
			for (it = s.begin();it != s.end();it++)
			{
				int num = *it;
				if (num == m[i]) continue;//如果 num 和当前要填的值一样 
				while (d[q.top().num] == 0) q.pop();//如果已经没有要填的了 
				vn temp = q.top();
				while (change[q.top().num] != 0)//如果最大的val需要改变 
				{
					temp = q.top();
					temp.val -= change[temp.num];
					change[q.top().num] = 0;
					q.pop();
					q.push(temp);
				}
				temp = q.top();
				int count = temp.val;
				if (temp.num == num) count--;//如果要填的num和最大的val的num相同
				if (count <= n - i)
				{
					ans[i] = num;
					d[num]--;
					change[num]++;
					if (d[num] == 0) s.erase(it);
					check = true;
					break;
				}
			}
			if (check == false)
			{
				t = false;
				break;
			}
		}
		if (t) 
		{
			printf("%d",ans[1]);
			for (int i = 2;i<=n;i++) printf(" %d",ans[i]);
			printf("\n");
		}
		else printf("Impossible\n");
		memset(d,0,sizeof(d)); memset(m,0,sizeof(m)); memset(ans,0,sizeof(ans));		
	}
	return 0; 
}

之前WA哭了,后来意识到自己维护没有实时维护,不说了爆零自闭orz

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值