CF892D—Gluttony(思维,好题)
http://codeforces.com/contest/892/problem/D
D. Gluttony
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.
附上官方题解:
Sort the array and shift it by one. This array will be an answer.
Proof:
When we shift the sorted array all of the elements become greater except the first one, consider f = {1, 2, ..., n} and t = {x1, x2, ..., xk} if 1 wasn't in t we would have
otherwise consider q = {y1, y2, ..., yn - k} = f - t then 1 can't be in q and we have
so
and we are done!
其实就是
把a序列做一个排序,设为c序列,然后我们开始构造b序列
b[i]=c[(j+1)%n],满足c[j]=a[i]
例如:
a=[0,3,1,2]
那么
c=[0,1,2,3]
那么
b=[1,0,2,3]
b 就是我们要构造的序列
来自http://blog.csdn.net/weixin_37517391/article/details/78569584
证明:
若子集不包含最大数,则一定有
若子集包含最大数,则反过来考虑,其补集一定也有上面的式子成立,又因为n个数的sum相同,所以一定有
(给定了序列中的数都是不同的条件)
证毕
代码:

1 /*
2 * @FileName: /media/shan/Study/代码与算法/2017训练比赛/11.17/d.cpp
3 * @Author: Pic
4 * @Created Time: 2017年11月17日 星期五 19时34分11秒
5 */
6 #include <bits/stdc++.h>
7 using namespace std;
8 const int maxn=30;
9 int a[maxn];
10 int b[maxn];
11 int n;
12 bool cmp(int a,int b)
13 {
14 return a>b;
15 }
16 int fid(int x)
17 {
18 for(int i=0;i<n;i++){
19 if(b[i]==x){
20 return i;
21 }
22 }
23 }
24 int main()
25 {
26 // freopen("data.in","r",stdin);
27 //freopen("data.out","w",stdout);
28 cin>>n;
29 for(int i=0;i<n;i++){
30 cin>>a[i];
31 b[i]=a[i];
32 }
33 sort(b,b+n,cmp);
34 int index;
35 for(int i=0;i<n;i++){
36 index=fid(a[i]);
37 if(index==0){
38 cout<<b[n-1]<<" ";
39 }
40 else{
41 cout<<b[index-1]<<" ";
42 }
43 }
44 cout<<endl;
45 }
46
CF892D—Gluttony(思维,好题)的更多相关文章
- [Gym101982M][思维好题][凸壳]Mobilization
[gym101982M][思维好题][凸壳]Mobilization 题目链接 20182019-acmicpc-pacific-northwest-regional-contest-div-1-en ...
- 土题大战Vol.0 A. 笨小猴 思维好题
土题大战Vol.0 A. 笨小猴 思维好题 题目描述 驴蛋蛋有 \(2n + 1\) 张 \(4\) 星武器卡片,每张卡片上都有两个数字,第 \(i\) 张卡片上的两个数字分别是 \(A_i\) 与 ...
- 思维水题:UVa512-Spreadsheet Tracking
Spreadsheet Tracking Data in spreadsheets are stored in cells, which are organized in rows (r) and c ...
- 【CodeForces - 707B】Bakery(思维水题)
Bakery Descriptions 玛莎想在从1到n的n个城市中开一家自己的面包店,在其中一个城市烘焙松饼. 为了在她的面包房烘焙松饼,玛莎需要从一些储存的地方建立面粉供应.只有k个仓库,位于不同 ...
- CodeForces 604C 【思维水题】`
题意: 给你01字符串的长度再给你一个串. 然后你可以在这个串中选择一个起点和一个终点使得这个连续区间内所有的位取反. 求: 经过处理后最多会得到多少次01变换. 例如:0101是4次,0001是2次 ...
- HDU 2674 N!Again(数学思维水题)
题目 //行开始看被吓一跳,那么大,没有头绪, //看了解题报告,发现这是一道大大大的水题,,,,,//2009 = 7 * 7 * 41//对2009分解,看它有哪些质因子,它最大的质因子是41,那 ...
- HDOJ/HDU 1256 画8(绞下思维~水题)
Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发. Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中有一个字符和一 ...
- 又一道简单题&&Ladygod(两道思维水题)
Ladygod Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- 2019年华南理工校赛(春季赛)--I--炒股(简单思维水题)
水题,想想就过了 题目如下: 链接:https://ac.nowcoder.com/acm/contest/625/I来源:牛客网 攒机一时爽,一直攒机一直爽. 沉迷攒机的胡老师很快就发现,他每天只能 ...
随机推荐
- 怎样查看Redis的版本号
Q: 怎样查看Redis版本 A: 下面两条命令都可以查看redis 版本: redis-server --version redis-server -v
- 检索 COM 类工厂中 CLSID 为 {13C28AD0-F195-4319-B7D7-A1BDAA329FB8} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。
上午前客户突然来电说换了台电脑重新装的程序不能正常用,发来错误提示如图: 这错误显然不是程序错误,异常写的很清楚 ,COM组件没注册,搜一下CLSID, 原来是GridReport++ ,参考地址: ...
- uni-app中picker组件的一个坑
这里直接贴出代码 <view class="goods-info-add fl-sw"> <view>运费模板:</view> <view ...
- 6. Java基本数据类型
Java 基本数据类型 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此,通过定义不 ...
- Web Api 将DataTable装换成Excel,并通过文件流将其下载
不废话,直接上代码 前端代码 <input type="button" class="layui-btn" value="Test-GetFil ...
- host文件简介及修改后不能保存解决方法
一.文件概述 Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先 ...
- centos 6.4系统双网卡绑定配置详解
Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡(需要交换机支持),这个聚合起来的设备看起来是一个单独的以太网接口设备,通俗点讲就是两块网卡具有相同的IP地址而并行链接聚合成一个逻辑链路工作. ...
- 解决docker容器开启端口映射后,会自动在防火墙上打开端口的问题
在docker中运行第三方服务时,通常需要绑定服务端口到本地主机.但使用 -p 参数进行的端口映射,会自动在iptables中建立规则,绕过firewalld,这对于端口级的黑白名单控制管理是很不利的 ...
- 使用 visual studio 2019 社区版打开touchgfx工程注意项
@2019-09-23 [环境] touchgfx designer 4.10.0 visual studio 2019 社区版 [问题] #error 1 使用 visual studio 2019 ...
- SQLite3学习笔记(3)
SQLite 表达式 表达式是一个或多个值.运算符和计算值的 SQL函数的组合. SQL表达式与公式类似,都写在查询语言中.您还可以使用特定的数据集来查询数据库. SELECT语句的基本语法如下: S ...