1. 程式人生 > >【CODEFORCES】 891B Gluttony(構造)

【CODEFORCES】 891B Gluttony(構造)

ray markdown tinc lower blog clu include first right

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(構造)