uva 1614——UVA 1614 - Hell on the Markets

探讨了金融危机后仅存两家银行如何通过限定交易量和种类逐步恢复金融市场运作的问题,并提出了一种确保交易平衡的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Download as PDFMost 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
 
 
题意: 给定一个长度为n的序列a,满足1<=ai<=i(很关键的条件),要求确定每个数的正负号,使得最后的和为0.没有这样的策略则输出No。
 
 
思路:贪心。因为1<=ai<=i,那么很容易得到的一个结论就是当所有数的绝对值得和为奇数时,一定没有方法使最后的和为0.当为偶数时,一定会存在一种方法。那么选取的时候必定是正1为sum的一半,-1为sum的一半,在遍历时不断贪心找到为+1的那一半即可。

code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int INF=0x3fffffff;
const int inf=-INF;
const int N=100005;
const int M=20005;
const int mod=1000000007;
const double esp=1e-4;
const double pi=acos(-1.0);

#define cls(x,c) memset(x,c,sizeof(x))
#define cpy(x,a) memcpy(x,a,sizeof(a))
#define fr(i,s,n) for (int i=s;i<=n;i++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt  rt<<1
#define rrt  rt<<1|1
#define middle int m=(r+l)>>1
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define mk make_pair
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);

int v[N],s[N];
int main()
{
	int n;
	while (~scanf("%d",&n)&&n)
	{
		ll sum=0;
		fr(i,1,n) scanf("%d",&v[i]),sum+=v[i];
		
		if (sum&1) {puts("No");continue;}
		ll p=0;
		for (int i=n;i>0;i--)
		{
			if ((p+v[i])*2<=sum)p+=v[i],s[i]=1;  //所有的为+1的那一半
			else s[i]=-1;
		}
		puts("Yes");
		fr(i,1,n) printf("%d ",s[i]);
		puts("");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值