(贪心)HDU 1789 解题报告

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7100    Accepted Submission(s): 4209

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

思路:

既然要让被扣掉的分数最少,那么必然是对分数高的作业优先安排。注意题中有一个不是很明显的条件可以支持这一点:完成每份作业都需要一天。这样就避免了优先完成一份分数高的作业而导致n(n>1)份作业没有完成,而且这n份作业分数和比一份分数高的作业还要大的情况。

方法:

对于所有的作业,按照分数从高到低排序,分数相同时,截止时间小的排在前面。另外初始化一个大小为n的数组,用来保存某一天是否已经被占用。然后开始贪心,对于每份作业,看从当天到当前之前的时间里面,有没有空余,如果有空余,安排到空余且天数最大的一天;如果没有空余,这份作业只能被扣分。注意这边天数从1开始,而代码中习惯数组从0开始,天数减1或者数组加1都可以。

参考代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
    int deadline;
    int score;
};
bool cmp(node a, node b){
    if(a.score==b.score){
    	return a.deadline<b.deadline;
    }
    return a.score>b.score;
}
node homework[1002];
int used[1002];
int main(){
	int t, n, ans;
	scanf("%d", &t);
	while(t--){
		scanf("%d", &n);
		for(int i=0;i<n;i++){
			scanf("%d", &homework[i].deadline);
		}
		for(int i=0;i<n;i++){
			scanf("%d", &homework[i].score);
		}
		sort(homework, homework+n, cmp);
		for(int i=0;i<n;i++){
			used[i] = 0;
		}
		ans = 0;
		for(int i=0;i<n;i++){
			bool flag = false;
			for(int j=homework[i].deadline-1;j>=0;j--){
				if(used[j]==0){
					used[j] = 1;
					flag = true;
					break;
				}
			}
			if(!flag){
				ans += homework[i].score;
			}
		}
		printf("%d\n", ans);
	}
}
时间: 2024-08-01 22:47:10

(贪心)HDU 1789 解题报告的相关文章

hdu 2112 HDU Today 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目意思:又是求最短路的,不过结合埋字符串来考查. 受之前1004 Let the Balloon Rise 学到用map 的解法做之后,有点蠢蠢欲动,当时见到要用字典树做有点吓坏了(之前看过下,非一般难理解,所以暂时放下了).于是,死就死吧,硬住头皮用map做,反反复复修改终于过了. 首先是memory limit exceeded,因为无读清题目意思,直接开10000 * 10000的数组

BestCoder17 1002.Select(hdu 5101) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有相应的人,每个人又有相应的IQ.现在要求从这些classes挑选两个人,满足IQ之和 > k,不过要满足这两个人不能来自同一个class的. 解题思路不难想出,直接所有人两两之和 > k - 同一个class 两两之和 > k  就是答案了. 不过很容易超时!!!! 用二分就可以过了.二分有

BestCoder18 1002.Math Problem(hdu 5105) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得这个函数 f(x)= |a∗x3+b∗x2+c∗x+d| 最大. 我一开始做的时候,很天真的认为数据量这么小,一个一个试,暴力搜就肯定能得到答案啦.但是一个很严重的问题是,x 没有说是实数还是整数,所以枚举根本不可行. 于是就联想到应该使用高中求一元三次方程的方法来做.我当时是直接从 f(l), f

BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是1 2(下标为2 3),而第一长递增子序列也是(下标为 1 3). 我一开始天真的以为,还是利用求最长递增子序列的算法啦,第二长不就是对dp 数组sort完后(从小到大)的dp[cnt-1] 啦,接着就呵呵啦----= = 题解说,要加多一个 dp 数组,以便对当前下标值为 i 的数 a[i] 为结

BestCoder12 1002.Help him(hdu 5059) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否是合法的整数,如果是的话问是否在[a, b] 范围内,是则输出 YES,否则输出 NO 合法的整数:(1)非负整数:只有数字,没有前导0 (2)负数:负号后面只能跟着数字,负号前没有任何字符 首先这条题感觉不是出得太好,不过都是硬着头皮学人家做啦.反正一些很变态的数据可能还是过不了,但却AC的. 模

BestCoder10 1001 Revenge of GCD(hdu 5019) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公约数依次为1 2 4 8,那么第 3 个 GCD(X, Y) = 2,也就是从后往前数第3个公共因子. TLE思路:求出 X 的所有因子(从小到大开始存储),再从后往前枚举这些因子,检查是否也为 Y 的因子,统计到第 K 个数就是答案了......以为可以顺利通过,原来数据量还是非常大滴!!! 正确

BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A, f[2] = B),通过这条式子f[n] = f[n-1] + f[n-2],问 C 是否在这条 new Fibonacci sequence 内.(1 <= A, B, C <= 1 000 000 000) 首先,要想到 C 有可能是 A 或者 B,这种情况也是属于在这个序列范围内的. 还

BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数,中位数是m的子序列有多少个. 我的做法被discuss 中的测试数据一下子就否定了. 这个是别人的做法,暂时留下来,有些地方还没真正弄懂,应该是缺了部分的知识没有学到... 留着先: (1)http://blog.csdn.net/hcbbt/article/details/38377815 (2

BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊----- 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1