Fear programming thinking week4 operation of A-DDL

topic

ZJM there are n jobs, each job has its own DDL, if ZJM not done the job before the DDL, then the teacher will deduct this job all the usual points.
So ZJM want to know how to arrange the order of homework to buckle a bit less points as possible.

Input

Input of T test. The first input line is a single integer T, was found in the number of test cases.
Each test case to begin a positive integer N (1 <= N <= 1000), indicates the number of jobs.
Then two lines. The first line contains N integers representing the DDL, the next row containing N integers representing buckle points.

Output

For each test case, you should reduce the output of the minimum total score, each test line.

Sample Input

3
3
3 3 3
10 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

Thinking

Using greedy algorithm.
The method is easy to think that the task will be sorted in descending order according to points, followed by traversal, to arrange a time to the task. For the first task i ddl traverse forward from the task, if found free time is scheduled on this day. Obviously, this complexity is O (n ^ 2), less than the amount of data can be processed 1000.
So, is there less complexity method?
From the back enumerate every day to one day per schedule tasks. The first day to enumerate x, x is greater than all ddl task in accordance with points descending order, the first task will be sorted scheduled day x. The sorting task, selecting a complexity of O (logn) data structure - large root stack. So this method is the complexity of O (nlogn).

Code

#include <cstdio>
#include <algorithm>
using namespace std;

struct work{
    int ddl,score;
    bool operator<(work w)const{
        if(ddl!=w.ddl)
            return ddl>w.ddl;
        else
            return score>w.score;
    }
}w[2000];

bool cmp(work a,work b){
    if(a.score!=b.score)
        return a.score>b.score;
    else
        return a.ddl>b.ddl;
}

int main() {
    int t,n,maxDDL,total,sum;
    scanf("%d",&t);
    for(int k=0;k<t;k++){
        maxDDL=0;total=0;sum=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&w[i].ddl);
            if(w[i].ddl>maxDDL)
                maxDDL=w[i].ddl;
        }
        for(int i=0;i<n;i++){
            scanf("%d",&w[i].score);
            total+=w[i].score;
        }
        sort(w,w+n);
        int j=0;
        for(int i=maxDDL;i>0;i--){
            while(j<n&&w[j].ddl>=i){
                j++;
            }
            sort(w,w+j,cmp);
            sum+=w[0].score;
            w[0].score=0;
        }
        printf("%d\n",total-sum);
    }
    return 0;
}

Topic Link

Released nine original articles · won praise 2 · Views 145

Guess you like

Origin blog.csdn.net/weixin_43805228/article/details/104927606