Stock (Zoj 2921 greedy Classics)

Source: Internet
Author: User

Stock Time limit: 2 Seconds Memory Limit: 65536 KB

Optiver sponsored problem.

After years of hard work Optiver have developed a mathematical model that allows them to predict wether or not a company WI ll 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 s Chedule. Unfortunately, Optiver ' s model have determined that the company would go bankrupt after exactly n days, after which their sh Ares'll become worthless.

Still, Optiver holds a large number of sell options that allows them to sell some of the shares before the company goes Ba Nkrupt. However, there is a limit on the number of shares Optiver can sell every day, and price Optiver receives for a share may V ary. Therefore, it isn't immediately clear when Optiver should sell their shares to maximize their profit, so they asked you t O Write a program to calculcate.

Input

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

One line with a integer n (1 <= n <=): The number of days before the company goes Bankrup T.

n lines with three integers XI (0 <= XI <=),Pi (0 <= pi <= 10 0) and mi (0 <=mi <=): The number of shares Optiver receives on day I, the (selling) PR Ice 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

1
6
4 4 2
2 9 3
2 6 3
2 5 9
2 2 2
2 3 3

Sample Output

76

Test instructions: There are n stock, give the daily stock buy quantity, the day's stock price and the day maximum throw amount, the day I get the stock day can not throw, can stay to later throw. Q How much can I sell for the N-day?

Idea: greedy, from the back to the greedy, the last day of the stock of course can only be sold on the last day, the first day can be sold in the day and after, then you can maintain a priority queue to store the day I and the number of days after the throw amount of not zero of the date (high price priority), That throws the first day from the priority queue out of the highest price.

Code:

#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 RSO N 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, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("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];p riority_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 greedy Classics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.