CF 549G swap(前-1,后+1),问是否能变成递增序列(灵活题)
来源:互联网 发布:阿里数据与谁 编辑:程序博客网 时间:2024/05/18 00:28
Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of n Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people, so each person agrees to swap places with the person right behind him for just 1 dollar. More formally, if person astands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if person a has zero dollars, he can not swap places with person b.
Residents of Berland are strange people. In particular, they get upset when there is someone with a strictly smaller sum of money in the line in front of them.
Can you help the residents of Berland form such order in the line so that they were all happy? A happy resident is the one who stands first in the line or the one in front of who another resident stands with not less number of dollars. Note that the people of Berland are people of honor and they agree to swap places only in the manner described above.
The first line contains integer n (1 ≤ n ≤ 200 000) — the number of residents who stand in the line.
The second line contains n space-separated integers ai (0 ≤ ai ≤ 109), where ai is the number of Berland dollars of a man standing on the i-th position in the line. The positions are numbered starting from the end of the line.
If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line n space-separated integers, the i-th of them must be equal to the number of money of the person on position i in the new line. If there are multiple answers, print any of them.
211 8
9 10
510 9 7 10 6
:(
312 3 3
4 4 10
In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line.
In the second sample it is impossible to achieve the desired result.
In the third sample the first person can swap with the second one, then they will have the following numbers of dollars: 4 11 3, then the second person (in the new line) swaps with the third one, and the resulting numbers of dollars will equal to: 4 4 10. In this line everybody will be happy.
http://codeforces.com/problemset/problem/549/G
这题我是用能量守恒的方法考虑的。请先看一眼代码——开始的时候依次加了i,最后依次减去i,也就是说,数组的总和还是不变的。+i是为了使中间过程形成递增。
x[i]==x[i-1],假设可以,那么下面输出序列就是 x[i-1]-(i-1) > x[i]-i ,明显不可以。
#include <set>#include <map>#include <queue>#include <cmath>#include <cstdio>#include <string>#include <vector>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int x[200010];int main(){int n;cin>>n;for(int i=0;i<n;++i){cin>>x[i];x[i]+=i;}sort(x,x+n);for(int i=1;i<n;++i){if(x[i]==x[i-1]){cout<<":(";return 0;}}for(int i=0;i<n;++i)cout<<x[i]-i<<" ";return 0;}
- CF 549G swap(前-1,后+1),问是否能变成递增序列(灵活题)
- iPod Touch一代(1G)变成白苹果后的故障修复
- 叠罗汉1(最长递增子序列)
- poj2369问置换多少次变成有序序列
- CF 549H. Degenerate Matrix(二分,退化矩阵灵活题)
- nyoj-17 单调递增最长子序列(灵活应对)
- 数组最长连续递增(+1)子序列
- 算法原型1:最长递增子序列
- SAP发票校验前收货后是否能更改物料价格
- CF 573B(Bear and Blocks-匹配最长递增序列)
- 某二叉树的前序遍历序列为-+a*b-cd/ef,后序遍历序列为abcd-*+ef/-,问其中序遍历序列是
- PTA_7-1 Pop Sequence(25 分)_给指定的序列压栈,可以随时出栈,问你出栈顺序是否正确
- 判断一个序列是否是二叉查找树的后序、前序、中序遍历序列
- 灵活运用递增递减
- 由中序和后序(前序)序列求前序(后序)序列
- CF 549G排序,构造
- 数据结构:题目(3)测试一个数组序列是否是二叉树的前序遍历或者后序遍历结果
- 动态规划(1) 最长递增子序列 leetcode 300系列
- 4.17
- 2015062102 - 亲近自然
- 使用pptpd搭建VPN
- 旧手机是”古董“千万不要卖掉,自己一直保存在家里(安全第一)
- hashmap 和 concurrentHashMap
- CF 549G swap(前-1,后+1),问是否能变成递增序列(灵活题)
- ubuntu搭建配置架设VPN服务器PPTP(简单)
- Algorithms—202.Happy Number
- 第一个HTML5代码:HTML、HTML、CSS
- Linux学习日志--文件处理命令
- cocos2d-x核心类剖析-CCDirector类
- Uva - 10954 - Add All
- 分享45个android实例源码
- CoreSpotlight简单入门