标签:
注意题目给的是到终点的距离,需要转成到起点的距离,还有就是将终点也看成是加油站,这样写起来方便很多,不必要单独考虑最后一个加油站不在终点以后的情况
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; int N,P,L; pair<int,int> d[10100]; bool cmp(pair<int,int> a, pair<int,int> b) { return a.first > b.first; } void solve() { priority_queue<int> que; sort(d,d+N,cmp); d[N].first = L, d[N++].second = 0; int ans = 0, pos = 0, tank = P; for(int i = 0; i < N; i++) { if(i != N - 1) d[i].first = L - d[i].first; int dis = d[i].first - pos; while(tank < dis) { if(!que.empty()) { tank += que.top(); que.pop(); ans++; } else { cout<<"-1"<<endl; return; } } tank -= dis; pos = d[i].first; que.push(d[i].second); } cout<<ans<<endl; return; } int main() { #ifdef xxz freopen("in.txt","r",stdin); #endif // xxz ios::sync_with_stdio(false); cin.tie(0); while(cin>>N) { for(int i = 0; i < N; i++) { cin>>d[i].first>>d[i].second; } cin>>L>>P; solve(); } return 0; }
标签:
原文地址:http://blog.csdn.net/u013445530/article/details/43673327