hdu1074--Doing Homework(状压dp)

Doing Homework

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Submit Status

Description

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, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject‘s name, each string will at most has 100 characters) and two integers D(the deadline of the subject),
C(how many days will it take Ignatius to finish this subject‘s homework).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.

Output

For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.

Sample Input

 2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3 

Sample Output

 2
Computer
Math
English
3
Computer
English
Math 

Hint

In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order. 

给出了n门作业,每科做也的提交时间和需要时间,每超过一天罚1分,问最少罚几分,要求最小字典序

状态表示,在n科中做了哪几科,二进制数中第i个为1表示做了第i科,dp[i]表示状态为i时的最少罚时,和已经用了的分数

状态转移方程从科目为j的转移到科目为j+1的

为保证字典序最小,所以要让dp[i]尽量有更小的i来得到。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
struct node
{
    char s[120] ;
    int d , c ;
} p[20];
int state[20][7000] , num[20] , c[20] , cnt ;
int dp1[20][7000] , dp2[20][7000] , step[20][7000] ; //dp[i][j][]写i科作业在状态j时,0代表超出的时间,1代表需要的时间。
int find1(int x,int i)
{
    //printf("x == %d i == %d\n", x, i) ;
    int low = 0 , mid , high = num[i]-1 ;
    while( low <= high )
    {
        mid = ( low + high ) ;
        //printf("mid--%d\n", mid) ;
        if( state[i][mid] == x )
            return mid ;
        else if( state[i][mid] > x )
            high = mid - 1 ;
        else
            low = mid + 1 ;
    }
}
int main()
{
    int t , n , i , j , k , l , x , y , temp ;
    scanf("%d", &t) ;
    while( t-- )
    {
        memset(num,0,sizeof(num)) ;
        memset(dp1,-1,sizeof(dp1)) ;
        memset(step,-1,sizeof(step)) ;
        memset(dp2,-1,sizeof(dp2));
        scanf("%d", &n) ;
        for(i = 1 ; i <= n ; i++)
            scanf("%s %d %d", p[i].s, &p[i].d, &p[i].c) ;
        x = 1<<n ;
        for(i = 0 ; i < x ; i++)
        {
            for(j = 0 , temp = 0 ; j < n ; j++)
                if( (1<<j) & i )
                    temp++ ;
            state[temp][ num[temp]++ ]=  i ;
        }
        /*
        for(i = 0 ; i <= n ; i++)
        {
            for(j = 0 ;  j < num[i] ; j++)
                printf("%d ", state[i][j]) ;
            printf("\n") ;
        }*/
        dp1[0][0] = dp2[0][0] = 0 ;
        for(i = 0 ; i < n ; i++)
        {
            for(j = 0 ; j < num[i] ; j++)
            {
                for(k = 1 ; k <= n ; k++)
                {
                    if( 1<<(k-1) & state[i][j] )
                        continue ;
                    l = find1(state[i][j]+(1<<(k-1)),i+1) ;
                    //printf("l == %d\n", l ) ;
                    if( dp1[i+1][l] == -1 || ( dp1[i+1][l] > dp1[i][j] + max(dp2[i][j]+p[k].c-p[k].d,0) ) )
                    {
                        dp1[i+1][l] = dp1[i][j] + max(dp2[i][j]+p[k].c-p[k].d,0) ;
                        dp2[i+1][l] = dp2[i][j]+p[k].c ;
                        step[i+1][l] = j ;
                    }
                }
            }
        }
        /*for(i = 0 ; i <= n ; i++)
        {
            for(j = 0 ; j < num[i] ; j++)
                printf("%d ", dp[i][j][0]) ;
            printf("\n") ;
        }*/
        cnt = 0 ;
        for(i = n , j = 0 ; i > 0 ; i--)
        {
            x = state[i][j] ;
            y = state[i-1][ step[i][j] ] ;
            //printf("x = %d y = %d\n", x, y) ;
            for(k = 1 ; k <= n ; k++)
            {
                if( x % 2 != y % 2 )
                    break ;
                x /= 2 ;
                y /= 2 ;
            }
            c[cnt++] = k ;
            j = step[i][j] ;
        }
        int min1 = dp1[n][0] ;
        printf("%d\n", min1) ;
        for(i = cnt - 1 ; i >= 0 ; i--)
            printf("%s\n", p[c[i]].s ) ;
    }
    return 0 ;
}
时间: 2024-08-05 18:27:47

hdu1074--Doing Homework(状压dp)的相关文章

[HDU1074]Doing Homework (状压DP)

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 t

HDU 1074 Doing Homework(状压DP)

Problem Description 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 r

HDU 1074 Doing Homework 状压DP

Problem Description 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 r

HDU1074:Doing Homework(状压DP)

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15868    Accepted Submission(s): 7718 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a l

状压DP [HDU 1074] Doing Homework

Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5472    Accepted Submission(s): 2311 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lo

HDU 1074:Doing Homework(状压DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7704    Accepted Submission(s): 3484 Problem Description Ignatius has just come bac

HDU 1074 Doing Homework (状压dp)

题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则按照作业字典序输出(注意:输入也是按照字典序输入的) 题解:首先想到的是暴力dfs,但是会超时.接着我们看到n最大只有15,因此可以使用状压dp,但是状态不能用位置表示 但我们可以这样:0表示此作业没有做过,1表示已经用过了,接着遍历0->(1<<n)-1贪心(例如:3(011)可以找2(0

HDU 1074 Doing Homework DP 状压DP

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目描述: 给你所有课程的截止时间和工作时长, 一次只能做一种作业, 问最少罚时几天 N <= 15 解题思路: 由于N很小, 所以第一反应就是状压DP, 我们可以用一个15位二进制数来表示各个课程做完还是没做完, 然后从 S 从 1 到 1 << N 枚举 i 从 1 到 N 枚举, 如果S & (1<<i) 有效则说明i 属于情况 S, 这样我们从上一步S -

【状压DP】【HDOJ1074】

http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12269    Accepted Submission(s): 5911 Problem Description Ignatius has just come bac

ZOJ3305Get Sauce 状压DP,

状压DP的题目留个纪念,首先题意一开始读错了,搞了好久,然后弄好了,觉得DFS可以,最后超时,修改了很久还是超时,没办法看了一下n的范围,然后觉得状压可以,但是没有直接推出来,就记忆化搜索了一下,可是一直错,莫名奇妙,然后没办法看了一下题解,发现了下面这个比较好的方法,然后按照这个方程去推,然后敲,也是WA了好多把,写的太搓了,没人家的清楚明了,唉~也算是给自己留个纪念,状压一直做的都不太好~唉~还好理解了, 参考了  http://blog.csdn.net/nash142857/articl