Poj 2431, poj

Source: Internet
Author: User

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;}

  


  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.