B. Gluttony
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

讲一下题意把吧   这个题的意思就是n个数a1,a2,a3.......,这n个数重新排列构成b数组

                                     

这个题吧   sum1(左边式子)和sum2(右边式子)都是从0开始加的,只要我能够使a[i]<b[i],那么sum2一定比sum1要大   这就是确定的

但是很明显,我做不到这个,

于是我这么构造,每个a都找到一个刚刚大于它的数,最大的数用最小的数代替,假设我现在计算的集合中没有最大的数和最小的数的组合

那么我的每个b元素都大于与之对应的a    sum2一定大于sum1,

这时候我们再考虑最大的数和最小的数的组合在集合中的情况

由于我们的k<n && k>=1 那么我一定不可能把所有的情况都取干净  所以必定有一些集合是a < b没有取到的

这时候  正难则反  总共加和都是一样的,减去相差的,这时候sum1 > sum2了就

于是这样构造是能够满足题意的

丑陋的代码:

#include <iostream>

#include <cstdio>

#include <algorithm>

using namespace std;

int arr[30];

int res[30];

int bin(int left,int right,int n)

{

int mid ;

while(left < right)

{

mid = (left+right) >> 1;

if(res[mid] > n) {

right = mid;

}

else if(res[mid] == n) {

return res[mid+1];

}

else if(res[mid] < n) {

left = mid + 1;

}

}

return res[mid+1];

}

int main()

{

int n,i,j;

int maxn = 0;

bool color_flag = false;

scanf("%d",&n);

for(i =0 ; i < n; ++i) {

scanf("%d",arr+i);

res[i] = arr[i];

maxn = max(maxn,arr[i]);

}

sort(res,res+n);

for(i = 0; i < n - 1; ++i) {

if(arr[i] == maxn)

printf("%d ",res[0]);

else

printf("%d ",bin(0, n-1, arr[i]));

}

if(arr[i] == maxn)

printf("%d\n",res[0]);

else

printf("%d\n",bin(0, n-1, arr[i]));

}

codeforces 891 b的更多相关文章

  1. Codeforces 891 C Envy

    题目大意 给定一个 $n$ 个点 $m$ 条边的连通的无向图,每条边有一个权值,可能有重边.给出 $q$ 组询问,一组询问给出 $k$ 条边,问是否存在一棵最小生成树包含这 $k$ 条边. 思路 这道 ...

  2. codeforces #446 892A Greed 892B Wrath 892C Pride 891B Gluttony

    A  链接:http://codeforces.com/problemset/problem/892/A 签到 #include <iostream> #include <algor ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. 三种简单排序算法(java实现)

    一.冒泡排序 算法思想:遍历待排序的数组,每次遍历比较相邻的两个元素,如果他们的排列顺序错误就交换他们的位置,经过一趟排序后,最大的元素会浮置数组的末端.重复操                   作 ...

  2. Web 文件上传方面的安全问题

    一. 文件上传漏洞与WebShell的关系 文件上传漏洞是指网络攻击者上传了一个可执行的文件到服务器并执行.这里上传的文件可以是木马,病毒,恶意脚本或者WebShell等.这种攻击方式是最为直接和有效 ...

  3. Spring ApplicationContext(十)finishRefresh

    ApplicationContext(十)finishRefresh Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 经过 ...

  4. 【jdbcTemplate】baseDao书写规范

    今天加班,为了下个月的北京之行,希望父亲身体安康,一切顺利: 老大今天发出来同事的代码,并标记了jdbcTemplate的书写规范,此处查询数据库之前声明对象时,不用new出来,因为在底层源码中已经给 ...

  5. ios微信打开网页键盘弹起后页面上滑,导致弹框里的按钮响应区域错位

    input失去焦点,页面被顶起没有还原,所以就有以下的方法来处理 $("input").blur(function(){   setTimeout(() => {    co ...

  6. mybatis学习一 环境搭建

    第一步导入Jar包 第二步配置sqlMapConfig.xml文件,xml文件放在src下面 <?xml version="1.0" encoding="UTF-8 ...

  7. js文件中获取${pageContext.request.contextPath}

    一般从 JSP文件中,可以直接使用 ${pageContext.request.contextPath}非常方便的获得当前页面的路径,用来处理被 Apache2代理之后出现 URL变化的问题,比如增加 ...

  8. Linux关机操作

    正确的关机流程为:sync > shutdown > reboot > halt 关机指令为:shutdown ,你可以man shutdown 来看一下帮助文档. 例如你可以运行如 ...

  9. WPF编程宝典(Pro wpf in c# 2012)(文摘)

    第一部分 基础知识 第1章 WPF概述 第2章 XAML 第3章 布局 第4章 依赖项属性 第5章 路由事件 第二部分 进一步研究WPF 第6章 控件 第7章 Application类 第8章 元素绑 ...

  10. ASP.NET WebApi 基于分布式Session方式实现Token签名认证(发布版)

    一.课程介绍 明人不说暗话,跟着阿笨一起学玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将会是需要思考的问题.在ASP.NETWebSer ...