贪心法应该是最优的。。没什么算法,排序遍历下就结束了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Loan
{
int profit;
int dead;
};
static int cmp(const void *p1, const void *p2)
{
struct Loan *l1 = (struct Loan*) p1;
struct Loan *l2 = (struct Loan*) p2;
if (l1->profit != l2->profit)
return l2->profit - l1->profit;
else
return l2->dead - l1->dead;
}
int main()
{
int n, l, time[10001];
while (scanf("%d %d", &n, &l) != EOF)
{
struct Loan *array = malloc(n * sizeof(struct Loan));
int i, j;
for (i = 0; i < n; i++)
scanf("%d %d", &(array[i].profit), &(array[i].dead));
qsort(array, n, sizeof(struct Loan), cmp);
if (!l)
puts("0");
else
{
for (i = 0; i <= 10000; i++)
time[i] = l;
int total = 0;
for (i = 0; i < n; i++)
for (j = array[i].dead; j >= 0; j--)
if (time[j])
{
time[j]--;
total += array[i].profit;
break;
}
printf("%d\n", total);
}
free(array);
}
return 0;
}