(Hdu step 9.1.2) Doing Homework again (greedy-there are n jobs, each of which has a certain completion time and the cost to be paid when not completed, at the minimum cost), serial number of acrobat9.1.2
Question:
Doing Homework again |
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission (s): 63 Accepted Submission (s): 57 |
|
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 Input333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4 |
Sample Output035 |
Authorlcy |
Source2007 provincial training team exercise (10) _ Thanks to DOOMIII |
Recommendlcy |
Question Analysis:
Greedy. Greedy strategy: If the completion time is different, the completion time is too short. If the completion time is the same, the cost will be higher. In fact, it only ensures that there are as many homework as possible, but it does not guarantee that the cost is the minimum. When a job may not be completed, we need to find a job that costs less than the current job does not do.
The Code is as follows:
/** B. cpp ** Created on: March 26, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <algorithm> using namespace std; const int maxn = 1001; struct Homework {int deadline; int score; bool flag;} homeworks [maxn]; bool cmp (Homework a, Homework B) {if (. deadline! = B. deadline) {// return. deadline <B. deadline; // the job with a tight completion time is listed at the top} return. score> B. score; // If the completion time is the same, the score deducted when the score is not completed is ranked first} int main () {int t; scanf ("% d", & t ); while (t --) {int n; scanf ("% d", & n); int I; for (I = 0; I <n; ++ I) {// read the completion time of each job in sequence scanf ("% d", & homeworks [I]. deadline) ;}for (I = 0; I <n; ++ I) {// The price scanf ("% d ", & homeworks [I]. score); homeworks [I]. flag = true; // all jobs can be completed by default at the beginning} sort (homeworks, homeworks + n, cmp ); // sort jobs by priority int k = 1; // the current day int sum = 0; // the price to be paid for (I = 0; I <n; ++ I) {// traverse all jobs if (homeworks [I]. deadline> = k) {// if the current job completion time is later than the current number of days, k ++; // It proves that the current job can complete continue; // find the next job} // the following code can be executed. zuming finds an unfinished job int p = homeworks [I]. score; // mark the price int pos = I when the current job is not completed; // mark the index int j of the current job; // search for a job, if this job is not performed, the minimum price is for (j = 0; j <I; ++ j) {// traverse the left and right jobs before the job change // if a job is found, price paid <if the current job does not do the price paid & the job is done if (homeworks [j]. score <p & homeworks [j]. flag = true) {p = homeworks [j]. score; // update the current price pos = j; // record the index of the job} sum + = p; // The total cost plus the cost of the discarded job. homeworks [pos]. flag = false; // mark the job as incomplete (discarded)} printf ("% d \ n", sum);} return 0 ;}