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(思维,好题)的更多相关文章

  1. [Gym101982M][思维好题][凸壳]Mobilization

    [gym101982M][思维好题][凸壳]Mobilization 题目链接 20182019-acmicpc-pacific-northwest-regional-contest-div-1-en ...

  2. 土题大战Vol.0 A. 笨小猴 思维好题

    土题大战Vol.0 A. 笨小猴 思维好题 题目描述 驴蛋蛋有 \(2n + 1\) 张 \(4\) 星武器卡片,每张卡片上都有两个数字,第 \(i\) 张卡片上的两个数字分别是 \(A_i\) 与 ...

  3. 思维水题:UVa512-Spreadsheet Tracking

    Spreadsheet Tracking Data in spreadsheets are stored in cells, which are organized in rows (r) and c ...

  4. 【CodeForces - 707B】Bakery(思维水题)

    Bakery Descriptions 玛莎想在从1到n的n个城市中开一家自己的面包店,在其中一个城市烘焙松饼. 为了在她的面包房烘焙松饼,玛莎需要从一些储存的地方建立面粉供应.只有k个仓库,位于不同 ...

  5. CodeForces 604C 【思维水题】`

    题意: 给你01字符串的长度再给你一个串. 然后你可以在这个串中选择一个起点和一个终点使得这个连续区间内所有的位取反. 求: 经过处理后最多会得到多少次01变换. 例如:0101是4次,0001是2次 ...

  6. HDU 2674 N!Again(数学思维水题)

    题目 //行开始看被吓一跳,那么大,没有头绪, //看了解题报告,发现这是一道大大大的水题,,,,,//2009 = 7 * 7 * 41//对2009分解,看它有哪些质因子,它最大的质因子是41,那 ...

  7. HDOJ/HDU 1256 画8(绞下思维~水题)

    Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发. Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中有一个字符和一 ...

  8. 又一道简单题&&Ladygod(两道思维水题)

    Ladygod Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  9. 2019年华南理工校赛(春季赛)--I--炒股(简单思维水题)

    水题,想想就过了 题目如下: 链接:https://ac.nowcoder.com/acm/contest/625/I来源:牛客网 攒机一时爽,一直攒机一直爽. 沉迷攒机的胡老师很快就发现,他每天只能 ...

随机推荐

  1. hdu 3549 初试最大流问题

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  2. PBE加密 .net 实现

    using System; using System.Security.Cryptography; using System.Text; namespace Demo { internal class ...

  3. Oracle 分页语句

    ** 写法1 :采用 ROWNUM的伪列: --查询10到20之间的数据 -- SELECT * FORM ( -- SELECT * , ROWNUM rn FROM TABLE_NAME -- W ...

  4. Graphics与Canvas

    Graphics: 1. java.awt.Graphics;2.android.graphics Canvas:1.java.awt.Canvas;2.android.graphics.Canvas ...

  5. Spring事务的配置、参数详情及其原理介绍(Transactional)

    Spring 事务管理分为编程式和声明式的两种方式.编程式事务指的是通过编码方式实现事务:声明式事务基于 AOP,将具体业务逻辑与事务处理解耦.声明式事务管理使业务代码逻辑不受污染, 因此在实际使用中 ...

  6. RabbitMQ的特点与应用场景(二)

      1.RabbitMQ的主要特点 (1)可靠性:RabbitMQ可通过队列持久化,交换机持久化,消息持久化及ACK回应等机制保证可靠性 (2)支持多种语言与协议:RabbitMQ几乎支持所有的编程语 ...

  7. Browser Security-超文本标记语言(HTML)

    Browser Security-超文本标记语言(HTML) 瞌睡龙 · 2013/06/19 18:55 重要的4个规则: 1 &符号不应该出现在HTML的大部分节点中. 2 尖括号< ...

  8. Linux下创建NFS来实现共享文件

    简介说明: 在项目生产环境我们经常需要实现文件共享,传统的常见方案是通过NFS,实现服务器之间共享某一块磁盘,通过网络传输将分散的文件集中存储在一块指定的共享磁盘,实现基本的文件共享.实现这种方案,分 ...

  9. 【TCP】连接管理

    TCP连接管理   本节将介绍一条TCP连接是如何建立和拆除的.此处假设客户机A上面的一个进程想要和服务 器B上的一个进程建立一条TCP连接.本文前面介绍的是比较正常的连接和拆除,特殊的会在后面介绍. ...

  10. CSS 中用户自定义字体 @font-face

    @font-face 允许网页中使用自定义的字体,这些自定义的字体被放置在服务器上,从而让网页摆脱对访问者计算机上字体环境的依赖. 简单的说,有了@font-face,只需将字体上传到服务器端,无论访 ...