Codeforces 892 D. Gluttony (思维)

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.

>i=1kaxii=1kbxi>

 

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 ≤ 10^9) — 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

 

Examples output

2 1

 

题意

寻找给定排列的一个置换,满足任意一个下标集合在 a 与 b 之间选中值的和都不同(不包括全集)。

 

思路

结论题,首先对原序列进行排序,然后循环左移一位,此时这两个排列满足题目中所说的要求,然后我们把新得到的序列按照原来的下标填入进去即可。

证明:

我们设 t={x1,x2,,xk} a 为排序后的序列, b a 循环左移一位得到的结果。

显然,循环左移一位会使得 b1...bn1>a1...an1 ,此时关键讨论 n 是否在 t 中:

  • nt ,显然 xt,bx>ax ,则 ki=1bxi>ki=1axi
  • nt ,我们考虑其逆事件,因为 ni=1ai=ni=1bi ,于是 ki=1bxi<ki=1axi

因此, ki=1bxiki=1axi ,证毕。

 

AC 代码

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
typedef __int64 LL;
const int maxn = 1e5+10;

int a[maxn];
typedef pair<int,int> P;
P p[maxn];
int main()
{
    IO;
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i],p[i] = P(a[i],i);
    sort(p,p+n);
    for(int i=0; i<n; i++)
        a[p[(i+1)%n].second] = p[i].first;
    for(int i=0; i<n; i++)
        cout<<a[i]<<" ";
    return 0;
}
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值