In my last article said, is this, greedy practice, contrast can be found, another deduction chapter cumulative and finally must be all the work done, and this deduction is a one-time, so should be discarded deduction points small, so the structure of the order, go ahead and choose a minimum loss of the program directly exchange on it.
#include <stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespacestd;structhomework{intdeadline; intreduce;} hw[1005];BOOLmark[1005];intT;intN;intSearch (homework a[],intXintLen) { inti,pl=-1, min=x; for(i=0; i<len; i++) if(mark[i]==true&&a[i].reduce<min) {min=A[i].reduce; PL=i; } returnPl;}BOOLCMP (homework A, homework B) {if(a.deadline!=b.deadline)returna.deadline<B.deadline; Else returnA.reduce>b.reduce;}intMain () {scanf ("%d",&t); while(t--) {memset (Mark,0,sizeof(Mark)); memset (HW,0,sizeof(HW)); inti; scanf ("%d",&N); for(i=0; i<n; i++) scanf ("%d",&hw[i].deadline); for(i=0; i<n; i++) scanf ("%d",&hw[i].reduce); Sort (HW,HW+n,cmp); intday=0, reduced=0, TMP; for(i=0; i<n; i++) { if(day<hw[i].deadline) { Day++; Mark[i]=true; } Else { intex =search (hw,hw[i].reduce,i); if(ex!=-1) {tmp=Hw[ex].reduce; Hw[ex].reduce=Hw[i].reduce; Hw[i].reduce=tmp; } Reduced+=Hw[i].reduce; }} printf ("%d\n", reduced); } return 0;}
HDU 1789 Doing Homework again