hdu-2385-Stock-贪心

http://acm.hdu.edu.cn/showproblem.php?pid=2385


n个股票流水,每天给你 x 份股票,每份可以在当天卖出y元,当天最多能卖z份


股票可以当天不卖留着后面卖,求最大的利润:


贪心  由于第1支股票可以在 1..n天卖,  第二支可在 2---n天卖,最后一支只能在第n天卖

所以倒着卖,先卖最后一支,如果卖完还有剩余的份额,丢到优先队列里,倒数第二天则从优先队列里选一个最大的价格 抛售 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

int a[112345],b[112345],c[112345];
struct node
{
    int price,id,res;
    node() {}
    node(int a,int b,int c)
    {
        price=a,id=b,res=c;
    }
    bool  operator <( node b) const
    {
        return  price<b.price;
    }
};
priority_queue<node>q;
int main()
{
    int n;
    int t;
    cin>>t;
    while(t--)
    {
        while(!q.empty())q.pop();
        scanf("%d",&n);
        if (!n) break;
        for (int  i=1; i<=n; i++)
            scanf("%d %d %d",&a[i],&b[i],&c[i]);

        int sum=0;
        for (int i=n; i>=1; i--)
        {
            q.push(node(b[i],i,c[i]));

            int num=a[i];
            while (num>0&&!q.empty())
            {
                node tp=q.top();
                q.pop();
                if (num<=tp.res)
                {
                    sum+=num*tp.price;
                    tp.res-=num;
                    q.push(tp);
                    break;
                }
                else
                {
                    sum+=tp.res*tp.price;
                    num-=tp.res;
                }
            }
        }
        printf("%d\n",sum);

    }
    return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值