杭电acm1789 Dong Homework again
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15648 Accepted Submission(s): 9126
Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
Output
For each test case, you should output the smallest total reduced score, one line per test case.
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
Solution
这是一个贪心问题,首先将作业按扣分大小从大到小排序先作扣分大的作业,然后从作业截止日期向前寻找空闲时间做作业
Code
/**
* date:2017.11.21
* author:孟小德
* function:杭电acm1789
* Doing Homework again 贪心算法
*/
import java.util.*;
public class acm1789
{
public static int[] date = new int[366];
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int num = input.nextInt();
boolean flag = false;
for (int i=0;i<num;i++)
{
for (int j=0;j<366;j++)
{
date[j] = 0;
}
int N = input.nextInt();
int[][] work = new int[N][2];
int result = 0;
// 输入作业截止时间
for (int j=0;j<N;j++)
{
work[j][0] = input.nextInt();
}
// 输入作业未做扣分数
for (int j=0;j<N;j++)
{
work[j][1] = input.nextInt();
}
sort(work);
for (int j=0;j<N;j++)
{
flag = false;
for (int k=work[j][0];k>0;k--)
{
if (date[k] == 0)
{
date[k] = 1;
flag = true;
break;
}
}
if (flag == false)
{
result += work[j][1];
}
}
System.out.println(result);
}
}
// 由扣分多少从大到小排序
public static void sort(int[][] work)
{
boolean flag = true;
int[] temp = new int[2];
for (int i = 0;i<work.length && flag;i++)
{
flag = false;
for (int j = 0;j<work.length-i-1;j++)
{
if (work[j][1] < work[j+1][1])
{
temp[0] = work[j][0];
temp[1] = work[j][1];
work[j][0] = work[j+1][0];
work[j][1] = work[j+1][1];
work[j+1][0] = temp[0];
work[j+1][1] = temp[1];
flag = true;
}
}
}
}
}