集训第四周(高效算法设计)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<tex2html_verbatim_mark> contracts for i<tex2html_verbatim_mark> -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<tex2html_verbatim_mark> contracts ( 1ai
i<tex2html_verbatim_mark> ) during i<tex2html_verbatim_mark> -th minute ( 1
i
n<tex2html_verbatim_mark> ), 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<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> when the first bank is buying and bi = - 1<tex2html_verbatim_mark> when the second one is buying (and the first one is selling), then the requirement for the trading session is that
aibi = 0<tex2html_verbatim_mark> . 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<tex2html_verbatim_mark> 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<tex2html_verbatim_mark>( 1n
100 000<tex2html_verbatim_mark> ). The second line of the input contains n<tex2html_verbatim_mark> integer numbers -- ai<tex2html_verbatim_mark> ( 1
ai
i<tex2html_verbatim_mark> ).
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<tex2html_verbatim_mark> numbers -- bi<tex2html_verbatim_mark> .
Sample Input
4
1 2 3 3
4
1 2 3 4
Sample Output
No
Yes
1 -1 -1 1 给你一个数组,选择其中的一半的数变成负数,然后这个数组的和为0
首先来判断一下NO的情况,如果这个数组的和是一个奇数的话,那么你无论怎么选都不会有结果,然后如果你数组长度为1,那么就没得选了。
YES该怎么做?先从大到小排序,然后一路加下去(使和大于数组和一半的不能加),如果刚好和等于数组和的一半,那么你选择的那些数就是应该乘以-1的数了
#include"iostream"
#include"algorithm"
using namespace std; const int maxn=100000+10; int f[maxn];
long long sum;
int n; struct node
{
int val,num;
}a[maxn]; bool cmp(struct node a1,struct node a2)
{
return a1.val>a2.val;
} bool Init()
{
sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i].val;
a[i].num=i;
f[i]=-1;
sum+=a[i].val;
}
if(sum%2) return false;
else return true;
} void Work()
{
sort(a,a+n,cmp);
long long cur=0;
for(int i=0;i<n;i++)
{
if(cur+a[i].val<=sum/2)
{
cur+=a[i].val;
f[a[i].num]=1;
if(cur==sum/2) break;
}
}
} void print()
{
for(int i=0;i<n-1;i++)
{
if(i!=0) cout<<' ';
cout<<f[i];
}
cout<<' '<<f[n-1]<<endl;
} int main()
{
while(cin>>n)
{
if(!Init()||n==1) cout<<"No"<<endl;
else
{
cout<<"Yes"<<endl;
Work();
print();
}
}
return 0;
}
集训第四周(高效算法设计)H题 (贪心)的更多相关文章
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- 集训第四周(高效算法设计)A题 Ultra-QuickSort
原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...
- 集训第四周(高效算法设计)P题 (构造题)
Description There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...
- 集训第四周(高效算法设计)O题 (构造题)
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...
- 集训第四周(高效算法设计)N题 (二分查找优化题)
原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...
- 集训第四周(高效算法设计)M题 (扫描法)
原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...
- 集训第四周(高效算法设计)K题 (滑窗问题)
UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include& ...
- 集训第四周(高效算法设计)I题 (贪心)
Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...
随机推荐
- Luogu P1122最大子树和 【水树形Dp】By cellur925
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- USACO Training刷题记录 By cellur925
Section 1.1 Your Ride Is Here 貌似没啥可说 Greedy Gift Givers 上来就想stl map映射,有两个坑:如果送给别人的人数为0,那么需要特判一下,防止整数 ...
- python服务之flask
前言: 关于python flask 的介绍.指导.案例,网络上比比皆是.这里参考官网:http://www.pythondoc.com/flask/index.html 你可能不知道的flask服务 ...
- java 学习书籍
<Effective java> <深入理解java虚拟机> <Java并发编程实践> <Java Performance> <java解惑> ...
- [SPOJ375]Qtree
Description You are given a tree (an acyclic undirected connected graph) with N nodes, and edges num ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)
Description 上下有两个长度为n.位置对应的序列A.B, 其中数的范围均为1~n.若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边. 现要求在边与边不相交的情 ...
- poj 1511 Invitation Cards spfa 邻接矩阵
题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...
- 工厂方法模式及php实现
工厂方法模式: 工厂方法模式(Factory Method Pattern)又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Polymorphic Facto ...
- bootstrap基本布局
中文API bootstrap.cn HTML5文档 类型 移动设备优先 width 属性控制设备的宽度.设置为 device-width 确保它能正确呈现在不同设备上. initial-sc ...
- orcale 数据库的一些知识
最近学了一些Oracle数据库的知识,我想自己整理一下,以后也方便自己查阅的. orcale 数据库登录(tiger) 1. sql plus 登录 用户名: sys 口令: 主机字符串:orcl a ...