HDU 1789 Doing homework again greedy algorithm

Source: Internet
Author: User
Tags bool
Doing Homework again

Time limit:1000/1000 MS (java/others)

Memory limit:32768/32768 K (java/others)

Problem Description Ignatius have just come back school from the 30th ACM/ICPC. Now he had 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 would reduce his score of the final Test. And now we assume this doing everyone homework always takes one day.   So Ignatius wants-to-help him to arrange the order of doing homework to minimize the reduced score. Input the input contains several test cases. The first line of the input was a single integer T so 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 this indicate the deadlines of the subjects, and the next line contains N integers that Indicate the reduced scores.
Output for each test case, you should output of the smallest total reduced score, one line per test case.
Sample Input

3 3 3 3 3 5 1 3 1 3 1 6 2 3 7 1 4 6 4 2 4 3 3 2 1 7 6 5 4 Sample Output
0 3 5 Problem Description:Ignatius after the game back, each teacher gave Ignatius a deadline to hand in the homework, if not pay off points. Each job must be completed in one day, the minimum number of points to deduct. First enter a T to indicate that there is a T group of test data, then each group of data first entered an n, representing n jobs, and then enter two lines, the first line represents the date of each job to be delivered, the second line indicates the corresponding if not to pay the job to buckle points. Outputs the minimum score to buckle. Problem Solving Ideas:First, the date from small to large sort, if the date is the same, the deduction of more than the row in front. If there is a deduction of more than the same date, then use the front to do the work of less time to do this job, if there is no smaller than him, the branch of the job. AC Code:
#include <stdio.h> #include <algorithm> using namespace std;
    struct work{int sco;
    int time; BOOL Flag;

Flag 1 indicates that the}a[1005] can be completed;
    BOOL Comp (Work A1, work A2) {if (a1.time! = a2.time) return A1.time < A2.time;
return A1.sco > A2.sco;
    } int main () {int T, n, I, J;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        for (i = 0; i < n; i++) scanf ("%d", &a[i].time);
            for (i = 0; i < n; i++) {scanf ("%d", &a[i].sco);
        A[i].flag = true;
        } sort (A, a+n, comp);
        int sum = 0, k = 1;
                for (i = 0; i < n; i++) {if (A[i].time >= k) {//) if the day of the job to be completed is after the current date, the job k++ can be completed;
            Continue
            } int p = A[i].sco, pos = i;
                    for (j = 0; J < i; j + +) if (A[j].sco < P && a[j].flag) {p = A[j].sco;
          pos = j;      } sum + = P;
        A[pos].flag = false;
    } 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.