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

  1. 4
  2. 1 2 3 3
  3. 4
  4. 1 2 3 4

Sample Output

  1. No
  2. Yes
  3. 1 -1 -1 1
  4.  
  5. 给你一个数组,选择其中的一半的数变成负数,然后这个数组的和为0
    首先来判断一下NO的情况,如果这个数组的和是一个奇数的话,那么你无论怎么选都不会有结果,然后如果你数组长度为1,那么就没得选了。
    YES该怎么做?先从大到小排序,然后一路加下去(使和大于数组和一半的不能加),如果刚好和等于数组和的一半,那么你选择的那些数就是应该乘以-1的数了
  1. #include"iostream"
  2. #include"algorithm"
  3. using namespace std;
  4.  
  5. const int maxn=100000+10;
  6.  
  7. int f[maxn];
  8. long long sum;
  9. int n;
  10.  
  11. struct node
  12. {
  13. int val,num;
  14. }a[maxn];
  15.  
  16. bool cmp(struct node a1,struct node a2)
  17. {
  18. return a1.val>a2.val;
  19. }
  20.  
  21. bool Init()
  22. {
  23. sum=0;
  24. for(int i=0;i<n;i++)
  25. {
  26. cin>>a[i].val;
  27. a[i].num=i;
  28. f[i]=-1;
  29. sum+=a[i].val;
  30. }
  31. if(sum%2) return false;
  32. else return true;
  33. }
  34.  
  35. void Work()
  36. {
  37. sort(a,a+n,cmp);
  38. long long cur=0;
  39. for(int i=0;i<n;i++)
  40. {
  41. if(cur+a[i].val<=sum/2)
  42. {
  43. cur+=a[i].val;
  44. f[a[i].num]=1;
  45. if(cur==sum/2) break;
  46. }
  47. }
  48. }
  49.  
  50. void print()
  51. {
  52. for(int i=0;i<n-1;i++)
  53. {
  54. if(i!=0) cout<<' ';
  55. cout<<f[i];
  56. }
  57. cout<<' '<<f[n-1]<<endl;
  58. }
  59.  
  60. int main()
  61. {
  62. while(cin>>n)
  63. {
  64. if(!Init()||n==1) cout<<"No"<<endl;
  65. else
  66. {
  67. cout<<"Yes"<<endl;
  68. Work();
  69. print();
  70. }
  71. }
  72. return 0;
  73. }

集训第四周(高效算法设计)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. java-异常简介

    1.简介 ############################################################### ############################### ...

  2. [hdu1695] GCD【莫比乌斯反演】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1695 先把题目转化为求一个数在区间[1, b / k],另一个数在区间[1, d / k]时,这两个数互 ...

  3. android 系统的时间间隔和睡眠用哪个?

    原文 : https://developer.android.com/reference/android/os/SystemClock.html SystemClock.elapsedRealtime ...

  4. C语言中Static和Const关键字的的作用 -- 转

    static作用:“改变生命周期” 或者 “改变作用域” 程序的局部变量存在于(堆栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中. 1.作用于变量: 用static声明局部变量-- ...

  5. ES之事件绑定,解除绑定以及事件冒泡、事件捕获

    绑定事件的处理方法任何元素都有事件属性,而绑定事件就是将这个事件与一个函数相连接. ①句柄事件dom.onXXX = function () {代码块} 以on开头的事件属于句柄事件兼容性非常好,但是 ...

  6. Rxlifecycle使用详解,解决RxJava内存泄露问题

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/1122/3711.html

  7. webpack3整理(第一节/满三节)

    一.css文件打包到js中(loader的三种写法) //第一种写法:直接用use. module: { rules: [{ test: /\.css$/, use: ['style-loader', ...

  8. (三)Redis for StackExchange.Redis

    目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis StackExchange.Re ...

  9. 15年第六届蓝桥杯第七题_手链样式_(stl_string)

    手链样式 小明有3颗红珊瑚,4颗白珊瑚,5颗黄玛瑙.他想用它们串成一圈作为手链,送给女朋友.现在小明想知道:如果考虑手链可以随意转动或翻转,一共可以有多少不同的组合样式呢? 请你提交该整数.不要填写任 ...

  10. ansible配置mysql主从复制

    配置主机1.下载安装所需安装包 [root@server1 ansible]# lsansible-2.7.8-1.el7.noarch.rpmansible-tower-setup-bundle-3 ...