codeforces 891 b
2 seconds
256 megabytes
standard input
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.

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.
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.
2
1 2
2 1
4
1000 100 10 1
100 1 1000 10
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的更多相关文章
- Codeforces 891 C Envy
题目大意 给定一个 $n$ 个点 $m$ 条边的连通的无向图,每条边有一个权值,可能有重边.给出 $q$ 组询问,一组询问给出 $k$ 条边,问是否存在一棵最小生成树包含这 $k$ 条边. 思路 这道 ...
- codeforces #446 892A Greed 892B Wrath 892C Pride 891B Gluttony
A 链接:http://codeforces.com/problemset/problem/892/A 签到 #include <iostream> #include <algor ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- prometheus 通过企业微信接收告警
准备工作 step 1: 访问网站 注册企业微信账号(不需要企业认证). step 2: 访问apps 创建第三方应用,点击创建应用按钮 -> 填写应用信息: prometheus 配置: # ...
- 1L - ASCII码排序
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符. Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格. Output 对于每组输入数据,输出一行,字符中间用一个空格 ...
- python之初接触
编程语言相关 1什么是编程语言 编程语言即语言,语言的本质就是沟通,因而编程语言与英语 .法语.日语等所有语言并无区别,只不过英语是人与人之间沟通的介质,而编程语言则是程序员与计算机沟通的介质. 程序 ...
- javascript 高级程序设计 十
理解JS对象(2)创建对象 JS中创建对象的方式有很多,我们把他们统称为模式. 工厂模式: 优点:解决了创建多个相似对象的问题. 缺点:没有解决对象识别问题.(不知道一个实例对象的类型) func ...
- SNP命名
SNP命名 [2016-11-24] 奶茶妹妹是谁,京东老板娘,咦?章泽天!没错! 国民老公是谁?万达少东家,王健林儿子,王思聪!恭喜你又答对了! 函数是谁?这不是数学上的名词吗?不对,是 ...
- Luogu 2812 校园网络 - Tarjan
Description 给出一个有向图, 要求出至少从哪几个点出发, 能不漏地经过所有节点. 再求出至少加几条边, 才能使图变成一个强联通分量 Solution 求出所有强联通分量, 形成一个有向无环 ...
- 6.26实力测试(小错笑cry)
6.26测试 本次考试的粗心不忍吐槽(自带贴吧喷水表情),本次考试主要考察的知识点如下: 算法的分析与精简 暴力枚举输出字符 判断与枚举的综合考察 题目 第一题 [问题描述] 在一口井里,有一只神牛( ...
- 判断\r\n结束标准
ucPrev==0x0D&&ucCur==0x0A \r\n uint8_t ucPrev =0,ucCur = 0; uint32_t recvlen = 0; for (; uiL ...
- JSONObject 的使用学习
http://www.cnblogs.com/hitwtx/articles/2468633.html put与accumulate,element方法的区别什么? http://blog.csdn. ...
- tomcat探索
双击apache-tomcat-7.0.64.exe,一路默认点. 静态页路径 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webap ...