Zoj-2921 stock ***

Source: Internet
Author: User
1/* pure turn, http://hi.baidu.com/sheep_finalfreedom/blog/item/1fbb2046408dd889b3b7dc4a.html */

2 /*
3 http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2921.
4
5 initially thought it was DP. Later I figured it out and found it was a classic greedy!
6
7. The strategy is to sell the product from the last day!
8
9 although the stock on the last day may include the stock that has not been sold for the past few days,
10 but the price of the day is the same for every stock,
11. Therefore, we use a greedy policy,
12 first, how much can I sell my own stock on the last day,
13. If you can sell it again after sales,
14. Add the quota to the last day,
15 In this way, the last day of the stock has two options,
16. The price of the last day or the price of the last day,
17 of course, it is the best price to sell,
18 if there is still a quota after the sale, it will be added to the last three days,
19 and so on
20
21. Use the priority queue when adding a quota. Each Pop will throw the highest price quota!
22 *
23 */
24 # include <iostream>
25 # include <queue>
26 using namespace STD;
27
28 struct share
29 {
30 int price, size;
31 friend bool operator <(share N1, share N2)
32 {
33 return n1.price <n2.price;
34}
35 };
36 priority_queue <share> q;
37
38 int G [100001];
39 int P [100001];
40 int s [100001];
41
42 int main ()
43 {
44 // freopen ("Debug \ in.txt", "r", stdin );
45 int repeat, M, I;
46 int total, temp;
47 share tt;
48 CIN> repeat;
49 while (Repeat --)
50 {
51 CIN> m;
52 for (I = 1; I <= m; I ++)
53 scanf ("% d", & G [I], & P [I], & S [I]);
54
55 while (! Q. Empty ())
56 Q. Pop ();
57 total = 0;
58 for (I = m; I> = 1; I --)
59 {
60 TT. Price = P [I];
61 TT. size = s [I];
62 Q. Push (TT );
63 temp = G [I];
64 While (! Q. Empty () & temp)
65 {
66 TT = Q. Top ();
67 Q. Pop ();
68 if (temp> = TT. Size)
69 {
70 temp-= TT. size;
71 total + = TT. Size * TT. price;
72}
73 else
74 {
75 total + = temp * TT. price;
76 TT. size = TT. Size-temp;
77 Q. Push (TT );
78 temp = 0;
79
80
81}
82}
83
84}
85 cout <total <Endl;
86}
87
88 return 0;
89}

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.