贪心
因为要尽可能少的扣学分,所以肯定是要按学分的高低来进行排序
贪心的最优情况是:刚好在最后一天完成相应的任务
所以本着这个情况,设置了一个vis[]数组 表示第i天是否有任务预约(待完成)
于是先按照贪心排序,完了从头到尾遍历,
eg:3 3
7 2
如果第三天没有任务预约,那就vis[3]=true,任务一(学分7的)设为完成。
再看任务二,因为第三天有预约,所以看前一天vis[2]是否有预约,没有 ,true,任务完成。
如果一直到第0天任务都没完成,那就舍弃该任务咯。
代码:
package _1789;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
class Node implements Comparable<Node>{
int e,v; //endline value
boolean f; //finish
Node(int a) {
e=a;
f = false;
}
public int compareTo(Node o) {
if(v==o.v) return e-o.e;
return -(v-o.v);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- >0){
List<Node> list = new LinkedList<Node>();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int e = sc.nextInt();
list.add(new Node(e));
}
for (int i = 0; i < n; i++) {
int v = sc.nextInt();
list.get(i).v=v;
}
Collections.sort(list);
boolean vis[] = new boolean[1005];
for (int i = 0; i < list.size(); i++) {
for (int j = list.get(i).e; j >= 1; j--) {
if(!vis[j]){
vis[j] = true;
list.get(i).f = true;
break;
}
}
}
int ans = 0;
for (int i = 0; i < list.size(); i++) {
if(!list.get(i).f){
ans += list.get(i).v;
}
}
System.out.println(ans);
}
}
}
Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15522 Accepted Submission(s): 9051
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.
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
Author
lcy
Source
Recommend
lcy