Week4 A Fear of DDL

Title description:

ZJM has n assignments, and each assignment has its own DDL. If ZJM does not complete this assignment before DDL, the teacher will deduct all the usual assignments for this assignment.
So ZJM wants to know how to arrange the order of doing homework, in order to deduct as little points as possible.

Input format:

The input contains T test cases. The first line of input is a single integer T, the number of test cases.
Each test case starts with a positive integer N (1 <= N <= 1000), indicating the number of jobs.
Then two lines. The first line contains N integers, indicating DDL, and the next line contains N integers, indicating points deducted.

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

Output format:

For each test case, the minimum total reduction score should be output, one line per test case.

Sample Output:

0
3
5

Sample description:

There are three sets of samples above.
For the first set of samples, there are three jobs whose DDL are all on the third day. ZJM makes one every day just before the DDL, so there is no penalty and the output is 0.
For the second set of samples, there are three assignments, and their DDLs are the first day, the third day, and the first day. ZJM did the first assignment on the first day and the second assignment the next day, with a total of 3 points deducted and output 3.

Ideas:

To minimize the points deducted, you should try to choose the homework with a larger score before DDL. Therefore, we sort the input jobs in descending order of scores, traverse from the beginning, and then start from their DDL. If there are no tasks scheduled for the day, they will be placed on the day. Arrived on the first day. Finally, add the remaining scores in the array to get the deducted score.

Code:

#include <iostream>
#include <algorithm>

using namespace std;

bool cmp(int *a,int *b)
{
	return a[1] > b[1];
}

int main()
{
	int t, n;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		int sum = 0;
		cin >> n;
		int **ddl = new int*[n];
		for (int j = 0; j < n; j++)
			ddl[j] = new int[2];
		for (int j = 0; j < n; j++)
			cin >> ddl[j][0];
		for (int j = 0; j < n; j++)
			cin >> ddl[j][1];
		sort(ddl, ddl+n, cmp);
		int *plan = new int[10000]();
		for (int j = 0; j <n; j++)
		{
			for (int k = ddl[j][0]; k >= 1; k--)
			{
				if (plan[k] == 0)
				{
					plan[k] = 1;
					ddl[j][1] = 0;
					break;
				}
			}
		}
		for (int j = 0; j < n; j++)
		{
			sum += ddl[j][1];
		}
		cout << sum << endl;
	}
	return 0;
}

Published 32 original articles · praised 0 · visits 690

Guess you like

Origin blog.csdn.net/qq_43814559/article/details/104978484
DDL