Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 3958 Accepted Submission (s): 1577
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 specified CED 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 shocould output the smallest total CED 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
Word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.
Analysis: State compression, which is expressed in binary; 1 indicates yes; 0 indicates no.
F [1 <n-1] indicates that all binary bits in the final state are 1.
The difficulty lies in finding the previous state to push the current state to be calculated.
Of course, it is easy to know that for a state f [S], its previous state is f [Ki], and {Ki is 1 less than S in binary space}
[Cpp] view plaincopyprint?
# Include <stdio. h>
# Include <string. h>
# Define MAXN 16
# Define INF 0x7fffffff
Struct tt {
Int time, deadline;
Char name [105];
} Hw [MAXN];
Struct t {
Int pre, now;
Int score, time;
T () {pre =-1 ;}
} Dp [1 <MAXN];
Void print (int k)
{
If (dp [k]. pre! =-1)
{
Print (dp [k]. pre );
Printf ("% s \ n", hw [dp [k]. now]. name );
}
}
Int main ()
{
Int T, n, s, I, recent, past, reduce, j, max;
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n );
For (I = 0; I <n; I ++)
Scanf ("% s % d", & hw [I]. name, & hw [I]. deadline, & hw [I]. time );
Max = 1 <n;
For (s = 1; s <max; s ++ ){
Dp [s]. score = INF;
For (I = n-1; I> = 0; I --){
Recent = 1 <I;
If (s & recent ){
Past = s-recent;
Reduce = dp [past]. time + hw [I]. time-hw [I]. deadline;
If (reduce <0)
Reduce = 0;
If (reduce + dp [past]. score <dp [s]. score ){
Dp [s]. score = dp [past]. score + reduce;
Dp [s]. now = I;
Dp [s]. pre = past;
Dp [s]. time = dp [past]. time + hw [I]. time;
}
}
}
}
Printf ("% d \ n", dp [max-1]. score );
Print (max-1 );
}
Return 0;
}
# Include <stdio. h>
# Include <string. h>
# Define MAXN 16
# Define INF 0x7fffffff
Struct tt {
Int time, deadline;
Char name [105];
} Hw [MAXN];
Struct t {
Int pre, now;
Int score, time;
T () {pre =-1 ;}
} Dp [1 <MAXN];
Void print (int k)
{
If (dp [k]. pre! =-1)
{
Print (dp [k]. pre );
Printf ("% s \ n", hw [dp [k]. now]. name );
}
}
Int main ()
{
Int T, n, s, I, recent, past, reduce, j, max;
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n );
For (I = 0; I <n; I ++)
Scanf ("% s % d", & hw [I]. name, & hw [I]. deadline, & hw [I]. time );
Max = 1 <n;
For (s = 1; s <max; s ++ ){
Dp [s]. score = INF;
For (I = n-1; I> = 0; I --){
Recent = 1 <I;
If (s & recent ){
Past = s-recent;
Reduce = dp [past]. time + hw [I]. time-hw [I]. deadline;
If (reduce <0)
Reduce = 0;
If (reduce + dp [past]. score <dp [s]. score ){
Dp [s]. score = dp [past]. score + reduce;
Dp [s]. now = I;
Dp [s]. pre = past;
Dp [s]. time = dp [past]. time + hw [I]. time;
}
}
}
}
Printf ("% d \ n", dp [max-1]. score );
Print (max-1 );
}
Return 0;
}