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. ios第三方开源库

    1.AFNetworking 目前比较推荐的iOS网络请求组件,默认网络请求是异步,通过block回调的方式对返回数据进行处理. 2.FMDB 对sqlite数据库操作进行了封装,demo也比较简单. ...

  2. html5极速3D立体式图片相册切换效果

    下载Demo  

  3. java AES加密算法

    package com.siro.tools; import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import j ...

  4. 11g 重建EM 报ORA-20001: SYSMAN already exists

    今天在安装11g(11.1.0.7.0)数据库之后,通过emca -config dbcontrol db -repos create 命令手工创建em的时候报错,查看日志后发现有以下错误 CONFI ...

  5. Android常用控件之FragmentTabHost的使用

    最近在学TabHost时发现TabActivity在API level 13以后不用了,所以就去寻找它的替换类,找到FragmentActivity,可以把每个Fragment作为子tab添加到Fra ...

  6. android studio SDK版本的调节

    android { compileSdkVersion 17 buildToolsVersion "17.0.0" defaultConfig { minSdkVersion 17 ...

  7. cocos2d anchor point 锚点解析

    anchor point 究竟是怎么回事? 之所以造成不容易理解的是因为我们平时看待一个图片是 以图片的中心点 这一个维度来决定图片的位置的.而在cocos2d中决定一个 图片的位置是由两个维度 一个 ...

  8. openfl关于windows平台编译报错解决办法

    报错信息:  无法打开程序数据库“e:\newproj\mainclient\bin\windows\cpp\obj\obj\msvc-debug-ncxp\vc.pdb”:如果要将多个 CL.EXE ...

  9. asp.net MVC 安全性[笔记]

    1. 跨站脚本(XSS) 1.1 介绍 1.1.1 被动注入,利用输入html,javascript 等信息伪造链接,图片等使用提交信息,调转页面等 1.1.2 主动注入,黑客主动参与攻击,不会傻等倒 ...

  10. 杂谈:HTML 5页面可视性API

    译文来源:http://www.ido321.com/1126.html 原文:HTML5 Page Visibility API 译文:HTML 5的页面可视性API 译者:dwqs 在早期,浏览器 ...