P1280 Nick's task, p1280 Nick
Description
Nick connects to the Internet every day before he goes to work and receives emails from his boss. These emails include all the tasks that Nick's supervisor's department should complete on the same day, each task consists of a start time and a duration.
Nick's Business Day is N minutes, from the first minute to the nth minute. He started to work when Nick arrived at the Organization. If multiple tasks need to be completed at the same time, Nick can choose one of them, and the rest will be completed by his colleagues. If there is only one task, this task must be completed by Nick. If Nick is working at the start of some tasks, they will also be completed by Nick's colleagues. If a task starts at the P minute and lasts for T minutes, the task ends at the P + T-1 minute.
Write a program to calculate how Nick should select a task to get the maximum free time.
Input/Output Format
Input Format:
The first line of the input data contains two integers, N and K separated by spaces (1 ≤ N ≤ hour, 1 ≤ K ≤ 10000). N indicates Nick's working time, in minutes, K indicates the total number of tasks.
There are K rows in total. Each row has two integers P and T separated by spaces, indicating that the task starts from the P minute and lasts for T minutes, where 1 ≤ P ≤ N, 1 ≤p + T-1 ≤ N.
Output Format:
Only one line of the output file contains an integer, which indicates the maximum available time that Nick may obtain.
Input and Output sample input sample #1:
15 61 21 64 118 58 111 5
Output sample #1:
4
We use dp [I] to represent the maximum rest time at the time point I.
There are two types of decisions:
1. There was nothing to do in the last minute.
2. There is something to do in this minute.
In addition, enumeration must be performed in reverse order !.
If you do not know the cause, read the question carefully .,
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 #include<cstdlib> 8 using namespace std; 9 const int MAXN=10001;10 void read(int &n)11 {12 char c='+';int x=0,flag=1;13 while(c<'0'||c>'9')14 {c=getchar();if(c=='-')flag=-1;}15 while(c>='0'&&c<='9')16 {x=x*10+c-48;c=getchar();}17 n=(x*flag);18 }19 int n,m;20 int dp[MAXN];21 int bg[MAXN],con[MAXN];22 int main()23 {24 read(n);read(m);25 for(int i=1;i<=m;i++)26 {27 read(bg[i]);read(con[i]);28 }29 for(int i=n;i>=1;i--)30 {31 if(bg[m]!=i)32 dp[i]=dp[i+1]+1;33 else34 {35 while(bg[m]==i)36 {37 dp[i]=max(dp[i],dp[bg[m]+con[m]]);38 m--;39 }40 }41 }42 printf("%d",dp[1]);43 return 0;44 }