【杭电oj】1789 - Doing Homework again(贪心,并查集)

39 篇文章 0 订阅
25 篇文章 0 订阅

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10545    Accepted Submission(s): 6179


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
 

Author
lcy
 

Source


思路同 http://blog.csdn.net/wyg1997/article/details/50612126 的思路,其分数就是其价值,应该优先考虑高价值的,并查集在这里表示其最近能安排的天数(感觉这个find函数很好,只是时间长容易忘)。


2016.7.22补充:下面又贴出一个比较一般的 find()函数的写法,那个比较好记,注意初始化的时候不能初始化到 n ,应该是全体日期。


代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct node
{
	int day,g;
}data[1011];

bool cmp(node a,node b)
{
	return a.g > b.g;
}

int f[1011];		//根表示最近空闲的天

int find (int x)
{
	if (f[x] == -1)		//若当前时间没标记,则当前空闲
		return x;
	else
	{
		f[x] = find (f[x]);		//路径压缩 
		return f[x];
	}
}
int main()
{
	int u;
	int n;
	int ans,sum;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%d",&n);
		ans = sum = 0;
		memset (f,-1,sizeof (f));
		for (int i = 1 ; i <= n ; i++)
			scanf ("%d",&data[i].day);
		for (int i = 1 ; i <= n ; i++)
		{
			scanf ("%d",&data[i].g);
			sum += data[i].g;
		}
		sort (data + 1,data + 1 + n,cmp);
		for (int i = 1 ; i <= n ; i++)
		{
			int t = find (data[i].day);
			if (t > 0)		//表示之前还有时间安排
			{
				ans += data[i].g;
				f[t] = t - 1;		//时间往前推移一天 
			}
		}
		ans = sum - ans;
		printf ("%d\n",ans);
	}
	return 0;
}



今天(2016.7.22)又写了一次,然后连着wa了四次,最后发现是初始化的问题。

现在贴一个比较好记的 find() 函数的写法,跟普通并查集一样。


代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
int f[1000+11];
struct node
{
	int t,g;		//终止时间,分数 
}data[1111];
int find(int x)
{
	if (f[x] != x)
		f[x] = find(f[x]);
	return f[x];
}
bool cmp (node a , node b)
{
	return a.g > b.g;		//分数大的优先
}
int main()
{
	int u;
	int n;
	int ans;
	scanf ("%d",&u);
	while (u--)
	{
		ans = 0;
		scanf ("%d",&n);
		for (int i = 1 ; i <= n ; i++)
			scanf ("%d",&data[i].t);
		for (int i = 1 ; i <= n ; i++)
			scanf ("%d",&data[i].g);
		for (int i = 1 ; i <= 1000 ; i++)		//切记不能初始化到n 
			f[i] = i;
		sort (data + 1 , data + n + 1 , cmp);		//按分值排序 
		for (int i = 1 ; i <= n ; i++)
		{
			int day = find (data[i].t);		//看看截止当前之前能不能有没有时间 
			if (day > 0)
				f[day] = day - 1;		//就这这一天完成,日期往前缩一天 
			else
				ans += data[i].g;
		}
		printf ("%d\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值