Codeforces 892 D. Gluttony (思维)
来源:互联网 发布:江南大学网络接入系统 编辑:程序博客网 时间:2024/09/21 06:33
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=1kaxi≠∑i=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
21 2
Examples output
2 1
题意
寻找给定排列的一个置换,满足任意一个下标集合在 a 与 b 之间选中值的和都不同(不包括全集)。
思路
结论题,首先对原序列进行排序,然后循环左移一位,此时这两个排列满足题目中所说的要求,然后我们把新得到的序列按照原来的下标填入进去即可。
证明:
我们设
显然,循环左移一位会使得
- 当
n∉t ,显然∀x∈t,bx>ax ,则∑ki=1bxi>∑ki=1axi 。 - 当
n∈t ,我们考虑其逆事件,因为∑ni=1ai=∑ni=1bi ,于是∑ki=1bxi<∑ki=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;}
- Codeforces 892 D. Gluttony (思维)
- codeforces 891B Gluttony (构造+思维)
- Codeforces Round #446 (Div. 2) D. Gluttony
- CF 892D Gluttony 构造
- CF contest 892 D题 Gluttony
- codeforces 891 B. Gluttony(构造)
- Codeforces Round #446 (Div. 2) D. Gluttony (构造 贪心)
- Codeforces Round #446 (Div. 2) D. Gluttony 找规律
- Codeforces Round #446 (Div. 2) D. Gluttony 构造 置换
- [Codeforces 891B] Gluttony
- CodeForces 66D--思维(yy)
- CodeForces-89D:Space mines(思维)
- codeforces 836D(思维题)
- codeforces 765D 思维
- codeForces 612D(思维)
- codeforces 673D(思维)
- codeforces 724D 思维
- CF#446 Gluttony(思维题)
- 抽象类练习:抽象动物
- STM32Cube 代码生成配置
- 心得丨对于机器学习,到底该选择哪种编程语言
- HTML+css制作温馨浪漫爱心动画
- vue的v_if
- Codeforces 892 D. Gluttony (思维)
- print、print_r、echo var_dump的区别
- 一. TabLayout 横滑布局文件xml
- webView加载Https网页
- mybatis的基本使用
- 本地计算机的oracleoradb11g_home1tnslistener服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止
- 数据库视频总结
- MapReduce之如何处理失败的task
- Linux常用命令集