Tyvj p1034 Nick's task (DP)

Source: Internet
Author: User
Background The background question bank depends on everyone and everyone loves it. Description Description Nick connects to the Internet every day before going 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 is done by his colleagues. If there is only one task, this task must be written 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.
Inputformat the first line of input data contains two integers N and K separated by spaces. 1 ≤ n ≤ hour, 1 ≤ k ≤ 10000, and 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 outputformat only one line of output file contains an integer to indicate the maximum available time that Nick may obtain. Sample input sampleinput [Copy Data] 15 61 21 64 118 58 111 5Sample output sampleoutput [Copy Data] 4


If you select the forward DP, selecting or not selecting the current task will affect the subsequent selection. However, if you push the task in reverse order, set DP [I] to start from the I minute and the maximum idle time, if there is a task in the I minute, the task must be done. If there are multiple tasks, the maximum idle time after each task is completed is compared, another case is that there is no task in the I minute, so the idle time is the maximum idle time starting from I + 1 plus 1 minute.


# Include <stdio. h> # include <string. h> # include <algorithm> # include <math. h> using namespace STD; typedef long ll; const int max = 0x3f3f3f; const int maxn = 10005; struct c {int St, La;} A [maxn]; int N, k; int DP [maxn], flag [maxn]; bool CMP (C x, c y) {return X. st <Y. st;} int BS (int x, int y, int v) {int m; while (x <Y) {M = (x + y)> 1; if (A [M]. st> = V) y = m; else x = m + 1;} return X;} int main () {scanf ("% d", & N, & K); For (INT I = 1; I <= K; I ++) {scanf ("% d", & A [I]. st, & A [I]. la); flag [A [I]. st] = 1; // records the mark of each start time} Sort (a + 1, A + k + 1, CMP); For (INT I = N; i> = 1; I --) {If (flag [I]) {int T = BS (1, K, I ); // find the lower bound of all tasks starting from the current I minute for (Int J = T; A [J]. st = I; j ++) // enumerate all tasks starting at I minute DP [I] = max (DP [I], DP [I + A [J]. la]);} else DP [I] = DP [I + 1] + 1;} printf ("% d \ n", DP [1]); Return 0 ;}


Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.