Luo Gu -1280 Nick task

Title Description
connected before Nick to work every day on the Internet, receiving his superiors sent by e-mail, these messages contain all the tasks Nick department in charge of the day to complete each task consists of a start time and a duration.
A working Nick is N minutes, from the first minute to the end of the N-th minutes. When Nick arrived at work he began to work. If there are multiple tasks to be done, Nick can choose one of them to do, and the rest by his colleagues completed, whereas if only one task, it is necessary to complete the task by Nick at the same time, if some tasks start time Nick is working to complete these tasks by Nick colleagues. If a task P starts at the first minute, the duration is T minutes, then the task to be concluded at P + T-1 minute.
Write a program to calculate how Nick task should be selected in order to get the most spare time.
Input Output Format
Input Format:
input data of the first line separated by a space containing two integers N and K (1≤N≤10000,1≤K≤10000), N represents Nick working time, in minutes, K represents The total number of tasks.
Next, a total of K rows, each row has two separated by a space T and the integer P, P represents the task minutes from the start, duration T min where 1≤P≤N, 1≤P + T-1 ≤N.
Output format:
the output file is only one line contains an integer representing the maximum spare time Nick might get.

Sample Input Output
Input Sample # 1:
15. 6
. 1 2
. 1. 6
. 4. 11
. 8. 5
. 8. 1
. 11. 5

Output Sample # 1:
4

Explanation: I did not expect a super simple linear dp, dp [i] represents the largest in idle time, maintenance tasks can be a good start every point, and I am here is sorted, then the maintenance interval of the same task start time

#include<iostream>
#include<algorithm>
#define N 10005
using namespace std;
int dp[N]={0};
int L[N]={0},R[N]={0};
struct node{
    int s,t;
};
node a[N];
int n=0,k=0;
bool cmp(node &a,node &b){
    return a.s<b.s;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1;i<=k;i++) cin>>a[i].s>>a[i].t;
    sort(a+1,a+1+k,cmp);
    for(int i=1;i<=k;i++){
        if(a[i].s!=a[i-1].s) L[a[i].s]=i;
    }
    for(int i=k;i>=1;i--){
        if(a[i].s!=a[i+1].s) R[a[i].s]=i;
    }
    for(int i=n;i>=1;i--){
        if(L[i]){
            for(int j=L[i];j<=R[i];j++){
                dp[i]=max(dp[i],dp[i+a[j].t]);
            }
        }else{
            dp[i]=dp[i+1]+1;
        }
    }
    cout<<dp[1]<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/mkopvec/article/details/92442714