UVA1614(贪心)

Hell on the Markets

Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu

[Submit]  [Go Back]  [Status]

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 ( 1aii
) during i -th minute ( 1in
), 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
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 input file contains several test cases, each of them as described below. The first line of the input contains the single integer number
n ( 1n100 000
). The second line of the input contains n integer numbers --
ai ( 1aii
).

Output

For each test case, the first line of the output must contain `` Yes‘‘ if the trading session with specified volumes is possible and ``
No‘‘ otherwise. In the former option a second line must contain
n numbers -- bi .

Sample Input

4
1 2 3 3
4
1 2 3 4

Sample Output

No
Yes
1 -1 -1 1

Source

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 8. Algorithm Design ::
Exercises

[Submit]  [Go Back]  [Status]

排序之后贪心瞎搞。。。

/*************************************************************************
    > File Name: c.cpp
    > Author: acvcla
    > QQ:
    > Mail: [email protected]
    > Created Time: 2014年10月11日 星期六 08时42分28秒
 ************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<cstdlib>
#include<ctime>
#include<set>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int A[maxn],cnt[maxn],f[maxn];
std::vector<int> v;
int main(){
	int n;
	while(~scanf("%d",&n)&&n){
		memset(cnt,0,sizeof(cnt));
		memset(f,0,sizeof f);
		v.clear();
		LL sum=0;
		LL M=0;
		for(int i=1;i<=n;i++){
			scanf("%d",A+i);
			if(A[i]>M)M=A[i];
			sum+=A[i];
			cnt[A[i]]++;
		}
		if(sum&1){
			puts("No");
			continue;
		}
		sum/=2;
		bool ok=false;
		for(int i=M;!ok&&i>=1;i--){
			f[i]=min((LL)cnt[i],sum/i);
			sum-=(LL)f[i]*i;
			if(sum==0){
				ok=true;
				break;
			}
		}
		if(!ok){
			puts("No");
		}else{
			puts("Yes");
			for(int i=1;i<=n;i++){
				if(i==1){
					if(f[A[i]]>0){
						printf("1");
						f[A[i]]--;
					}
					else printf("-1");
				}
				else{
					if(f[A[i]]>0){
						printf(" 1");
						f[A[i]]--;
					}
					else printf(" -1");
				}
			}
			puts("");
		}

	}
	return 0;
}
时间: 2024-10-06 23:53:22

UVA1614(贪心)的相关文章

UVa1614 Hell on the Markets (贪心,结论)

链接:http://bak.vjudge.net/problem/UVA-1614 分析:http://blog.csdn.net/wcr1996/article/details/43957461 有了这个结论,接下来就是贪心搞一搞了. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 100000 + 5

UVA-1614 Hell on the Markets(贪心+推理) (有待补充)

题目大意:一个整数序列a,1≤a[i]≤i.问能否通过在一些元素前加上负号,使得整个序列和为0. 题目分析:贪心.贪心策略:每次都先选最大的元素加负号(或保留,不加负号). 贪心依据:对于1≤a[i]≤i,1~sum[i]总能表示出来.   贪心依据证明:用数学归纳法证明,当i=1时,显然成立.假设当i=k时,也成立.当i=k+1时,(先证明到这儿,有空再补). 代码如下: # include<iostream> # include<cstdio> # include<cst

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

CoderForce 140C-New Year Snowmen(贪心)

题目大意:有n个已知半径的雪球.堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人? 题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多. 代码如下: # include<iostream> # include<map> # include<vector> # include<cstdio> # include<queue> # include<algorithm>

计蒜客 跳跃游戏(贪心)

给定一个非负整数数组,假定你的初始位置为数组第一个下标.数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如: A = [2,3,1,1,4], return ture A = [3,2,1,0,4], return false. 格式: 第一行输入一个正整数n,接下来的一行,输入数组A[n].如果能跳到最后一个下标,输出"true",否则输出"false" 样例1 ????输入:???? ????????5 ???

BZOJ 2525 [Poi2011]Dynamite 二分+树形贪心

题意: n个点,一棵树,有些点是关键点,可以将m个点染色. 求所有关键点到最近的被染色点的距离的最大值最小. 解析: 反正从这道题我是学了一种做题思路? 大爷讲课的时候说的:一般选择某些点的代价相同的话都是贪心,代价不同的话一般都是DP. 想想也挺对的,不过就是没有感悟到过? 反正这题考试的时候我是直接D了贪心的- -! 忘了为啥D了. 显然最大值最小我们需要二分一下这个值. 然后接下来我们从下往上扫整棵树. 节点的状态有几个? 第一种是 子树内没有不被覆盖的关键点,并且子树中有一个节点的贡献可