Buy and Resell
- Time Limit: 2000/1000 MS (Java/Others)
- Memory Limit: 65536/65536 K (Java/Others)
- Total Submission(s): 1469
- Accepted Submission(s): 488
Problem Description
The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the i-th city and choose exactly one of the following three options on the i-th day:
- spend ai dollars to buy a Power Cube
- resell a Power Cube and get ai dollars if he has at least one Power Cube
- do nothing
Obviously, Noswal can own more than one Power Cubes at the same time. After going to the n cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning.
Input
There are multiple test cases. The first line of input contains a positive integer T (T≤250), indicating the number of test cases. For each test case:
The first line has an integer n. ($1≤n≤10^5$)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. ($1≤a_i≤10^9$)
It is guaranteed that the sum of all n is no more than $5×10^5$.
Output
For each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.
Sample Input
3
4
1 2 10 9
5
9 5 9 10 5
2
2 1
Sample Output
16 4
5 2
0 0
Hint
In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16
In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5
In the third case, he will do nothing and earn nothing. profit = 0
Source
题意
给你接下来n天的交易价格,每天可以买进一个或者卖出一个或者什么都不做,问n天之后能获得的最大利润,并要求交易次数最少,假设一开始什么都没有,但是钱足够多。
题解
和CF866D Buy Low Sell High基本上一样,加了一个最少交易次数的限制,貌似之前湖南多校和牛客多校都出过类似的题。
思考一个问题,怎么才能获得收益?当然是低价买入高价卖出,但是问题是如果现在就卖出了后面可能会有更高的价格可以卖出。怎么解决这个问题呢?我们可以用一个优先队列(从小到大排序)代表当前所持有的所有股票,枚举每个时候的股票价格,如果队首元素值小于当前枚举值,那么就可以卖出股票赚取利润,队首元素出队,但是注意需要把当前枚举值再次加入队列,因为被弹出的队首元素可能会和后面价格更高股票交易获取更多的利润,可以用当前枚举值与后面价格更高的股票交易,相当于当前枚举的股票没有进行操作。A 买入 B 卖出,B 买入 C 卖出等价于 A 买入 C 卖出,相当于 B 没有操作。比如
2 5 9
,5-2=3
+9-5=4
=9+5-(5-2)=7
。
那么怎么才能交易次数最少呢,当然是在后面获取更大的利润时才算交易,使用一个标记数组,对当前交易的价格进行记录,如果队首元素被标记过,那么在后面获取更大的利润时交易次数就减一,相当于之前的交易没有进行,具体参见代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
typedef long long ll;
using namespace std;
int main()
{
int T, n;
ll x, ans1, ans2;
multiset <ll> S;
map <int, int> mp;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
ans1 = ans2 = 0;
S.clear();
mp.clear();
for (int i = 0; i < n; ++i)
{
scanf("%lld", &x);
S.insert(x);
if (*S.begin() < x) // 队首元素小于当前价格
{
ans2++;
if (mp[*S.begin()] > 0) // 如果之前交易过次数减1
{
ans2--;
mp[*S.begin()]--;
}
ans1 += (x - * S.begin()); // 利润
S.erase(S.begin()); // 卖出
S.insert(x); // 再次加入,后面可能还有更高的利润
mp[x]++; // 标记
}
}
printf("%lld %lld\n", ans1, 2 * ans2);
}
return 0;
}
好像还有更神仙的写法。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <algorithm>
typedef long long ll;
using namespace std;
int main()
{
int T, n;
ll x, ans1, ans2;
multiset <pair<ll, int> > S;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
ans1 = ans2 = 0;
S.clear();
for (int i = 0; i < n; ++i)
{
scanf("%lld", &x);
S.insert(make_pair(x, 0)); // 买入状态
S.insert(make_pair(x, 1)); // 卖出状态
ans1 += (x - S.begin()->first);
if (S.begin()->second == 1) // 卖出时操作*2
ans2 += 2;
S.erase(S.begin());
}
printf("%lld %lld\n", ans1, ans2);
}
return 0;
}
当然本题也可以使用优先队列完成,可以重载或者存入相反数,就相当于从小到大排序。
The end.
2018-08-27 星期一