gym:Problem D Daydreaming Stockbroker(贪心买股票2.0)

				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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值