2013
12-23

# Doing Homework again

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.

For each test case, you should output the smallest total reduced score, one line per test case.

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

2011-12-19 08:22:18

mark：贪心orDP，that’s a problem。想dp没YY出来。看了别人的思路，贪心水过。

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

typedef struct node{
int dl, sc ;
}node ;
typedef node *pnode ;

node a[1010] ;
int flag[1010] ;

int min(int a, int b){return a<b?a:b ;}
int cmp(const void *a, const void *b)
{
pnode p = (pnode)a, q = (pnode)b ;
if (p->sc != q->sc) return q->sc - p->sc ;
return p->dl - q->dl ;
}

int main ()
{
int i, j, T ;
int cur, ans, n ;
scanf ("%d", &T) ;
while (T--)
{
scanf ("%d", &n) ;
for (i = 0 ; i < n ; i++)
{
scanf ("%d", &a[i].dl) ;
if (a[i].dl >1010)
a[i].dl = 1010 ;
}
for (i = 0 ; i < n ; i++)
scanf ("%d", &a[i].sc) ;
qsort(a, n, sizeof(node),cmp) ;
ans = 0 ;
memset (flag, 0, sizeof(flag)) ;
for (i = 0 ; i < n ;i++)
{
for (j = a[i].dl - 1 ; j >= 0 ; j--)
{
if (flag[j] == 0)
{
flag[j] = 1 ;
break ;
}
}
if (j == -1) ans += a[i].sc ;
}
printf ("%d\n", ans) ;
}
return 0 ;
}

1. 有限自动机在ACM中是必须掌握的算法，实际上在面试当中几乎不可能让你单独的去实现这个算法，如果有题目要用到有限自动机来降低时间复杂度，那么这种面试题应该属于很难的级别了。