Hdu 1789 Greedy Algorithm

Source: Internet
Author: User

The general idea of this question is that to calculate how many points are deducted at least, we need to arrange the jobs with the most points deducted before the last time. If there are as many points to be deducted, it is necessary to schedule a tight period of time first. Therefore, the scores are first deducted in descending order. If the scores of the two courses are the same, the scores are sorted in descending order based on their end time! Then arrange it! [Cpp] # include <stdio. h> # include <stdlib. h> # include <string. h> # define N 1000 int f [N + 5]; struct T {int deadline; int score;} homework [N + 5]; int cmp (const void *, const void * B) {if (* (struct T *) ). score! = (* (Struct T *) B ). score) return (* (struct T *) B ). score-(* (struct T *) ). score; else return (* (struct T *) ). deadline-(* (struct T *) B ). deadline;} int main () {int C, n, I, j, sum; scanf ("% d", & C); while (C --) {memset (f, 0, sizeof (f); scanf ("% d", & n); for (I = 0; I <n; I ++) scanf ("% d ", & homework [I]. deadline); for (I = 0; I <n; I ++) scanf ("% d", & homework [I]. score); qsort (homework, n, sizeof (homework [0]), cmp); for (I = 0, sum = 0; I <n; I ++) {for (j = homework [I]. deadline; j> 0; j --) // check whether the event has been scheduled in the past few days from the deadline. {if (f [j] = 0) {f [j] = 1; break;} if (j = 0) // if there is no spare time until the end of the process, you can only deduct sum + = homework [I]. score;} printf ("% d \ n", sum);} 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.