Problem Solution P2920 [USACO08NOV] Time Management]

Okay, no more nonsense. Let's get to the point. First of all, if you don't know how to get rid of it, you can look at it. The Big Man's Articles
This question is to ask when we can get up at the latest. Here we need to add a little greed, that is, to deal with the problem before the end time is short. As for why, I believe you can understand it.
Then, we define three variables, \\\\\\\\\\\\\\\\\\\\\\\\ For a long time.
First of all, we can judge that if the current time plus the time needed for the task does not exceed the time that the task must end, then the new time equals the original time plus the time spent by the task. Otherwise, it proves that this number is illegal. This is the way of thinking. If you do not understand, please read the following code carefully:

#include <iostream>
#include <cstdio>
using namespace std;
struct node
{
    int start;   //Starting time 
    int comes;   //What time must it end? 
};
struct node que[1000001];
int n;
bool check(int cnt)
{
    int x,y,tnt=cnt;
    for(x=1;x<=n;x++)
    {
        if(que[x].start+tnt<=que[x].comes)//If the current time plus task time is less than or equal to the end time, the new time is equal to the old time plus task time.
            tnt=que[x].start+tnt;
        else
            return false;//Otherwise, the figure is not valid.
    }
    return true;
}
int main()
{
    int right=1000000000,left=1,mid=(left+right)/2;
    int i,j,k,ans=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d %d",&que[i].start,&que[i].comes);//read in data
    for(i=1;i<=n;i++)
        for(j=i+1;j<=n;j++)
            if(que[i].comes>que[j].comes)
                swap(que[i],que[j]);//Short end time needs to be handled in front
    while(left<=right)//This is just a template. I use the method of recording answers.
    {
        mid=(left+right+1)/2;//Preventing Dead Cycle
        if(check(mid))
        {
            ans=mid;    //If this number is valid, look for a better solution on its right.
            left=mid+1;
        }
        else
            right=mid-1;//Otherwise, find the solution on its left.
    }
    if(ans!=0)
        cout<<ans;
    else
        cout<<-1;
    return 0;//Successful conclusion
}

Keywords: PHP less

Added by Kower on Sat, 12 Oct 2019 19:22:34 +0300