标签: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 ( 1aii ) during i -th minute ( 1in), 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.
4 1 2 3 3 4 1 2 3 4
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