Codeforces 549G. Happy Line 贪心
来源:互联网 发布:移动机顶盒软件下载 编辑:程序博客网 时间:2024/05/21 08:46
很有意思的贪心:
Let's reformulate the condition in terms of a certain height the towers, which will be on the stairs. Then an appropriate amount of money of a person in the queue is equal to the height of the tower with the height of the step at which the tower stands. And the process of moving in the queue will be equivalent to raising a tower on the top step, and the one in whose place it came up — down. As shown in the illustrations. Then, it becomes apparent that to make all of the tower on the steps to be sorted, it is enough to sort the tower without the height of step it stays. Total complexity of sorting is O(nlog(n)).
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 a stands just behind person b, then person a can pay person b 1 dollar, then a and b get swapped. Of course, if persona 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 thei-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.
/* ***********************************************Author :CKbossCreated Time :2015年06月09日 星期二 00时24分13秒File Name :CF549.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int maxn=200200;int n,a[maxn],base[maxn],b[maxn];int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",a+i);base[i]=n-1-i;b[i]=a[i]-base[i];}sort(b,b+n);bool flag=true;for(int i=0;i<n;i++){a[i]=base[i]+b[i];if(i&&a[i]<a[i-1]){flag=false; break;}}if(flag==false){puts(":(");}else{for(int i=0;i<n;i++)printf("%d%c",a[i],(i==n-1)?10:32);} return 0;}
- codeforces 549G G. Happy Line(贪心)
- Codeforces 549G. Happy Line 贪心
- Codeforces 549G Happy Line [贪心] [数学]
- 【Codeforces 549G】【贪心】Happy Line
- 贪心——Happy Line (CodeForces 549G)
- codeforces #549G Happy Line
- Looksery Cup 2015 G. Happy Line
- Looksery Cup 2015-G. Happy Line
- Line CodeForces
- TOJ 4284 Happy watering / 贪心
- G - Happy 2004------(HDU 1452)
- POJ3617Best Cow Line(贪心)
- [贪心]codeforces
- CodeForces 7C - Line
- CodeForces 278ACircle Line
- codeforces 7C Line
- Codeforces 549B Looksery Party [贪心]
- codeforces 888G.Xor-MST(01字典树+贪心+最小异或生成树)
- 如何设置微信订阅号的微信号以及申请微信认证
- win8中如何设定editplus为txt默认打开程序
- 减少linux服务器大量TIME_WAIT
- svn:ignore的使用
- LeetCode | Merge Sorted Array
- Codeforces 549G. Happy Line 贪心
- C++——试题(一般赋值语句的概念和方法)
- 类型别名
- Python pip install 出现UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 34:错误的解决方法
- UIButton 设置圆角 边框颜色 点击回调方法
- ViewPager做的引导界面Demo
- Android的Activity屏幕切换动画(一)-左右滑动切换
- 在java中 i++和++i有什么区别
- [5]_技术那么多,我该如何选择