Problem H. Hell on the Markets
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86821#problem/H

Description

Most financial institutions had become insolvent during financial crisis and went bankrupt or were bought by larger institutions, usually by banks. By the end of financial crisis of all the financial institutions only two banks still continue to operate. Financial markets had remained closed throughout the crisis and now regulators are gradually opening them. To prevent speculation and to gradually ramp up trading they will initially allow trading in only one financial instrument and the volume of trading will be limited to i contracts for i-th minute of market operation. Two banks had decided to cooperate with the government to kick-start the market operation. The boards of directors had agreed on trading volume for each minute of this first trading session. One bank will be buying ai contracts (1 ≤ ai ≤ i) during i-th minute (1 ≤ i ≤ n), while the other one will be selling. They do not really care whether to buy or to sell, and the outside observer will only see the volume ai of contracts traded per minute. However, they do not want to take any extra risk and want to have no position in the contract by the end of the trading session. Thus, if we define bi = 1 when the first bank is buying and bi = −1 when the second one is buying (and the first one is selling), then the requirement for the trading session is that Pn i=1 aibi = 0. Your lucky team of three still works in the data center (due to the crisis, banks now share the data center and its personnel) and your task is to find such bi or to report that this is impossible.

Input

The first line of the input file contains the single integer number n (1 ≤ n ≤ 100 000). The second line of the input file contains n integer numbers — ai (1 ≤ ai ≤ i).

Output

The first line of the output file must contain “Yes” if the trading session with specified volumes is possible and “No” otherwise. In the former case the second line must contain n numbers — bi

Sample Input

4
1 2 3 4

Sample Output

Yes
1 -1 -1 1

HINT

题意

给你n个数,让你构造一个bi,bi的取值不是1

然后满足ai*bi的累加等于0

题解

注意题意,a[i]<=i,所以先排一个序,然后就可以贪心弄了

每一个数不是属于第一组,就是属于第二组

然后貌似就可以贪心了?反正这个贪心是猜的……

测了几组数据,发现是对的

群里的老司机给了一个证明:

对于1≤ai≤i+,前面ai的数一定可以表示出1~sum[i]中的任意一个数.

对于i=1显然成立,

假设对于i=k结论成立,那么对于i=k+1来说,只要证明sum[k]+i,≤i≤ak+1可以凑出来就行了。

因为sum[k]+i≥k+,且1≤ak+≤k+,所以可以先选一个ak+,剩下的0≤sum[k]+i-ak+≤sum[k]一定是可以由前面的数字凑出来的。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = 1e5 + ;
int n;
typedef struct data
{
int val;
int idx;
friend bool operator < (const data & x,const data & y)
{
return x.val < y.val;
}
}; data A[maxn];
long long sum = ;
long long L = , R = ;
int ans[maxn]; int main(int argc,char *argv[])
{
freopen("hell.in","r",stdin);
freopen("hell.out","w",stdout);
scanf("%d",&n);
for(int i = ; i < n ; ++ i)
{
scanf("%d",&A[i].val);
A[i].idx = i;
sum += A[i].val;
}
sort(A,A+n);
if (sum & ) printf("No\n");
else
{
for(int i = n- ; i >= ; -- i)
{
if (L < R)
{
L += A[i].val;
ans[A[i].idx] = -;
}
else
{
R += A[i].val;
ans[A[i].idx] = ;
}
}
if (L == R)
{
printf("Yes\n");
printf("%d",ans[]);
for(int i = ; i < n ; ++ i) printf(" %d",ans[i]);
printf("\n");
}
else
printf("No\n");
}
return ;
}

Codeforces Gym H. Hell on the Markets 贪心的更多相关文章

  1. codeforces gym 100286 H - Hell on the Markets (贪心算法)

    题目链接 题意:n个数分别为a[i],问是否存在一组对应的b[i],b[i]=1 || b[i]=-1,使得ai*bi的n项和为0. 题解: 先证明一个结论吧,对于1≤ai≤i+1,前面ai个数一定可 ...

  2. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  3. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

  4. codeforces Gym 100286H Hell on the Markets

    紫书上面的题,队友做的,WA了freopen..爆了int... UVA 1614 - Hell on the Markets 奇怪的股市(贪心,结论)

  5. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  6. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  7. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

随机推荐

  1. java web 学习十一(使用cookie进行会话管理)

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  2. TortoiseHg简单的入门使用说明

    参考资料: 互普的 TortoiseHg使用说明_百度文库 Mercurial(Hg)基本操作 - Tim Gong - 博客园 Mercurial与TortoiseHg使用入门教程(转) - mee ...

  3. ASP.NET常用技巧方法代码断

    1. 打开新的窗口并传送参数:传送参数:response.write("<script>window.open('*.aspx?id="+this.DropDownLi ...

  4. Python常用模块的安装方法

    http://blog.163.com/yang_jianli/blog/static/161990006201162152724339/

  5. CSS:7个你可能不认识的单位

    原文:7 CSS Units You Might Not Know About 众所周知,当使用CSS技术的时候,很容被一些奇异问题给困住.而当我们面对新的问题时,这会让我们处于非常不利的位置. 但是 ...

  6. C++容器学习

    以前自学C++的时候就没怎么看容器,一直以来也没怎么编过C++程序,现在想用C++写点东西,突感容器类型有些生疏,故做此笔记.(参考<C++ primer> 容器:容纳特定类型对象的集合. ...

  7. [HIve - LanguageManual] Hive Operators and User-Defined Functions (UDFs)

    Hive Operators and User-Defined Functions (UDFs) Hive Operators and User-Defined Functions (UDFs) Bu ...

  8. [Hive - LanguageManual] Create/Drop/Alter -View、 Index 、 Function

    Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version info ...

  9. 《Genesis-3D开源游戏引擎-官方录制系列视频教程:基础操作篇》

    注:本系列教程仅针对引擎编辑器:v1.2.2及以下版本 G3D基础操作   第一课<G3D编辑器初探> G3D编辑器介绍,依托于一个复杂场景,讲解了场景视图及其基本操作,属性面板和工具栏的 ...

  10. 【hadoop代码笔记】Hadoop作业提交中EagerTaskInitializationListener的作用

    在整理FairScheduler实现的task调度逻辑时,注意到EagerTaskInitializationListener类.差不多应该是job提交相关的逻辑代码中最简单清楚的一个了. todo: ...