Codeforces 549G. Happy Line 贪心

很有意思的贪心:

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)).

 

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 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.

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 thei-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.

Sample test(s)

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.

/* ***********************************************
Author        :CKboss
Created 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;
}
时间: 2024-10-16 20:20:25

Codeforces 549G. Happy Line 贪心的相关文章

CodeForces 549G Happy Line

Happy Line Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 549G Description Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this

Codeforces 549G Happy Line[问题转换 sort]

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 larg

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

POJ3617 Best Cow Line 贪心

这题虽然简单但是挺不错的,因为过程很好,比较开发思维 和鼓励人,不像有些贪心太偏不好推反而让人厌烦 给出长度为N的字符串S,然后还有一个空串STR,每次有两个选择 1:删除S的头元素假加入STR中      2:删除S的尾元素加入STR中 是的STR字典序最小 并输出 开始可能没有什么顾虑的去想 每次比较S的头和尾元素 取小的那个删除并假如STR中,但是若S的头和尾元素一样的话这个方法就不行了,因为先取头或者尾还得看他们之间的元素,这时候是倒着来还是顺着好呢?那就直接拿顺的跟倒的进行字典序的大小

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

[Luogu2870] [USACO07DEC]最佳牛线Best Cow Line(贪心+后缀数组)

[Luogu2870] [USACO07DEC]最佳牛线Best Cow Line(贪心+后缀数组) 题面 FJ打算带他的\(N(1 \leq N \leq 30,000)\)头奶牛去参加一年一度的"全美农场主大奖赛".在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席前依次走过. 今年,竞赛委员会在接受队伍报名时,采用了一种新的登记规则:他们把所有队伍中奶牛名字的首字母取出,按它们对应奶牛在队伍中的次序排成一列(比如说,如果FJ带去的奶牛依次为Bessie.Sylv

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about

Codeforces 700B Connecting Universities - 贪心

Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recently, the president signe

CodeForces 215D Hot Days(贪心)

题目链接:http://codeforces.com/problemset/problem/215/D Description The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1?≤?i?≤?n) region has a