[Dynamic Route] Nick's workday (lignja)

Source: Internet
Author: User
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 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 oneProgramCalculate how Nick should select a task to get the maximum free time. The first line of input data in the input format contains two integers N and K separated by spaces. 1 ≤ n ≤ limit, 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. Only one line of the output file contains an integer, which indicates the maximum available time of Nick. Sample input 15 6 1 2 1 6 4 11 8 8 1 11 5 sample output 4

This topic uses the Inverted Sequence calculation method.
Sort the data in sequence based on the start time and end time as the second-related word (when the number of logs is less than the number of logs each time ), then initialize the first sequence of time segments starting with I.
Next, use the dynamic sequence for solving.

Maximum duration: Use F [I] to indicate the maximum amount of time from I to the end.

Moving equations: F [I] = max (F [R [k]) (where K exists );

= F [I + 1] + 1 (k does not exist ).

K indicates all the lower-order orders whose start time is I, and this K has been initialized and the number of parts is small.
Accode:

# Include <cstdio> # include <cstring> # include <cstdlib> # include <bitset> Using STD: Max; const char fi [] = "rqnoj434.in "; const char fo [] = "rqnoj434.out"; const int maxn = 10010; const int maxm = 10010; const int max = 0x3fffff00; const int min =-Max; struct LR {int L, r ;}; int f [maxm]; int FIR [maxm]; LR qujm [maxn]; int n, m; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W", stdout);} int CMP (const Void * a, const void * B) {If (LR *) A)-> L! = (LR *) B)-> L) Return (LR *) A)-> L-(LR *) B)-> L; return (LR *) A)-> r-(LR *) B)-> r;} void readdata () {scanf ("% d ", & M, & N); For (INT I = 1; I <n + 1; ++ I) {scanf ("% d", & qujm [I]. l, & qujm [I]. r); qujm [I]. R + = qujm [I]. l-1;} qsort (qujm + 1, n, sizeof (qujm [0]), CMP); For (INT I = N; I> 0; -- I) FIR [qujm [I]. l] = I;} void work () {f [M + 1] = 0; For (INT I = m; I> 0; -- I) if (! FIR [I]) f [I] = f [I + 1] + 1; else for (Int J = FIR [I]; j <n + 1; ++ J) {If (qujm [J]. l! = I) break; F [I] = max (F [I], F [qujm [J]. R + 1]);} printf ("% d", F [1]);} int main () {init_file (); readdata (); Work (); exit (0 );}

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.