码迷,mamicode.com
首页 > 其他好文 > 详细

Uva - 1614 - Hell on the Markets

时间:2015-06-24 22:40:57      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:acm   oj   uva   1614   hell on the markets   

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 icontracts for i -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 contracts ( 1技术分享ai技术分享i ) during i -th minute ( 1技术分享i技术分享n), 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 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 when the first bank is buying and bi = - 1 when the second one is buying (and the first one is selling), then the requirement for the trading session is that 技术分享aibi = 0 . 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 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 ( 1技术分享n技术分享100 000 ). The second line of the input contains n integer numbers -- ai ( 1技术分享ai技术分享i ).

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 numbers -- bi .

Sample Input 

4
1 2 3 3
4
1 2 3 4

Sample Output 

No
Yes
1 -1 -1 1

先考虑一个问题,总和为零,那么意味着正数之和与负数之和的绝对值相等,那么所有数的绝对值之和必定是偶数,所以先判断绝对值之和是不是偶数,不是偶数直接无解。

如果是偶数,把绝对值之和除以2就是正数之和,把输入的序列排序,然后从大到小贪心求解。因为排序之后顺序有了变化,所以用了pair存储,first是值,而second是原始序列中的位置,这样排序的时候存储位置的值也是跟着变化,但每个值的原始位置信息还保存完好。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset> 
#include <cassert> 
#include <cmath>
#include <functional>

using namespace std;

const int maxn = 100005;
pair<int, int> A[maxn];
int n;

int main()
{
	ios::sync_with_stdio(false);
	while (cin >> n) {
		long long sum = 0;
		for (int i = 0; i < n; i++) {
			cin >> A[i].first;
			A[i].second = i;
			sum += A[i].first;
		}

		int ans[maxn];
		if (sum % 2) { // 如果和是奇数,无解
			cout << "No\n";
		}
		else {
			cout << "Yes\n";
			sum /= 2;
			sort(A, A + n);
			for (int i = n - 1; i >= 0; i--) { // 从大到小贪心求解
				if (sum >= A[i].first) {
					sum -= A[i].first;
					ans[A[i].second] = 1;
				}
				else {
					ans[A[i].second] = -1;
				}
			}
			cout << ans[0];
			for (int i = 1; i < n; i++) {
				cout << ' ' << ans[i];
			}
			cout << endl;
		}
	}

	return 0;
}



Uva - 1614 - Hell on the Markets

标签:acm   oj   uva   1614   hell on the markets   

原文地址:http://blog.csdn.net/zyq522376829/article/details/46625629

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!