CODEFORCES 891B Gluttony(构造)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CODEFORCES 891B Gluttony(构造)相关的知识,希望对你有一定的参考价值。

codeforces 891B Gluttony
链接:http://codeforces.com/problemset/problem/891/B

Description


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.

Input


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.

Output


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.

Examples

input

2
1 2

output

2 1 

input

4
1000 100 10 1

output

100 1 1000 10

Note


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}\),且最小的数不在这个子集中。

  1. 考虑这个子集,必然
    \[\sum_1^ka_{x_i}<\sum_1^kb_{x_i}\]
  2. 考虑它的补集,必然
    \[\sum_1^{n-k}a_{x_i}>\sum_1^{n-k}b_{x_i}\]
    \(\Longrightarrow\)证明
    考虑这个补集为全集,那么最开始为
    \[\sum_1^{n-k}a_{x_i}=\sum_1^{n-k}b_{x_i},(k=0)\]
    倒着将其退化为真子集,即相应地减去几个元素,即\(\sum_1^{n-k}a_{x_i}-a[j]\),\(\sum_1^{n-k}b_{x_i}-b[j]\)
    因为减去的数不是最小的数,因此\(a[j]<b[j]\),可得
    \[\sum_1^{n-k}a_{x_i}>\sum_1^{n-k}b_{x_i}\]

因此对于所有的情况,这种构造方法都是正确的。

#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(构造)的主要内容,如果未能解决你的问题,请参考以下文章

openGL实现二维图形和三维图形

codeforces上怎么看测试数据

如何看codeforces做了多少题

codeforces上怎么看测试数据

codeforces比赛后怎么看题解和答案

codeforces是啥?