首页 > ACM题库 > HDU-杭电 > HDU 1789 Doing Homework again-贪心-[解题报告] C++
2013
12-23

HDU 1789 Doing Homework again-贪心-[解题报告] C++

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

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1789

题意:n个作业,第i个作业有个deadline[i],过了deadline才完成需要罚score[i]分。每个作业完成需要1天。问最少要被扣多少分。

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 ;
}

解题报告转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/06/2315231.html


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