【HDOJ】2385 Stock

水题,逆向做+优先级队列。

  1 /* 2385 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <algorithm>
 12 #include <cstdio>
 13 #include <cmath>
 14 #include <ctime>
 15 #include <cstring>
 16 #include <climits>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <functional>
 20 #include <iterator>
 21 #include <iomanip>
 22 using namespace std;
 23 //#pragma comment(linker,"/STACK:102400000,1024000")
 24 
 25 #define sti                set<int>
 26 #define stpii            set<pair<int, int> >
 27 #define mpii            map<int,int>
 28 #define vi                vector<int>
 29 #define pii                pair<int,int>
 30 #define vpii            vector<pair<int,int> >
 31 #define rep(i, a, n)     for (int i=a;i<n;++i)
 32 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 33 #define clr                clear
 34 #define pb                 push_back
 35 #define mp                 make_pair
 36 #define fir                first
 37 #define sec                second
 38 #define all(x)             (x).begin(),(x).end()
 39 #define SZ(x)             ((int)(x).size())
 40 #define lson            l, mid, rt<<1
 41 #define rson            mid+1, r, rt<<1|1
 42 
 43 typedef struct node_t {
 44     int p, m;
 45     
 46     friend bool operator< (const node_t& a, const node_t& b) {
 47         return a.p < b.p;
 48     }
 49     
 50 } node_t;
 51 
 52 const int maxn = 1e5+5;
 53 node_t nd[maxn];
 54 int X[maxn];
 55 int n;
 56 
 57 void solve() {
 58     priority_queue<node_t> Q;
 59     node_t d;
 60     int x;
 61     int ans = 0;
 62     
 63     per(i, 0, n) {
 64         Q.push(nd[i]);
 65         x = X[i];
 66         while (!Q.empty() && x>0) {
 67             d = Q.top();
 68             Q.pop();
 69             if (d.m > x) {
 70                 d.m -= x;
 71                 ans += d.p * x;
 72                 x = 0;
 73                 Q.push(d);
 74             } else {
 75                 x -= d.m;
 76                 ans += d.p * d.m;
 77             }
 78         }
 79     }
 80     
 81     printf("%d\n", ans);
 82 }
 83 
 84 int main() {
 85     ios::sync_with_stdio(false);
 86     #ifndef ONLINE_JUDGE
 87         freopen("data.in", "r", stdin);
 88         freopen("data.out", "w", stdout);
 89     #endif
 90     
 91     int t;
 92     
 93     scanf("%d", &t);
 94     while (t--) {
 95         scanf("%d", &n);
 96         rep(i, 0, n)
 97             scanf("%d %d %d", &X[i], &nd[i].p, &nd[i].m);
 98         solve();    
 99     }
100     
101     #ifndef ONLINE_JUDGE
102         printf("time = %d.\n", (int)clock());
103     #endif
104     
105     return 0;
106 }

 

posted on 2016-01-31 21:28  Bombe  阅读(112)  评论(0编辑  收藏  举报

导航