1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include<bits/stdc++.h> #define int long long using namespace std; struct Node{ int a,b,c; bool used; }; vector<Node> v; struct Node1{ int a,b,c,id; bool operator < (Node1 b) const { return (this->a)>b.a; } }; struct Node2{ int a,b,c,id; bool operator < (Node2 b) const { return (this->b)-(this->c)<b.b-b.c; } }; priority_queue<Node1> p1; priority_queue<Node2> p2;
#define gc getchar() template<typename T> void read(T&r) { r=0; char ch=gc,last='z'; while(ch<'0'||ch>'9') last=ch,ch=gc; while(ch>='0'&&ch<='9') r=r*10+ch-'0',ch=gc; if(last=='-') r=-r; }
signed main(signed argc,char* argv[]) {
int T,V,n,a,b,c; read(T); while(T--) { v.clear(); while(!p1.empty()) p1.pop(); while(!p2.empty()) p2.pop(); read(V),read(n); for(int i=0;i<n;i++) { read(a),read(b),read(c); v.push_back({a,b,c,0}); p1.push({a,b,c,i}); p2.push({a,b,c,i}); } for(int i=0;i<n;i++) { Node1 t1=p1.top(); while(v[t1.id].used) p1.pop(),t1=p1.top(); Node2 t2=p2.top(); while(v[t2.id].used) p2.pop(),t2=p2.top(); if(V>=t1.a) { V+=t1.b; v[t1.id].used=true; p1.pop(); } else { V+=t2.b-t2.c; v[t2.id].used=true; p2.pop(); } } printf("%d\n",V); } return 0; }
|