|
|
Hi. I wonder, how can I solve it with dp? I thinked a whole day and cant find the solution less than O(N*T). Give me please some hint. Just sort by endtime and use greedy. It's that easy. Why does it work? Can you prove that approach? I don't understand why it does work :(. O(N * T)? What is 'T'? This can be solved using greedy, but the O(N^2) DP solution is as follows: 1) Sort by start times dp[i]: maximum events that can be attended
iterate from j = 0 to i-1, if end time of j-th conference is less than start time of i-th and dp[j] + 1 > dp[i] then dp[i] = dp[j] + 1 answer is max of all elements of dp array You should save dp[max(end_time)]; remember that for each end time there maybe different start times so that (end - start) are different. Save these segments, mp[end].push_back(end-start + 1); now check from 1 to max(end_time) and for each i, if i is end time? then check this segments sizes, for each such segment dp[i] = 1 + dp[i - s]; where s is saved segment sizes for end time 'i'. https://pastebin.com/C3dagNL2 Edited by author 30.04.2022 18:18Use scanline and sort events by right border abd after that just count segments that their l > now so after this make now = r, ans++; firstly ans = 0 and now = 0; Sorry for my English i'm from Russia Read tasks and deadlines from the programmer's handbook. similar greedy approach will work in this one. Also, why is this tagged dp? my dp soln is giving tle My DP solution works in ~0.001 seconds, please optimize your DP. 5 1 9 14 15 1 11 12 13 10 15 Right answer is 3, but some algorithmes may give answer 2 for this test. P.S. I had this mistake:( P.P.S. Sorry, for my poor English... Thank you! This test helped me to fix WA6 Edited by author 10.07.2019 16:23 if you use dp, l, r can be (>30000). I still WA on it!!! 5 1 8 2 3 4 5 6 7 9 10 Answer: 4. I have answer 4 on this test! Why WA? Try this: 3 1 5 2 5 3 5 Answer: 1 Check when completion time is equal,then sort it in increasing order of arrival time If you are using DP, make sure you are using multimap instead of map ( In c++ )
I wrote the following code. It takes 0.748 s, 13 524 KB. Can you suggest something faster? [code deleted] Edited by author 01.05.2016 22:19 Edited by moderator 04.12.2019 21:38 Why does this solution work? Can anybody prove that? I dont understand why it work. Thanks! you can replace for x in range(confs): l.append(tuple(map(int, input().split()))) by for x in sys.stdin: l.append(tuple(map(int, x.strip().split()))) 1. Sort it by endTime, if endTime equals another endTime then sort them by startTime. 2. Open one iteration from 2 to n, init just check that, is endTime<starTime, if so then ans++; 3. print ans; endTime and startTimes is one array with unchanged same index... Sorry for poor English
I don't think sorting by startTime is necessary. I think we should reverse-sort by startTime. This is a famous problem known as "interval scheduling". My solution in Go (library sort + O(N)) got TLE on 16. Wrote in in C++ and got accepted. But I really surprised that someone (gadget, zalick, rozdestvenskiy) solved it in Python, which is slower then Go Finally got accepted in Go Used bufio instead of fmt. fmt is unbuffered and might be very slow 8 1 2 1 2 3 4 4 5 3 4 1 2 3 4 1 2 Answer 2 p.s. Greedy Algorithm leads to AC I know AC solution that answers 0 to the following test: 1 29999 30000 New tests were added. Read Site news. thank you . you inspired me! Try this: 5 2 3 4 5 6 7 2 8 9 10 The answer is 4. This is Test 2: 7 1 90 91 125 129 200 3 130 140 150 160 162 201 202 The answer is 5. try this: 4 1 5 6 7 2 3 4 5 resault is 3. Thanks for this examples :) Edited by author 18.03.2015 19:57 Edited by author 18.03.2015 19:57 Thanks for the site problem, a very interesting and exciting)) Passed with PD + Segment tree. Edited by author 16.06.2012 05:41 Edited by author 16.06.2012 05:42 Yes, thanks for the problem. My solution is one sort, DP without recursion and with binary search. AC since 19th try :) Can anybody help me??? Please... i think this problem can be solved using greedy algo too, sort them according to the ending time, take a time variable 't',we took the interval whose starting time is later than 't' and choose the one which ends the earliest. for example: 1-4 4-14 6-10 3-7 11-16 sorted: 1-4 3-7 6-10 4-14 11-16 here 1-4 ends the earliest, then we have a choose 6-10 (as 3-7 starting time is before 4) .... i think you get the idea. Hi. What's in stdin of 13th test? My solution is crashing with std::cin or scanf. With VS 2010 scanf_s it doesn't crash. UPD hm... replaced scanf with std::cin that I was using at first. Looks like it has nothing to do with it, but because of compiler... UPD2 comparator function for std::sort requires strict ordering :( Edited by author 17.06.2014 02:44 Greedy can pass this problem quickly. Edited by author 11.11.2012 02:32 of course greedy is correct. First sort the data according to finish time,than starting time. Suppose I have sorted the data by finish time and then by start time. Now what are you doing with the data? Are you binary searching over the data for each of the N events of the array? Please specify more. one linear pass Useful test: 3 1 6 2 3 4 5 Answer: 2 Why won't greedy pass? Exactly this problem is used to teach greedy in most of all text books(all I have ever read). |
|
|