Codeforces 549G Happy Line[问题转换 sort]
2016-09-16 16:53
330 查看
G. Happy Line
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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.
Input
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.
Output
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.
Examples
input
output
input
output
input
output
Note
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.
官方题解:
G. Happy Line
Authors: 2222, MrDindows
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)).
很类似于371E中让严格递减变为不严格的做法
考虑位于i位置上的元素,要想让他和最后一个元素在同一地位上比较,只需要-(n-i),然后排序
排序后每个位置元素的实际值是+n-i
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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.
Input
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.
Output
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.
Examples
input
2 11 8
output
9 10
input
5 10 9 7 10 6
output
:(
input
3 12 3 3
output
4 4 10
Note
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.
官方题解:
G. Happy Line
Authors: 2222, MrDindows
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)).
很类似于371E中让严格递减变为不严格的做法
考虑位于i位置上的元素,要想让他和最后一个元素在同一地位上比较,只需要-(n-i),然后排序
排序后每个位置元素的实际值是+n-i
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2e5+5,INF=1e9+5; int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } int n,a ; int main(){ n=read(); for(int i=1;i<=n;i++) a[i]=read()-(n-i); sort(a+1,a+1+n); for(int i=1;i<n;i++) if(a[i]+n-i>a[i+1]+n-i-1){printf(":(");return 0;} for(int i=1;i<=n;i++) printf("%d ",a[i]+n-i); }
相关文章推荐
- iOS中delegate、NSNotification和KVO的区别
- android自定义view 模仿win10进度条
- Android & iOS设计尺寸规范
- Genymotion终于复活了
- iOS内存管理之NSAutoreleasePool
- ios Tableview行高的计算
- Android EventBus使用详解
- 初探Object
- 零java基础搞定微信Server_7_微信公众号的自定义菜单
- 零java基础搞定微信Server_6_微信公众号文本消息处理
- Intellij Idea 开发android应用时,获取签名SHA1值
- 零java基础搞定微信Server_5_启用微信公众号开发模式
- 零java基础搞定微信Server_4_写一个servlet的DEMO
- 零java基础搞定微信Server_3_搭建微信Server本地开发环境
- 零java基础搞定微信Server_2_Servlet基础
- 零java基础搞定微信Server_1_XML基础
- Android服务器端开发
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'解决记录
- Android权限uses-permission
- android studio 导入、移除module、以及加载module中的jar包