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 给你一个数组,选择其中的一半的数变成负数,然后这个数组的和为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题 (贪心)的更多相关文章

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

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

  2. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

  3. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  4. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  5. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  6. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  7. 集训第四周(高效算法设计)K题 (滑窗问题)

    UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include& ...

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

    Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...

  9. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

随机推荐

  1. 使用Quartz实现定时作业

    该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. Quartz是一个开源的作业调度框架,准确的称谓应该是 Quartz.Net,它是Ja ...

  2. solr的安装配置与helloworld

    一.安装solr 1.安装jdk环境和tomcat 2.解压solr压缩包,这里我解压到opt目录下 3.把/usr/local/solr-4.8.0/dist/solr-4.8.0.war部署到to ...

  3. IDEA远程调试Tomcat程序

    如何使用 Idea 远程调试 Java 代码 IDEA远程调试的 基本就是在服务端先设置Tomcat服务器启动脚本catalina.bat,然后在客户端IDEA上进行参数配置,最后二者可以通过Sock ...

  4. RMAN-06564错误的原因及解决办法

    今日在进行数据库恢复时,遭遇RMAN-06564错误,如下: RMAN> restore spfile from autobackup; Starting restore at 01-NOV-1 ...

  5. [转]Azure 表存储和 Windows Azure SQL Database - 比较与对照

    本文转自:https://msdn.microsoft.com/library/azure/jj553018 更新时间: 2014年10月 作者:Valery Mizonov 和 Seth Manhe ...

  6. Spring需要的几个关键配置文件(SSM框架整合)

    打包下载 springmvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...

  7. 学习笔记 第十五章 JavaScript基础

    第15章   JavaScript基础 [学习重点] 了解JavaScript基础知识 熟悉常量和变量 能够使用表达式和运算符 正确使用语句 能够掌握数据类型和转换的基本方法 正确使用函数.对象.数组 ...

  8. rest_framework基于generics.CreateAPIView创建用户

    最近在写新版的devops3.0,被generics.CreateAPIView创建用户密码序列化的问题折磨的欲仙欲死.反复看源码测试,得出下面的流程,这也是做generics.CreateAPIVi ...

  9. javaEE web 系统安装时自定义初始化

    通常JavaWeb项目在第一次启动时我们需要做一些初始化工作,比如:初始化一个管理员的登录账户和密码,配置缓存.定时任务等,这些操作可以通过手工修改数据库完成,但是容易出错且繁琐,而且也很麻烦.如果这 ...

  10. InChatter系统之服务器开发(一)

    服务器端是整个消息系统的中枢,类似与人类的大脑.没有他,根本无法实现客户端之间的交流,为什么呢?这也涉及到我们的系统涉及,在服务器端,每个客户端的标识数据都会在服务器端进行保存,在这种情况下,当某一个 ...