题目来源:HDU1789
好久没写题解了,最近有些忙,过几天的网络赛估计也要GG了。
题意及思路:给你n个任务,每个任务有一个花费和截止时间,如果你没在截止时间之前完成它就会累计这个任务所需要的花费,每天只能完成一个任务,求最小花费。
简单贪心,肯定是优先完成花费高的任务,按花费从大到小排序,同时用一个数组记录每天是否被占用(用来完成任务),如果被占用则向前继续查询,直到前面所有的天数都被占用,则加上这个任务的花费即可。
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
struct node
{
int cost;
int ddl;
};
bool cmp(node a, node b)
{
return a.cost > b.cost;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
node p[1005];
bool vis[1005];
memset(vis, false, sizeof(vis));
for (int i = 0;i < n;++i)
scanf("%d", &p[i].ddl);
for (int i = 0;i < n;++i)
scanf("%d", &p[i].cost);
sort(p, p + n, cmp);
int ans = 0;
for (int i = 0;i < n;++i)
{
int j = p[i].ddl;
while (j)
{
if (vis[j] == false)
{
vis[j] = true;
break;
}
j--;
}
if (j == 0)
ans += p[i].cost;
}
printf("%d\n", ans);
}
return 0;
}