水贪心。
当经过一个加油站的时候,记下这个加油站能加的油,然后没油的时候从经过的加油站中选择加油最多的加。
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<vector> 9 #include<stack> 10 #include<queue> 11 using namespace std; 12 #define enter puts("") 13 #define space putchar(' ') 14 #define Mem(a, x) memset(a, x, sizeof(a)) 15 #define rg register 16 typedef long long ll; 17 typedef double db; 18 const int INF = 0x3f3f3f3f; 19 const db eps = 1e-8; 20 const int maxn = 1e4 + 5; 21 inline ll read() 22 { 23 ll ans = 0; 24 char ch = getchar(), last = ' '; 25 while(!isdigit(ch)) {last = ch; ch = getchar();} 26 while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();} 27 if(last == '-') ans = -ans; 28 return ans; 29 } 30 inline void write(ll x) 31 { 32 if(x < 0) x = -x, putchar('-'); 33 if(x >= 10) write(x / 10); 34 putchar(x % 10 + '0'); 35 } 36 37 int n; 38 struct Node 39 { 40 int d, val; 41 bool operator < (const Node &oth)const 42 { 43 return d < oth.d; 44 } 45 }t[maxn]; 46 47 priority_queue<int> q; 48 49 int main() 50 { 51 int T = read(); 52 while(T--) 53 { 54 while(!q.empty()) q.pop(); 55 n = read(); 56 for(int i = 1; i <= n; ++i) t[i].d = read(), t[i].val = read(); 57 int dis = read(), p = read(); 58 for(int i = 1; i <= n; ++i) t[i].d = dis - t[i].d; 59 sort(t + 1, t + n + 1); 60 int now = 0, ans = 0; 61 bool flg = 1; 62 int i = 1; 63 while(now < dis) 64 { 65 while(p - t[i].d + now >= 0 && i <= n) 66 { 67 p = p - t[i].d + now, now = t[i].d; 68 q.push(t[i].val); 69 i++; 70 } 71 if(now + p >= dis) break; 72 if(q.empty()) {flg = 0; break;} 73 p += q.top(); q.pop(); ans++; 74 } 75 if(!flg) puts("-1"); 76 else write(ans), enter; 77 } 78 return 0; 79 }