[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. asp.net网站安全常见问题与防范

    1:SQL 注入 2:XSS 3:CSRF 4:文件上传 1:SQL 注入 引起原因: 其实现在很多网站中都存在这种问题.就是程序中直接进行SQL语句拼接.可能有些读者不太明白. 下面通过一个登录时对 ...

  2. android之外部文件存储和读取

    这次借用上次读写内部存储的代码,只是对将更换文件的读写路径即可.这里需要对获取SDcard的读写权限. 一.AndroidManifest.xml 这里增加了对外部存储设备的读写权限 <?xml ...

  3. 项目笔记---C#异步Socket示例

    概要 在C#领域或者说.net通信领域中有着众多的解决方案,WCF,HttpRequest,WebAPI,Remoting,socket等技术.这些技术都有着自己擅长的领域,或者被合并或者仍然应用于某 ...

  4. BZOJ1452 [JSOI2009]Count

    Description Input Output Sample Input Sample Output 1 2 HINT 正解:二维树状数组 解题报告: 这是一道送肉题.二维树状数组直接维护每种颜色的 ...

  5. JSP登录验证并显示信息

    加入C标签: 加入jstl.jar 和standard.jar加入Lib文件夹中 将c.tld放入WEB-Info文件夹中 index.jsp <%@ page language="j ...

  6. Ubuntu添加开机自动启动程序的方法

    文章出处:http://hi.baidu.com/gcc_gun/blog/item/fe9bbc4b84e911fa83025cb8.html 1. 开机启动时自动运行程序 Linux加载后, 它将 ...

  7. SOAP 格式设置选项

    SOAP 格式设置选项 两个格式设置选项为: Style:适用于 SOAP 消息中 Body 元素的子元素(也可能是孙级).此选项指定为 binding WSDL 元素(通常情况下)或 operati ...

  8. NSKeyedArchiver 类 格式

    结论:NSKeyedArchiver 以健值对+类名的形式保存数据: NSDictionary *tDic = @{@"kxxxx":@"hhhh",@&quo ...

  9. C#实现自动升级(附源码)

    http://blog.csdn.net/zhuweisky/article/details/50439386 OAUS

  10. xml 嵌入式资源

    使用Ibatis总是说未能加载相应的sqlmap.xml,原来是 xml以内容方式,而不是嵌入式方式载入Dll中