Poj 2431, poj
Note:
There are n Refueling points, giving the position of each refueling point from the end point and how much oil can be added. The last line shows the total length and initial oil volume. Add at least a few times to reach the end. If not, output-1.
Sample Input
44 45 211 515 1025 10
Sample Output
2
Analysis:
At the beginning, we plan to use dfs for search. dis [I] indicates the amount of fuel that can be added to the I point. dfs (I, p, l) indicates the current position, and p indicates the current amount of fuel, l represents the total length. When I + p> = l, this condition meets the condition and requires the minimum value for the number of refuelling times that can be established, but it times out.
So I thought of using the priority queue to run farther with the current amount of fuel, and the less I refuel. Therefore, the priority queue stores the amount of oil. The pre represents the previous position, and the rest represents the current amount of oil. The dis represents how far it is going, and every time it passes through a point rest-= dis, store the fuel amount of this point in the priority queue, and retrieve elements from the queue when the rest <dis. If no element is desirable, it means that the end cannot be reached.
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;struct point{ int x,y;}s[10010];bool cmp(point a,point b){ return a.x<b.x;};int main(){ int n; cin>>n; int i; for(i=0;i<n;i++) cin>>s[i].x>>s[i].y; int l,p; cin>>l>>p; for(i=0;i<n;i++) s[i].x=l-s[i].x; sort(s,s+n,cmp); s[n].x=l; s[n].y=0; priority_queue<int>q; int rest=p; int pre=0; int ans=0; for(i=0;i<n+1;i++) { int dis=s[i].x-pre; while(rest<dis) { if(q.empty()) { ans=-1; break; } rest+=q.top(); q.pop(); ans++; } if(ans==-1) break; rest-=dis; pre=s[i].x; q.push(s[i].y); } cout<<ans<<endl;}