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. MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器

    转:http://boke.25k5.com/kan141919.html 通过前面几篇,我们已经完成了内容类型,列表定义,列表实例g 8h"@的开发.本篇继续讲解列表中的一个重要环节- ...

  2. ADO.NET访问SQL Server调用存储过程带回参

    1,ADO.NET访问SQL Server调用存储过程带回参 2,DatabaseDesign  use northwind go --存储过程1 --插入一条商品 productname=芹菜 un ...

  3. [OFBiz]开发 五

    1.初学者例程:OFBiz Tutorial - A Beginners Development Guidehttps://cwiki.apache.org/confluence/display/OF ...

  4. NSarray 赋值 拷贝 等问题记录

    1. NSArray * a1 = @[@"1",@"2",@"3"]; NSArray * a2 = a1; a1跟a2所指向的地址是一样 ...

  5. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_label(self, locator)

    def get_selected_list_label(self, locator): """Returns the visible label of the selec ...

  6. oracleasm方式创建ASM

    1.准备oracleasm包 [root@localhost oracle]# uname -r 2.6.18-164.el5 [oracle@localhost ~]$ ls -l total 26 ...

  7. LeetCode题解——4Sum

    题目: 给定一个数组,找出其中和为0的所有4个数组合,每个组合内的4个数非递降. 解法: ①先排序,然后利用4个指针,前两个遍历,后两个在第二个指针之后的部分里夹逼,时间O(N3). ②或者利用一个哈 ...

  8. 【跟我一起学Python吧】python学习摘要

    开始靖,怒推一篇文章:码农平时应该注意的办公室礼节 学习一门脚本语言是很有必要的,当我们在开发一些简单,快速程序时,脚本语言便有了用武之地,而且还很給力.根据python的介绍,python是一门很简 ...

  9. Android Application Project 工程目录下各个文件的意思

    (1) src:源文件,主要是完成java代码的编写 (2) gen:ADT即系统自动生成的JAVA文件(即源代码目录),程序员千万不要去修改 (3) gen->[Package Name]-& ...

  10. JSP学习笔记(一)

    注释: 1.单行注释<!-- -->或者// <%@ page language="java" import="java.util.*" co ...