Hdu1789 doing homework again [greedy]

Source: Internet
Author: User

Doing homework againtime limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others) Total submission (s): 6878 accepted submission (s): 4096

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 competition (10) _ Thanks to doomiii: There are n tasks, each task needs to be completed all day, and the deadline and the final penalty value for each task are given, calculates the minimum penalty value. Question: after reading this question, the first response is the optimal matching of the bipartite graph, and then directly on the template. The result is a timeout. Later I found that this question can be greedy, select the task arrangement with the highest penalty value each time. The rule is to select a number of days from the end of the task's validity period before the task is not scheduled, and place the task on this day. Greedy:

#include <stdio.h>#include <string.h>#include <algorithm>#define maxn 1010struct Node {int days, penalty;} arr[maxn];bool vis[maxn];bool cmp(Node a, Node b) {return a.penalty > b.penalty;}int main() {// freopen("stdin.txt", "r", stdin);int t, n, i, j, sum, sum2;scanf("%d", &t);while(t--) {scanf("%d", &n);sum = sum2 = 0;for(i = 0; i < n; ++i) scanf("%d", &arr[i].days);for(i = 0; i < n; ++i) {scanf("%d", &arr[i].penalty);sum += arr[i].penalty;}std::sort(arr, arr + n, cmp);memset(vis, 0, sizeof(vis));for(j = 0; j < n; ++j)for(i = arr[j].days; i; --i)if(!vis[i]) {vis[i] = true;sum2 += arr[j].penalty;break;}printf("%d\n", sum - sum2);}return 0;}

The km of TLE:
# Include <stdio. h> # include <string. h> # define maxn 1010 # define INF 0x3f3f3f3fint G [maxn] [maxn]; int lx [maxn], Ly [maxn]; int match [maxn]; bool visx [maxn], visy [maxn]; int slack [maxn], n, m, sum; bool DFS (INT cur) {int T, Y; visx [cur] = true; For (y = 1; y <= N; ++ y) {If (visy [y]) continue; T = Lx [cur] + ly [y]-G [cur] [Y]; If (0 = T) {visy [y] = true; if (-1 = match [y] | DFS (Match [y]) {match [y] = cur; return tru E ;}} else if (Slack [y]> T) slack [y] = T;} return false;} int km () {int I, j, x, D, ret = 0; memset (match,-1, sizeof (INT) * (n + 1); memset (ly, 0, sizeof (INT) * (n + 1); for (I = 1; I <= N; ++ I) {lx [I] =-INF; For (j = 1; j <= N; ++ J) if (G [I] [J]> lx [I]) lx [I] = G [I] [J];} for (x = 1; x <= N; ++ X) {memset (slack, 0x3f, sizeof (INT) * (n + 1); While (true) {memset (visx, 0, sizeof (bool) * (n + 1); memset (visy, 0, Sizeof (bool) * (n + 1); If (DFS (x) break; D = inf; for (I = 1; I <= N; ++ I) if (! Visy [I] & D> slack [I]) d = slack [I]; for (I = 1; I <= N; ++ I) if (visx [I]) lx [I]-= D; for (I = 1; I <= N; ++ I) if (visy [I]) ly [I] + = D; else slack [I]-= D ;}}for (I = 1; I <= N; ++ I) if (Match [I]>-1) RET + = G [Match [I] [I]; return ret;} void getmap () {int I, W, J; sum = 0; scanf ("% d", & N); memset (G, 0, sizeof (g); for (I = 1; I <= N; + + I) scanf ("% d", & Match [I]); // indicates the number of days that the I task can match. For (I = 1; I <= N; ++ I) {scanf ("% d", & W); sum + = W; For (j = 1; j <= match [I]; ++ J) g [I] [J] = W ;}} void solve () {printf ("% d \ n", Sum-km ());} int main () {// freopen ("stdin.txt", "r", stdin); int t; scanf ("% d", & T); While (t --) {getmap (); solve ();}}




 

Hdu1789 doing homework again [greedy]

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.