HDU 1789 doing homework again (Greedy Algorithm)

Source: Internet
Author: User
Doing homework again

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


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


The greedy algorithm for recommendlcy is relatively simple and easy to think, it is to sort the deducted scores from small to large, and then start from scratch greedy from the deadline in each position to find the time not occupied to do this job so greedy to ensure that the time is as far back as possible, this prevents the previous jobs from having no time to do, and the second job is greedy for higher points. This is why the scores are sorted from large to small.

#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;struct point{    int re;    int deadline;}po[1500];bool rec[1500];int cmp(const void *a,const void *b){    return (*(point*)a).re < (*(point*)b).re ? 1 : -1;}int main(){    int t;    int n,i,j;    scanf("%d",&t);    int ans;    while(t--)    {        scanf("%d",&n);        for(i=0;i<n;i++)        scanf("%d",&po[i].deadline);        for(i=0;i<n;i++)        scanf("%d",&po[i].re);        qsort(po,n,sizeof(po[0]),cmp);        memset(rec,1,sizeof(rec));        ans=0;        for(i=0;i<n;i++)        {            for(j=po[i].deadline;j>=1;j--)            if(rec[j])            { rec[j]=false;break;}            if(j==0)            ans+=po[i].re;        }        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.