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

输入示例

 

输出示例

 

数据规模及约定

见“输入

题解

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

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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
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 200010
int n, B[maxn];
priority_queue <int> 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;
}

[codeforces 549]G. Happy Line的更多相关文章

  1. CodeForces 794 G.Replace All

    CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...

  2. Codeforces 1207 G. Indie Album

    Codeforces 1207 G. Indie Album 解题思路 离线下来用SAM或者AC自动机就是一个单点加子树求和,套个树状数组就好了,因为这个题广义SAM不能存在 \(len[u] = l ...

  3. codeforces 659 G. Fence Divercity 组合数学 dp

    http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...

  4. Codeforces 803 G. Periodic RMQ Problem

    题目链接:http://codeforces.com/problemset/problem/803/G 大致就是线段树动态开节点. 然后考虑到如果一个点还没有出现过,那么这个点显然未被修改,就将这个点 ...

  5. Codeforces 954 G. Castle Defense

    http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...

  6. Codeforces 746 G. New Roads

    题目链接:http://codeforces.com/contest/746/problem/G mamaya,不知道YY了一个什么做法就这样过去了啊 2333 首先我显然可以随便构造出一棵树满足他所 ...

  7. Codeforces 915 G Coprime Arrays

    Discipntion Let's call an array a of size n coprime iff gcd(a1, a2, ..., an) = 1, where gcd is the g ...

  8. codeforces 589G G. Hiring(树状数组+二分)

    题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...

  9. Codeforces Gym100735 G.LCS Revised (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    G.LCS Revised   The longest common subsequence is a well known DP problem: given two strings A and B ...

随机推荐

  1. setTimeout 0秒

    我们通常知道常用setTimeout 0秒来解决动画或者一些效果的延迟问题:众所周知js是单线程,用0秒能把要执行的任务从队列中提出来.其实我也不太懂 有这个问题alert(1);setTimeout ...

  2. 前端开发之Chrome插件

    Postman - REST Client Postman是Ajax开发的神器,对于Restful开发方式特别有帮助,可以用来模拟各种请求来测试API的正确性,比如用来模拟Ajax请求.它还支持认证, ...

  3. 19.C#逐一介绍IEnumerable和IEnumerable<T>中的扩展方法(10.3-10.5)

    今天没有太多的言语,只有代码,扩展方法多得太多,不能一一列完,书中一些,看多了也就会使用了. //Enumerable.Range 返回起始到结束范围,是一个Enumrable<int>类 ...

  4. Foix_Reader_6.0|PDF阅读器

    福晰PDF阅读器,是阅读器中的精品.此版本是优化版本. 00:风格前卫 01:使用简洁 下载地址:        http://yunpan.cn/cHvyUfCdMKZz6  访问密码 ead7

  5. Tensorflow学习笔记(一):MNIST机器学习入门

    学习深度学习,首先从深度学习的入门MNIST入手.通过这个例子,了解Tensorflow的工作流程和机器学习的基本概念. 一  MNIST数据集 MNIST是入门级的计算机视觉数据集,包含了各种手写数 ...

  6. Missing message for key "err1" in bundle "(default bundle)" for locale zh_CN

    这个问题是: 你的使用了ApplicationResources_zh_CN.properties文件没有找到. 1.是struts-config.xml中的<message-resources ...

  7. poj2406 KMP

    kmp简单题 找循环节.由于KMP的next[]数组,所以可以考虑最后一组的情况,及next[n]的值:n-next[n]的值表示一个循环节. 如果n%(n-next[n])!=0表明该循环不成立.不 ...

  8. Java怎么实现多继承的功效

    Java不支持多继承,但是通过一些巧妙的设计来达到和多继承同样的效果  通过接口.内隐类,继承.实现,互相配合,达到多继承的效果  1.Java中一个类不能继承多个具体class.  2.一个类只可继 ...

  9. Java编程思想学习(十四) 枚举

    关键字enum可以将一组具名的值有限集合创建一种为新的类型,而这些具名的值可以作为常规的程序组件使用. 基本enum特性 调用enum的values()方法可以遍历enum实例,values()方法返 ...

  10. BZOJ-1879 Bill的挑战 状态压缩DP

    MD....怎么又是状压....... 1879: [Sdoi2009]Bill的挑战 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 537 Solved ...