标签:put 选择 distinct diff get position other turn std
codeforces 891B Gluttony
链接:http://codeforces.com/problemset/problem/891/B
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S?=?{x1,?x2,?...,?xk} (1?≤?xi?≤?n, 0?<?k?<?n) the sums of elements on that positions in a and b are different, i. e.
The first line contains one integer n (1?≤?n?≤?22) — the size of the array.
The second line contains n space-separated distinct integers a1,?a2,?...,?an (0?≤?ai?≤?109) — the elements of the array.
If there is no such array b, print -1.
Otherwise in the only line print n space-separated integers b1,?b2,?...,?bn. Note that b must be a permutation of a.
If there are multiple answers, print any of them.
2
1 2
2 1
4
1000 100 10 1
100 1 1000 10
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.
Note that the empty subset and the subset containing all indices are not counted.
首先注意到数组的元素值没有重复的。
构造一种解决方案:
每个数选择一个刚好大于它的数与之对应,最大的数用最小的数对应(相当于右移一位的环)。
即
\[(1,2,3,4)-->(2,3,4,1)\]
证明这种方案的正确性。
选择了一个子集 \({x_1,x_2,x_3,\dots ,x_k}\),且最小的数不在这个子集中。
因此对于所有的情况,这种构造方法都是正确的。
#include <bits/stdc++.h>
#define ll long long
#define inf 1000000000
#define PI acos(-1)
#define bug puts("here")
#define REP(i,x,n) for(int i=x;i<=n;i++)
#define DEP(i,n,x) for(int i=n;i>=x;i--)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=25;
int a[N],b[N],ans[N];
int main(){
// while(1)
{
int n=read();
REP(i,0,n-1) a[i]=read(),b[i]=a[i];
sort(a,a+n);
REP(i,0,n-1){
int id=lower_bound(a,a+n,b[i])-a;
printf(i<n-1?"%d ":"%d",a[(id+1)%n]);
}
puts("");
}
return 0;
}
【CODEFORCES】 891B Gluttony(构造)
标签:put 选择 distinct diff get position other turn std
原文地址:http://www.cnblogs.com/zsyacm666666/p/7856483.html