Stock (zoj 2921 贪心经典)

StockTime Limit: 2 Seconds Memory Limit: 65536 KB

Optiver sponsored problem.

After years of hard work Optiver has developed a mathematical model that allows them to predict wether or not a company will be succesful. This obviously gives them a great advantage on the stock market.

In the past, Optiver made a deal with a big company, which forces them to buy shares of the company according to a fixed schedule. Unfortunately, Optiver’s model has determined that the company will go bankrupt after exactly n days, after which their shares will become worthless.

Still, Optiver holds a large number of sell options that allows them to sell some of the shares before the company goes bankrupt. However, there is a limit on the number of shares Optiver can sell every day, and price Optiver receives for a share may vary from day to day. Therefore, it is not immediately clear when Optiver should sell their shares to maximize their profit, so they asked you to write a program to calculcate this.

Input

On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

One line with an integer n (1 <= n <= 100 000): the number of days before the company goes bankrupt.

n lines with three integers xi (0 <= xi <= 100),pi (0 <= pi <= 100) and mi (0 <=mi <= 10 000 000): the number of shares Optiver receives on day i, the (selling) price per share on dayi, and the maximum number of shares Optiver can sell on day i, respectively.

Output

For each test case:

One line with the maximum profit Optiver can achieve.

Sample Input

164 4 22 9 32 6 32 5 92 2 22 3 3

Sample Output

76

题意:有n张股票,给出每天股票的买进数量,当天的股票价格和当天最大抛出量,第i天得到的股票当天可以不抛,,可以留到以后抛。问这n天最多能卖多少钱?

思路:贪心,从后往前贪心,最后一天的股票当然只能在最后一天卖出,第i天的可以在第i天及以后卖出,那么就可以维护一个优先队列来存放第i天及以后的天数中抛出量不为零的日期(价格高的优先),那抛出第i天时先从优先队列中取出价格最高的。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 100005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FRL(i,a,b) for(i = a; i < b; i++)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n)scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pfprintf#define DBGpf("Hi\n")typedef long long ll;using namespace std;struct Node{int p,num;friend bool operator <(Node x,Node y){return x.p<y.p;}};int a[maxn],b[maxn],c[maxn];priority_queue<Node>Q;int main(){int i,j,t,n,x;sf(t);while (t–){sf(n);FRL(i,0,n)sfff(a[i],b[i],c[i]);while (!Q.empty()) Q.pop();Node st;int ans=0;for (i=n-1;i>=0;i–){st.num=c[i];st.p=b[i];Q.push(st);int temp=a[i];while (!Q.empty()&&temp){st=Q.top();Q.pop();if (st.num>temp){st.num-=temp;ans+=temp*st.p;temp=0;Q.push(st);}else{temp-=st.num;ans+=st.p*st.num;}}}printf("%d\n",ans);}return 0;}

空虚无聊的时候就读书,但一定得有自己的生活目标和计划。

Stock (zoj 2921 贪心经典)

相关文章:

你感兴趣的文章:

标签云: