贪心:
题目意思:
有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
*/