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 ( 1aii<tex2html_verbatim_mark> ) during i<tex2html_verbatim_mark> -th minute ( 1in<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> ( 1n100 000<tex2html_verbatim_mark> ). The second line of the input contains n<tex2html_verbatim_mark> integer numbers -- ai<tex2html_verbatim_mark> ( 1aii<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的更多相关文章

  1. 高效算法——Bin Packing F - 贪心

      Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Descripti ...

  2. 集训第四周(高效算法设计)H题 (贪心)

    Description   Most financial institutions had become insolvent during financial crisis and went bank ...

  3. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  4. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

  5. Java 算法(一)贪心算法

    Java 算法(一)贪心算法 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 一.贪心算法 什么是贪心算法?是指在对问题进行求 ...

  6. python常用算法(6)——贪心算法,欧几里得算法

    1,贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的的时在某种意义上的局部最优解. 贪心算法并不保证会得到最优解,但 ...

  7. CVPR2020论文介绍: 3D 目标检测高效算法

    CVPR2020论文介绍: 3D 目标检测高效算法 CVPR 2020: Structure Aware Single-Stage 3D Object Detection from Point Clo ...

  8. 高效算法——G - 贪心

    G - 贪心 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Desc ...

  9. 高效算法——E - 贪心-- 区间覆盖

    E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...

随机推荐

  1. hadoop之wordCount程序理解

    有篇文章http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html中介绍的

  2. Bash常用快捷键

    快捷键 作用 Ctrl+A 把光标移动到命令行开头,如果我们输入的命令过长,想要把光标移动到命令行开头时使用 Ctrl+E 把光标移动到命令行结尾 Ctrl+C 强制终止当前的命令 Ctrl+L 清屏 ...

  3. 完全卸载oracle

    今天在网上看到有位网友写的篇日志,感觉蛮好的,一般卸载oracle有4个地方需求注意:1)Services,2)software,3eventlog,4)path. 1.关闭 oracle 所有的服务 ...

  4. godaddy_关于产品退款

    You're chatting with Danny.Danny - Thank you for contacting live chat. My name is Danny. How can I a ...

  5. 在oracle中怎么把一张表的数据插入到另一张表中

    把table2表的数据插入到table1中 insert   into   table1   select   *   from   table2

  6. 对 Xcode 菜单选项的详细探索(转)

    转自 http://www.cnblogs.com/dsxniubility/p/4983614.html 本文调研Xcode的版本是 7.1,基本是探索了菜单的每一个按钮.虽然从xcode4一直用到 ...

  7. UITapGestureRecognizer会屏蔽掉Button的点击事件( 转载)

    UITapGestureRecognis 前几天在做项目的时候,遇到这个一个问题,在一个视图也就是UIView上添加一个手势,然后又在这个View上添加一个UIButton,然后给按钮添加事件,运行项 ...

  8. mahout的安装、配置及运行java程序

    一.下载安装包: http://mahout.apache.org/general/downloads.html 二.解压: 将下载的安装包解压到需要的目录下 三.配置环境变量: export MAH ...

  9. SGU 150.Mr. Beetle II

    非常烦人的题,思路比较简单,十分容易出错,细节非常重要. 从四个不同的行走方向讨论经过的每一个格子. code: #include <iostream> #include <util ...

  10. MAC 开发工具

    web开发编辑器 Espresso下载地址   密码: i9hr