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  i  contracts 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$ \le$ai$ \le$i ) during  i -th minute (  1$ \le$i$ \le$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  $ \sum_{​{i=1}}^{​{n}}$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$ \le$n$ \le$100 000 ). The second line of the input contains  n integer numbers --  ai (  1$ \le$ai$ \le$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
 
#include <cstdio>
#include <algorithm>
using namespace std;

//int a[100010];

int n;

long long sum;

int flag[100010];

typedef struct a_node
{
	int val;
	int num;
	bool operator < (const struct a_node& x) const
	{
		return val < x.val;
	}	
}a_node;

a_node a[100010];


int main()
{
	while(scanf("%d", &n) == 1)
	{
		sum = 0;
		for(int i = 0; i < n; i++)
		{
			scanf("%d", &a[i].val);
			a[i].num = i;
			sum += a[i].val;
			flag[i] = -1;
		}
	
		if(sum % 2 != 0)
		{
			printf("No\n");
			continue;
		}		

		sort(a, a+n);

		long long now_sum = 0;
		int i = n-1;
		while(now_sum != sum/2)
		{
			for(; i >= 0; i--)
			{
				if(now_sum + a[i].val <= sum/2)
				{
					flag[a[i].num] = 1;
					now_sum += a[i].val;
					i--;
					break;
				}
			}	
		}		
	/*		
		if(now_sum != sum/2)
			printf("No\n");
		else
		{
	*/		printf("Yes\n");
			for(int	i = 0; i < n; i++)
			{
				if(i == 0)
					printf("%d", flag[i]);
				else
					printf(" %d", flag[i]);
			}				
			printf("\n");
	//	}
	}
	return 0;
}

关键点在于排序后a1-ai可以凑出1-sum(a1-ai)中的任意一个整数。

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值