高效算法——Most financial institutions 贪心 H
H - 贪心
Crawling in process... Crawling failed Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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
解题思路:
这个题目的题思就是输入n个数,判断这n个数是否能分成总和的一半并相等,我们可以定义一个类或者结构体数组存储,并定义两个成员x,y,x用来存放输入的数,y用来记录这个数的位置,因为之后我们要对
x进行排序,再定义一个整形数组,用来记录这个数乘-1还是1,最后要控制好循环条件,当所有数的和为奇数的时候,直接输出No,或者个数为1也直接输出No,否则输出Yes,并输出方案,即b数组
程序代码:
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX=;
long long sum;
int n,i;
int b[MAX];
struct node
{
int x,y; }a[MAX];
bool cmp(node a,node b)
{
return a.x>b.x;
}
bool init()
{
sum=;
for(int i=;i<n;i++)
{
scanf("%d",&a[i].x);
a[i].y=i;
b[i]=-;
sum+=a[i].x;
}
if(sum%) return true;
else return false;
}
void solve()
{
sort(a,a+n,cmp);
long long num=;
for( i=;i<n;i++)
{
if(num+a[i].x<=sum/)
{
num+=a[i].x;
b[a[i].y]=;
if(num==sum/) break;
}
}
}
void print()
{ for( i=;i<n-;i++)
{
printf("%d ",b[i]);
}
printf("%d\n",b[n-]);
}
int main()
{
while( scanf("%d",&n)==)
{
if(init()||n==) printf("No\n");
else
{
printf("Yes\n");
solve();
print();
}
}
return ;
}
---恢复内容结束---
高效算法——Most financial institutions 贪心 H的更多相关文章
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- 集训第四周(高效算法设计)H题 (贪心)
Description Most financial institutions had become insolvent during financial crisis and went bank ...
- 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- Java 算法(一)贪心算法
Java 算法(一)贪心算法 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 一.贪心算法 什么是贪心算法?是指在对问题进行求 ...
- python常用算法(6)——贪心算法,欧几里得算法
1,贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的的时在某种意义上的局部最优解. 贪心算法并不保证会得到最优解,但 ...
- CVPR2020论文介绍: 3D 目标检测高效算法
CVPR2020论文介绍: 3D 目标检测高效算法 CVPR 2020: Structure Aware Single-Stage 3D Object Detection from Point Clo ...
- 高效算法——G - 贪心
G - 贪心 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Desc ...
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
随机推荐
- js substr和substring字符串截取
substr(start,length)第一个参数是开始位置(注:start的开始是从0开始,看到好多博客上面是从1开始,在火狐和谷歌执行了一下是从0开始),第二个参数是截取字符串的长度(可以省略,表 ...
- Android Service初步学习的笔记
1.Service的应用场景 条件:a.并不依赖于用户可视化界面(不是绝对的,如前台service就是与notification界面结合使用的) b.具有较长时间的运行特性. service的应用 ...
- C#中邮件的发送基本操作
本地配置的邮箱:http://localhost:6080/index.php //邮件的收发需要用到两个类 //1.用来创建一封邮件对象 //1.MailMessage 添加对 usin ...
- express不是内部命令解决办法
安装nodejs后,安装express,cmd下命令:npm install express -g;之后敲入express -V会提示不是内部命令,是因为, 4.0版本中将命令工具分家出来了,所以我们 ...
- 受限玻尔兹曼机(RBM)
能量模型 RBM用到了能量模型. 简单的概括一下能量模型.假设一个孤立系统(总能量$E$一定,粒子个数$N$一定),温度恒定为1,每个粒子有$m$个可能的状态,每个状态对应一个能量$e_i$.那么,在 ...
- iOS socket编程
// // ViewController.m // socket // // Created by emerys on 16/3/2. // Copyright © 2016年 Emerys. All ...
- 继承语法含有main()方法
package me.ybleeho; class Cleanser{ //清洁剂 private String s="Cleanser"; public void append( ...
- Java线程(学习整理)--2---加入另一个线程join
1.join简介: 今天刚学的,这里我简单总结一下,join本身就是“加入”的意思,那么在线程中是什么意思呢?是在一个线程的run方法执行过程中,当特殊情况下需要执行一些其他的操作的时候,我们会用到j ...
- throw 导致 Error C2220, wraning C4702错误
今天在程序加了一个语句,发现报 Error C2220, Wraning C4702错误 查询Wraning C4702 ,[无法访问的代码] 由于为 Visual Studio .NET 2003 ...
- 【BZOJ2038】【莫队】小z的袜子
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...