HOJ 1789 Doing Homework again(贪心)

9 篇文章 0 订阅

贪心:
题目意思:
有n门作业,每一门作业都有最后交作业日期 d 和 惩罚分数(不交或者延迟交就被扣分)。
学生一天只能做一门作业。问经过合理的安排,学生被罚的分数最少。
解题思路:
要求扣分最少,则将扣的分从大到小排序,先安排扣分最多的作业,如第i个课程,以该课程结束时间为初始点,
向前扫描,看它能安排到第几天(每个课程尽量在接近截止日期完成,贪心)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MaxN = 1010;
int T, n;
bool vis[MaxN];

struct node
{
	int d;
	int score;
	bool operator<(const node& rhs) const
	{
		if(score == rhs.score)
		{
			return d < rhs.d;
		}
		return score > rhs.score;
	}
}hwork[MaxN];

void solve()
{
	sort(hwork, hwork + n);
	memset(vis, false, sizeof vis);
	int y, ans = 0;
	bool flag = false;
	for(int i = 0; i < n; ++i)
	{
		flag = false;
		y = hwork[i].d;
		while(y) 
		{
			if(!vis[y])	
			{
				vis[y] = true;	//这天做作业 hwork[i]	
				flag = true;
				break;
			}
			--y;
		}
		if(!flag)
		{
			ans += hwork[i].score;
		}
	}
	printf("%d\n", ans);
}

int main()
{
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(int i = 0; i < n; ++i)
		{
			scanf("%d", &hwork[i].d);
		}
		for(int i = 0; i < n; ++i)
		{
			scanf("%d", &hwork[i].score);
		}
		solve();
	}
	return 0;
}

/*
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
*/

/*
0
3
5
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值