博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[codeforces 549]G. Happy Line
阅读量:5039 次
发布时间:2019-06-12

本文共 2902 字,大约阅读时间需要 9 分钟。

[codeforces 549]G. Happy Line

试题描述

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.

输入

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

数据规模及约定

见“输入

题解

考虑每个人在这个队列中的每个位置时的钱数,那么对于原队列第 i 个人,当他跑到队列的 j 位置时,他的钱数变成 ai - j + i。现在我们从最后一个位置开始考虑,我们找到所有人到这个位置时最大的钱数(若有多个最大钱数相等,任取一个),把它作为答案序列的最后一个元素,然后再往前移动,以此类推。我们观察从位置 i 转移到位置 i-1,所有人在当前位置的钱数会加 1,所以他们的相对大小关系是不会改变的。

按照 ai - n + i 从大到小排序,从位置 n 开始处理,每次选择一个最大的作为当前位置的答案,然后整个序列加 1,当前位置左移 1 位。注意判断 小于 0 和 前面的数小于后面的数 这两种不合法的情况。

#include 
#include
#include
#include
#include
#include
using namespace std;int read() { int x = 0, f = 1; char c = getchar(); while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); } while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); } return x * f;}#define maxn 200010int n, B[maxn];priority_queue
Q;int main() { n = read(); for(int i = 1; i <= n; i++) Q.push(read() - n + i); for(int i = n; i; i--) { B[i] = Q.top() + n - i; Q.pop(); if((i < n && B[i] > B[i+1]) || B[i] < 0) return puts(":("), 0; } for(int i = 1; i < n; i++) printf("%d ", B[i]); printf("%d\n", B[n]); return 0;}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5810516.html

你可能感兴趣的文章
浅谈 @RequestParam 和@PathVariable
查看>>
NSEnumerator用法小结
查看>>
redhat 7 源码安装 mysql5.5.49
查看>>
Android官方技术文档翻译——ApplicationId 与 PackageName
查看>>
Feign使用Hystrix无效原因及解决方法
查看>>
Sam做题记录
查看>>
hexo 搭建博客
查看>>
建造者模式(屌丝专用)
查看>>
Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
查看>>
C++的引用
查看>>
python itertools
查看>>
http://lorempixel.com/ 可以快速产生假图
查看>>
编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成...
查看>>
文件操作
查看>>
NYOJ-613//HDU-1176-免费馅饼,数字三角形的兄弟~~
查看>>
graphite custom functions
查看>>
ssh无密码登陆屌丝指南
查看>>
一个自己写的判断2个相同对象的属性值差异的工具类
查看>>
[CF803C] Maximal GCD(gcd,贪心,构造)
查看>>
oracle连接的三个配置文件(转)
查看>>