HDU 1789 doing homework again [greedy | greedy + priority queue]

Source: Internet
Author: User
Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1789 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 29256 # Problem/adoing homework again

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4618 accepted submission (s): 2707


Problem descriptionignatius has just come back school from the 30th ACM/ICPC. now he has a lot of homework to do. every teacher gives him a deadline of handing in the homework. if Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final
Test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the specified CED score.


Inputthe input contains several test cases. The first line of the input is a single integer t that is the number of test cases. t test cases follow.
Each test case start with a positive integer N (1 <= n <= 1000) which indicate the number of homework .. then 2 lines follow. the first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the specified CED
Scores.


Outputfor each test case, You shoshould output the smallest total balanced CED score, one line per test case.


Sample Input

333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
 


Sample output

035
 


Authorlcy


Source2007 provincial training team exercise (10) _ Thanks to doomiii


Recommendlcy

Algorithm: greedy idea: it should have been solved by DP. After all, greedy can only solve local optimization, while DP solves global optimization, just as Hao said, all problems can be converted to DP, but DP is too weak. This data is relatively small and greedy, and can be solved in two cycles...
Sort jobs first: the jobs are written before the deadline, and the jobs with the same deadline are written with more points, so how to sort them is determined. The number of days of scanning starts. Only one job can be completed every day. Of course, the jobs with the most points deducted from the deadline are done first. We have sorted the jobs before. However, after such sorting, only the local optimum is guaranteed. If there is a lot of deduction points, but the end date is not completed, what should I do? [That is, the local optimization is converted to the global optimization problem] Then we can establish a loop to find the completed job, but the points are deducted less than the jobs that cannot be completed on schedule, find the job with the least points deducted from the heap of the job, and use its completion time to complete the job that cannot be completed currently. Time complexity O (N * n)
If DP is used, it can be reduced to O (N * logn), but the super senior at level 10 has mentioned: greedy many times you can consider the maximum and minimum heap (implemented using the priority queue) N * logn is the same as DP, and the maximum heap is not used. How does the minimum heap break through orzps? The idea seems to be confused, indicates that it is better to directly view the code...
Code:

Accepted 1789 62 Ms 284 K 2146 B C ++ Freeze

// Greedy solution DP # include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 1000 + 10; struct node {int day; // the end date int score; // The task is not handed in time, the deducted int flag; // indicates whether the job is written.} node [maxn]; bool CMP (node A, Node B) {if (. day = B. day) return. score> B. score; // end of the same day. First, write the else return. day <B. day; // write ahead of schedule to temporarily ensure local optimum} int main () {int t; int N; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); // number of jobs for (INT I = 0; I <n; I ++) scanf ("% d", & node [I]. day); For (INT I = 0; I <n; I ++) scanf ("% d", & node [I]. score); For (INT I = 0; I <n; I ++) node [I]. flag =-1; // initialization tag, no sort (node, node + N, CMP); int day = 1; // traverse the int index from the first day; int min; For (INT I = 0; I <n; I ++) {If (day <= node [I]. day) // can be completed and written directly. First, ensure the local optimum {node [I]. flag = 1; // mark the completed day ++; // Number of days plus one} else // if the end date cannot be completed, search for the number of days above and replace the previous one, but the minimum deduction value is {min = node [I]. score; Index =-1; // tag not found for (Int J = I-1; j> = 0; j --) // traverse to the front, at the same time, the minimum deduction point is updated {If (node [J]. flag = 1 & node [J]. score <min) // If {min = node [J] is found. score; // updated minimum score index = J; // subscript found in the record} If (index> = 0) // If {node [Index] is found. flag =-1; // replace, marked as incomplete node [I]. flag = 1; // currently marked as complete }}int ans = 0; For (INT I = 0; I <n; I ++) // traverse in sequence, find the completed job {If (node [I]. flag =-1) ans + = node [I]. score;} printf ("% d \ n", ANS);} return 0 ;}
Later, I tried using the priority queue according to this sort of thinking, and the time was halved:
Accepted 1789 31 Ms 256 K 1783 B C ++ Freeze

# Include <stdio. h> # include <queue> # include <string. h> # include <algorithm> using namespace STD; const int maxn = 1000 + 10; struct node {int day; int score; bool operator <(const node & B) const {// sort return B by score from small to large. score <score; // do not forget return ...}} node [maxn]; bool CMP (node A, Node B) {if (. day = B. day) return. score> B. score; else return. day <B. day;} int main () {int t; int N; scanf ("% d", & T); While (T --) {Int sum = 0; scanf ("% d", & N); For (INT I = 0; I <n; I ++) scanf ("% d", & node [I]. day); For (INT I = 0; I <n; I ++) {scanf ("% d", & node [I]. score); sum + = node [I]. score;} Sort (node, node + N, CMP); priority_queue <node> q; while (! Q. empty () Q. pop (); int Index = 1; for (INT I = 0; I <n; I ++) {If (index <= node [I]. day) {q. push (node [I]); index ++;} else {node TMP = Q. top (); If (TMP. score <node [I]. score) // if the minimum score in the queue is smaller than the score to be completed, the time is replaced by {q. pop (); q. push (node [I]) ;}} while (! Q. empty () // total score-score of the completed job {sum-= (Q. top ()). score; q. pop () ;}printf ("% d \ n", sum) ;}return 0 ;}


Teammates Orc directly use greedy, but the greedy method is different, and the efficiency of No-priority queue is also relatively high:

9002782 2013-08-19 13:05:55 Accepted 1789 31 Ms 216 K 987b G ++ Csust_2012_16

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 1010; struct point {int S, D;} p [maxn]; int CMP (point a, point B) {if (. S = B. s) return. d> B. d; return. s> B. s;} int main () {int I, j, k, n, m; int vis [maxn]; int t; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); memset (p, 0, sizeof (p); memset (VIS, 0, sizeof (VIS); for (I = 1; I <= N; I ++) scanf ("% d", & P [I]. d); for (I = 1; I <= N; I ++) scan F ("% d", & P [I]. s); sort (p + 1, P + n + 1, CMP); int ans = 0; for (I = 1; I <= N; I ++) {for (j = P [I]. d; j> = 1; j --) // start from the back, and hide the front to free up more space for individual jobs {If (! Vis [J]) // If the J-day is not occupied {vis [J] = 1; // use the J-day to write the I job break ;}} if (j = 0) ans + = P [I]. s; // If the I job cannot be completed} printf ("% d \ n", ANS);} return 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.