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.
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int> >a;
int b[222];
int main(){
int n;
cin >> n;
int x;
for(int i = 1; i <= n; ++i){
cin >> x;
a.push_back(make_pair(x, i - 1));
}
sort(a.begin(), a.end());
for(int i = 0; i < n - 1; ++i){
b[a[i].second] = a[i + 1].first;
}
b[a[n - 1].second] = a[0].first;
for(int i = 0; i < n; ++i){
cout<<b[i]<<" ";
}
}
/*
题意:
22个不同的数,构造一个不同的排列,使两个排列任意相同位置的数字集合和不同。
思路:
构造题,,,说实话我比赛时是猜的结论。。将最大移到次大,次大移到次次大,最小移到最大,结束。。
*/

本文介绍了一个算法问题,即如何构造一个独特的数组排列b,使得它与给定数组a的任意相同位置子集和都不同。文章提供了一种通过重新排列元素来解决此问题的有效方法。
432

被折叠的 条评论
为什么被折叠?



