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
本文来自博客园,作者:Un-Defined,转载请保留本文署名Un-Defined,并在文章顶部注明原文链接:https://www.cnblogs.com/EIPsilly/p/17463707.html