本来以为不是贪心,后来看了一下分类,属于贪心,就开始乱搞了。原来只需要模拟一下把课程放到课程表就是。
思路是,有一张空的日程表,有一张任务表。任务按照权重排序。在任务表中选出权重最大的,填在日程表的那个任务对应的那个日子上,如果那个日子已经填了一个数字,则这个数字必定比选定的任务的权重大,那就在日程表中向前找,找到一个没有填数字的日子,把权重填进去。不必担心下次来的没有地方填,因为下次来的权重肯定没有这次的大,如果要选的话,也是选这次权重大的。重复上述步骤,知道把 1 ~ deadline 号的日程表都填满,就结束。最后算一下全部课程的 score ,填了的日程所获得多少 score ,相减得到丢失的 score 。
具体代码如下:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define MAXN 1005
using namespace std;
struct Node
{
int deadline;
int score;
bool operator<(Node o) const
{
if(score == o.score)
{
return deadline < o.deadline;
}
else
{
return score > o.score;
}
}
} node[MAXN];
int map[MAXN];
void Set(int day, int score)
{
while(day > 0)
{
if(map[day] == 0)
{
map[day] = score;
return ;
}
else
{
day --;
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t --)
{
memset(map, 0, sizeof(map));
int i, n;
scanf("%d", &n);
int total = 0;
int max_deadline = -1;
for(i = 0; i < n; i ++)
{
scanf("%d", &node[i].deadline);
if(node[i].deadline > max_deadline)
{
max_deadline = node[i].deadline;
}
}
for(i = 0; i < n; i ++)
{
scanf("%d", &node[i].score);
total += node[i].score;
}
sort(node, node + n);
for(i = 0; i < n; i ++)
{
Set(node[i].deadline, node[i].score);
}
int get = 0;
for(i = 1; i <= max_deadline; i ++)
{
get += map[i];
}
printf("%d\n", total - get);
}
return 0;
}