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;
}