ZOJ 4102 Array in the Pocket (贪心)

博客围绕数组重排问题展开,给定包含n个数的序列,需重新排列使每个位置上的数和原来不同且字典序最小。采用贪心思路,通过维护两个set来构造解,还给出无解的判断条件,最后提及代码实现但未展示具体代码。

Array in the Pocket


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

题意:

给你一个包含n(n<=1e5)个数(1<=每个数<=n)的序列,让你重新排列这个序列,使得重新排列后每个位置上的数和原来的不一样,并且字典序最小。

思路:

考虑贪心。

假设当前还有z个数要填,你手里数i的剩余数量为c[i],剩下的位置中有sum[i]个位置不能填i。

那么当max{c[i]+sum[i]}>z时,便无解。

max{c[i]+sum[i]}==z时,这个位置必须填i,否则找一个字典序最小的和a[i]不相等的填上即可。

因此我们用一个set1维护(c[i]+sum[i],i),一个set2维护(i,c[i]),便可以构造出解。

对于每个数i,我们先把a[i]对应的set1中(c[a[i]],a[i])进行更新,然后判断(--set1.end())->first是否==n-i+1,如果是则填i,不是就从set2中找一个最小的不等于a[i]的填上,然后再更新你填的这个数对应的set1、set2中的值。

如果刚开始就((--set1.end())->first)>n 那么肯定就是Impossible了。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=100010;
int n,m,k;
int a[maxn],c[maxn],sum[maxn];
int b[maxn],ok[maxn];
set<pair<int,int> >st1,st2;
set<pair<int,int> >::iterator it1,it2;
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(c,0,sizeof(c));
        memset(sum,0,sizeof(sum));
        rep(i,1,n)
        {
            scanf("%d",&a[i]);
            c[a[i]]++;
            sum[a[i]]+=2;
        }
        st1.clear();st2.clear();
        rep(i,1,n)
        if(c[i]){
            st1.insert(make_pair(sum[i],i));
            st2.insert(make_pair(i,c[i]));
        }
        it1=st1.end();it1--;
        if((*it1).first>n)
        {
            puts("Impossible");
            continue;
        }
        rep(i,1,n)
        {
            st1.erase(make_pair(sum[a[i]],a[i]));
            st1.insert(make_pair(--sum[a[i]],a[i]));
            pair<int,int>x=*(--st1.end());
            if(n-i+1==x.first)
            {
                b[i]=x.second;
            }
            else
            {
                if(a[i]==(st2.begin())->first)
                b[i]=(++st2.begin())->first;
                else b[i]=(st2.begin())->first;
            }
            st2.erase(make_pair(b[i],c[b[i]]));
            if(--c[b[i]]>0) st2.insert(make_pair(b[i],c[b[i]]));
            st1.erase(make_pair(sum[b[i]],b[i]));
            if(--sum[b[i]]>0) st1.insert(make_pair(sum[b[i]],b[i]));
        }
        rep(i,1,n)
        {
            printf("%d%c",b[i],i==n?'\n':' ');
        }
    }
    return 0;
}
/*
3
4
4 1 3 2
4
1 1 2 3
3
1 1 1
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值