题目链接:[HDU1789]Doing Homework again[贪心]
题意分析:Ignatius打比赛归来,结果老师布置了作业,每份作业都需要一天来完成,并且老师还好心地给了截至日期,要是超过截至日期上交,就要扣相应的分,请问怎样使扣分最少?最少是多少?
解题思路:贪心规则就是:扣分高的先完成。至于怎么个完成法呢,先取出门作业,看看在截至日期前是否还有时间可以做,没有时间做的话,只能加入罚分了。
个人感受:扣分高的贪,到底怎么贪呢,困惑了半天TAT。实现能力还有待加强。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int MAXN = 1110;
struct Lesson{
int day;
int score;
bool operator < (const Lesson& t)const
{
if (score == t.score) //分数相同,优先截至时间近的
return day < t.day;
else return score > t.score; //按分数高的排列
}
}lesson[MAXN];
bool used[MAXN];
int main() {
int t; scanf("%d", &t);
while (t --)
{
memset(used, 0, sizeof used);
int n; scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &lesson[i].day);
for (int i = 0; i < n; ++i) scanf("%d", &lesson[i].score);
sort(lesson, lesson + n);
int ans = 0, j;
for (int i = 0; i < n; ++i)
{
for (j = lesson[i].day; j > 0; --j)
{
if (!used[j]) //有没有日期空出可以写作业
{
used[j] = 1;
break;
}
}
if (j == 0) //没有就只能加入罚时了
ans += lesson[i].score;
}
printf("%d\n", ans);
}
return 0;
}