2016-2017-acmicpc-nordic-collegiate-programming-contest-ncpc-2016
Problem D Daydreaming Stockbroker
题目链接 http://codeforces.com/gym/101550/attachments
题意:你穿越了,知道n天股票的价格,现在你手上有100本金,你可以在每一天选择买或者卖出,每一天你可以买多股,但你手上的股票数目在任何时候不超过100000,问n天之后你最多有多少钱。
解题思路:
- 刚开始看到这个题的时候感觉以前做过,但是以前是每支股票你只能买一次。这个题把一个折线图画出来贪心的思路就很清晰了,不然穿越了也赚不到钱。
- 买入或者卖出只在折线图的转折点也就是波谷或者波峰的时候买卖。那肯定是每个波谷买,波峰卖啊。就这么简单,没炒过股看这个题半天没思路。
- 还需要连续天数价格一样需要去重,不然可能找不到转折点。
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll Max = 1e5;
vector <ll> num;
int main() {
//freopen("1.in", "r", stdin);
int n;
scanf("%d", &n);
long long ans = 100;
for(int i=0;i<n;i++) {
ll temp; scanf("%lld", &temp);
if(i != 0 && temp == num[num.size()-1])//去重
continue;
num.push_back(temp);
}
ll have = 0;
num.push_back(-1);//设置最后没有股票剩余
n = int(num.size());
for (int i = 0; i < n-1; i++) {
if(i == 0 && num[i] < num[i+1]) {
ll cnt = ans / num[i];
ans -= cnt*num[i];
have = cnt;
continue;
}
if(num[i] < num[i-1] && num[i] < num[i+1]) {//波谷买
ll cnt = ans / num[i];
cnt = min(cnt, Max);
have = cnt;
ans -= have*num[i];
} else if(num[i] > num[i-1] && num[i] > num[i+1]) {//波峰卖
ans += have * num[i];
have = 0;
}
}
printf("%lld\n", ans);
return 0;
}