Los task solution to a problem Nick's Valley P1280

Topic links: https://www.luogu.org/problem/P1280

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 Format

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 Format

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

Sample input and output

Input # 1
15 6
1 2
1 6
4 11
8 5
8 1
11 5
Output # 1
4

answer

This is a general problem solution are DP. In fact, you can use DFS violence before.

Introduce the code of a TLE. The code we do not have time to sort tasks, but the task vis information stored in the array. vis [i] [0] holds the start time of the task when the number i, and vis [i] [1] ...... vis [i] [n] holds the start time is the duration of each task i.

 1 #include <iostream>
 2 #include <math.h>
 3 #include <stdio.h>
 4 #include <algorithm>
 5 #include <string.h>
 6 
 7 using namespace std;
 8 
 9 const int MAXN = 1e4 + 5;
10 int n, t, start[MAXN], keep[MAXN], vis[MAXN][1005];
11 
12 int dfs(int st)
13 {
14     if(st >= t)
15     {
16         return 0;
17     }
18     if(vis[st][0] == 0)
19     {
20         st++;
21         return dfs(st);
22     }    
23     else
24     {    
25         int mint = 0x3f3f3f;
26         for(int i = 1; i <= vis[st][0]; i++)
27         {
28             int w = dfs(st + vis[st][i]);
29             if(mint > w + vis[st][i])
30             {
31                 mint = w + vis[st][i];
32             }
33         }
34         return mint;
35     }
36 }
37 
38 int main()
39 {
40     cin >> t >> n;
41     for(int i = 1; i <= n; i++)
42     {
43         cin >> start[i] >> keep[i];
44         vis[start[i]][0]++;
45         vis[start[i]][vis[start[i]][0]] = keep[i];
46     }
47     cout << t - dfs(1) << endl;
48     return 0;
49 }

To avoid TLE, each child node depth of search results can be saved in the data, if the search elements have been through the child node, it is no longer a search elements, so that you can the AC.

 1 #include <iostream>
 2 #include <math.h>
 3 #include <stdio.h>
 4 #include <algorithm>
 5 #include <string.h>
 6 
 7 using namespace std;
 8 
 9 const int MAXN = 1e4 + 5;
10 int n, t, start, keep, vis[MAXN][1005];
11 int data[MAXN];
12 
13 int dfs(int st)
14 {
15     if(st > t)
16     {
17         return 0;
18     }
19     if(data[st])
20     {
21         return data[st];
22     }
23     if(vis[st][0] == 0)
24     {
25         st++;
26         return dfs(st);
27     }    
28     else
29     {    
30         int mint = 0x3f3f3f;
31         for(int i = 1; i <= vis[st][0]; i++)
32         {
33             int w = dfs(st + vis[st][i]);
34             if(mint > w + vis[st][i])
35             {
36                 mint = w + vis[st][i];
37             }
38         }
39         data[st] = mint;
40         return mint;
41     }
42 }
43 
44 int main()
45 {
46     cin >> t >> n;
47     for(int i = 1; i <= n; i++)
48     {
49         cin >> start >> keep;
50         vis[start][0]++;
51         vis[start][vis[start][0]] = keep;
52     }
53     cout << t - dfs(1) << endl;
54     return 0;
55 }

 

Guess you like

Origin www.cnblogs.com/zealsoft/p/11330597.html