hdu 1074 Doing Homework【状压dp】好题

Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8284    Accepted Submission(s): 3828

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 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个作业,对应每个作业都有一个截止日期,对应要完成这个作业也需要对应的天数来完成,当一个作业如果在截止日期之后x天才提交,那么对应就要扣掉x分,问一个最小扣分分配的方案,如果有多种方案,对应输出字典序最小的那个。


思路:


1、首先观察到这个题的N并不大,只有15,那么考虑到状态压缩。

贪心可能会有疏漏点,考虑dp求解。


2、对应dp求解有两个问题:

①如何解决这个提交作业的顺序的问题,状态压缩只能压缩这个对应当前位的事物的有无,但是确定不了其顺序。

②如何解决这个扣除分数的问题。

我们首先来解决第一个问题:

①我们既然需要一个提交作业的顺序,那么我们对应将dp数组升一个维度,设定dp【i】【j】表示当前完成作业情况为j,并且最后一个做完的作业是第i个作业对应的最小花费。那么我们不难递推出其状态转移方程:dp【i】【j】=dp【k】【q】+扣除的分数值。这里k表示倒数第二个完成的作业。那么此时我们就有了一个顺序。

②然后我们解决这个扣除的分数的值的问题,我们对应解决这个问题设定一个数组sum【i】表示搞定了状态i的作业的时候,一共需要的用时。那么此时对应如果已经枚举出了这个四个值i,j,k,q,我们再设定一个变量tmp表示当前最后做这第i个作业需要扣除多少花费。那么这里tmp=第i个作业的截止日期-sum【j】;如果tmp现在的值大于0,那么设定tmp=0,相反,如果tmp的值小于0,那么对应tmp=-tmp;然后我们得到了此时状态转移需要扣除的分数值。


3、最终其解在dp【i】【(1<

然后我们回溯记录答案即可。

注意这里需要字典序最小,因为我们是从最后一个作业向前找答案,那么当前答案的字典序越大越好,使得越靠前提交的作业的字典序对应也就越小。


4、注意数组的初始化、注意数组大小,其他的就没有什么了。


Ac代码:


#include
#include
#include
using namespace std;
struct node
{
    char s[155];
    int x,y;
} a[155];
int sum[(1<<17)];
int dp[17][(1<<17)];
int ans[17];
int n;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i=0)tmp=0;
            else tmp=-tmp;
            dp[i][(1<=0; i--)
            {
                if((j&(1<=0; k--)
                    {
                        if((j&(1<=0)tmp=0;
                            else tmp=-tmp;
                            dp[i][j]=min(dp[i][j],dp[k][q]+tmp);
                        }
                    }
                }
            }
        }
        int output=0x3f3f3f3f;
        int pos;
        for(int i=n-1; i>=0; i--)
        {
            if(dp[i][(1<=0;i--)
            {
                if(dp[i][tmp]=0; i--)
        {
            printf("%s\n",a[ans[i]].s);
        }
    }
}



你可能感兴趣的:(dp)