Week4: DDL fear - greed

Title Contents
ZJM there are n jobs, each job has its own DDL, if ZJM not done the job before the DDL, then the teacher will be deducted all the usual points of this job.

So ZJM want to know how to arrange the order of homework to buckle a bit less points as possible.

Input format
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 format
for each test case, you should reduce the output of the minimum total score, each test line.

Input Example

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

Output Example

0
3
5

Example Explanation

There are three sets of examples above.
For the first set of samples, there are three jobs DDL their third day, ZJM do all done in just a day before the DDL, so no points, 0 is output.
For the second set of sample, there are three jobs, they are DDL the first day, the third day, the first day. ZJM made on the first day of a job, a second job done the next day, a total of 3 points deducted, output 3.

Problem-solving ideas
This question is a simple greed.
First save all the latest DLL in a (large) at the time of input, that is the maximum time maxtime.
Then Pretreatment:

  • Sort all jobs
  • Priority arrangement according to the score, a relatively large fraction of the top surface
  • If the score is the same, that of course, is to finish it early DDL (DDL that is smaller top surface)

Each job for next time to go down again.
If you have time to meet current:

  1. Currently there is no other job that day occupied the
  2. On this basis, this job is not to DDL deadline (ie ddl smaller than the current time)

Then set the current time point is a job "occupied" (visit arrays), and then out of the current point traversal of schedule, continue to the next traverse a point.

If the current point can not find a job that used to write the day, then you have to deduct the points.

Finally, the output of all of the points the whole process down button on it.

AC Code

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

struct DDL
{
    int time;
    int score;
    DDL(int t, int s = 0)
    {
        time = t;
        score = s;
    }
};

bool cmp(const DDL& a, const DDL& b)
{
    if (a.score != b.score) return a.score > b.score;
    else return a.time < b.time;
}

vector<DDL>tasks;
int timetable[50000];

int main()
{
    int T, n;
    cin >> T;

    while (T--)
    {
        cin >> n;
        memset(timetable, 0, sizeof(timetable));
        tasks.clear();
        

        int temp;
        int maxtime = -1;
        for (int i = 0; i < n; i++)
        {          
            cin >> temp;
            tasks.push_back(DDL(temp));
            maxtime = max(maxtime, temp);
        }

        for (int i = 0; i < n; i++)
        {
            cin >> temp;
            tasks[i].score = temp;
        }

        sort(tasks.begin(), tasks.end(), cmp);
       
        int sum = 0;
        for (int i = 0; i < tasks.size(); i++)//遍历点
        {
            bool isok = false;
            for (int t = maxtime; t > 0; t--)//遍历时间表
            {
                if (timetable[t]) continue;
                else
                {
                    if (tasks[i].time < t) continue;
                    else
                    {
                        timetable[t] = 1;
                        isok = true;
                        break;
                    }
                }
            }

            if (!isok) sum += tasks[i].score;
        }

        cout << sum << endl;
    }
}
Published 21 original articles · won praise 3 · Views 416

Guess you like

Origin blog.csdn.net/qq_44506233/article/details/104979718
DDL