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
本文探讨了一种特殊的数据排序问题,旨在通过重新排列数组元素以满足特定条件,实现最优解。问题源自ACM竞赛,要求在保持元素唯一性的同时,确保每个元素的前后关系符合一定规则,且在多种可能中找到最小的那个。文章提供了详细的算法思路及代码实现,适合对数据结构和算法有兴趣的读者。
1599

被折叠的 条评论
为什么被折叠?



