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来源:牛客网 攒机一时爽,一直攒机一直爽. 沉迷攒机的胡老师很快就发现,他每天只能 ...
随机推荐
- AOP与IOC区别
Spring核心知识 Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由Rod Johnson在其著作Expert One-On-One J2EE Devel ...
- Inversion 多校签到题
存下值和下标后排序,每次从坐后面开始取就可以了. ac代码: #include <algorithm> #include <cstdio> #include <cstri ...
- MSP432 BSL流程(UART)
升级流程 PC程序会解析脚本中的命令,根据命令码做相应的操作.数据来自于命令后的文件(当前目录下的数据文件) # cat script_P4xx_uart.txt LOG //记录日志 MODE P4 ...
- Java并发编程之线程池及示例
1.Executor 线程池顶级接口.定义方法,void execute(Runnable).方法是用于处理任务的一个服务方法.调用者提供Runnable 接口的实现,线程池通过线程执行这个 Runn ...
- 【python】导入自定义模块
一.直接import 1.当执行文件与要导入的py文件在同一目录下时 假设要在wangyi.py中导入weibo.py文件 import weibo 2.当执行文件与要导入的py文件所在文件夹在同一目 ...
- 【atcoder】Enclosed Points [abc136F]
题目传送门:https://atcoder.jp/contests/abc136/tasks/abc136_f 题目大意:在平面上有$n$个点我们,定义一个点集的权值为平面上包含这个点集的最小矩形所包 ...
- ppp协议解析二
转:http://blog.csdn.net/yangzheng_yz/article/details/11526747 PPP(Point to Point Protocol,点对点协议)协议是为在 ...
- ListSetAndMap
package com.collection.test; import java.util.ArrayList; import java.util.HashMap; import java.util. ...
- 02_Hive安装简介
1.下载Hive安装包: 官网下载:http://hive.apache.org/downloads.html 百度云分享:https://pan.baidu.com/s/1M4LmdOXaq6T-P ...
- Js操作DOM元素及获取浏览器高宽
在JavaScript中,经常会来获取Document文档元素,是 HTML 文档对象模型的英文缩写,Document Object Model for HTML,是基于浏览器编程,HTML DOM ...