Acm--doing homework again (greedy algorithm)

Source: Internet
Author: User
The main idea: a classmate to complete the homework assigned by the teacher. Each job has a deadline and corresponding deduction points. If the deadline is not completed, the corresponding points will be deducted. Q: How to arrange the order of homework to make the total score of the deduction minimum (1 days to complete a job)Idea: According to the greedy thought, priority to deduct a lot of homework, first deal with them. To do this, sort the jobs in descending order by the number of snaps. From the most points of the job start, it is scheduled to the deadline of its completion day, if the day has been occupied, then move forward one day, if the previous days are occupied, then this job can only give up, must be deducted points.
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace
Std
	struct Node {int deadline;
    
int score;

}A[1002];

int isdone[2000];


BOOL CMP (node A,node b) {return a.score>b.score;}
    int main () {int case,n;
    cin>>case;
        while (case--) {memset (isdone,0,sizeof (IsDone));
        cin>>n;
        for (int i=1;i<=n;i++) cin>>a[i].deadline;
        for (int i=1;i<=n;i++) cin>>a[i].score;
        Sort (a+1,a+n+1,cmp);
        int sum=0;
            for (int i=1;i<=n;i++) {int flag=0; for (int j=a[i].deadline;j>=1;j--) {if (!
                    Isdone[j]) {isdone[j]=1;
                    flag=1;
                Break
        }} if (!flag) Sum+=a[i].score;
    } cout<<sum<<endl;
} 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.