[1280] Luo Gu Nick task

Title Description

Nick is connected before going 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 and output formats

Input formats:

 

The first line of input data 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 formats:

 

Output file only one line, contains an integer representing the maximum spare time Nick might get.

 

Sample input and output

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

problem Solution: DP la la
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,k,i,j,f[10005],h,z,v,c[10005];//p[10005],t[10005];
 
struct node
{
    int p;
    int t;
}a[10005];
 
int cmp(node x,node y)
{
    return x.p<y.p;
}
int main()
{
    cin>>n>>k;
    for(i=1;i<=k;i++)
    cin>>a[i].p>>a[i].t;
    sort(a+1,a+k+1,cmp);
    f[k+1]=0;
    for(i=n;i>=1;i--)
    {
        h=0;v=0;
        for(z=k;z>=1;z--)
        {
            if(a[z].p==i)
            {
                h=1;c[++v]=z;
            }
        }
        if(h!=1)f[i]=f[i+1]+1;
        else
        {
            for(z=1;z<=v;z++)
            {
                f[i]=max(f[a[c[z]].p+a[c[z]].t],f[i]);
            }
        }
    }
    cout<<f[1]<<endl;
    return 0;
}

 



Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11205904.html