C language dynamic planning _ Doing Homework (HDU 1074)
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
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
Sample Output
2ComputerMathEnglish3ComputerEnglishMath
Question:
Now you need to have a variety of homework to catch up with. The time required by each assignment teacher is not the same as what you actually need, but you cannot do two homework at the same time. if one of the assignments cannot be handed in on time, one point will be deducted every day after the delay. Of course, this is also true for each assignment. Now you need to determine a sequence of homework to minimize the points.
The first act of each sample data is integer n, indicating that there are n other jobs to be done. The next n rows are the names of each job, the specified time and the actual time required. (Note: The assignment names entered in the question are alphabetically ordered by the first letter)
It is required to output the minimum score for the first row, and the next n rows are the order in which you perform the job. If there are two types of order with the minimum score, the output order is alphabetically ordered by the first letter of the job name.
This problem is caused by too many States because every job is not the same. It is difficult to write the state transfer equation.
Here we involve a new dp idea-state compression dp
Here, because each question is different, we cannot simply use the number of jobs completed as the number of States for recurrence. Here we want to classify the completion of each job as the status. because there are only two job states: finished and not done, we use binary as the status mark. for example, binary 101 indicates that the same job is finished with the third job, and the second job is not. then, the Fifteen jobs use 0 ~ 1 <15-1 can be completely enumerated.
The State compression State indicates that the binary is used, so some techniques of In-Place calculation are designed:
1. Obtain one or several fixed-position values of a binary number.
Assume that x = 1010 (10 in decimal format) is used to obtain the value of the second number from the right side of x. Then we can: x & (1 <1) then, judge whether the value is equal to 0 to know whether the second digit from the right of x is 0 or 1.
Promote to k-bit (x> = 1 <
Similarly, by judging the value of x & (3 <2), we can obtain the value of the third and fourth digits from the right of x.
2. Set one or more fixed positions of a binary number to zero.
Set the number k at the right of x to Zero x = x &(~ (1 <(k-1 ))).
Similarly, x = x &(~ (3 <2) The right side of x can be set to 0 at the position of 3rd and 4.
3. Reverse one or several fixed locations of a binary.
Returns the number k at the right of x. x = x ^ (~ (1 <(k-1 )));
Similarly, x = x ^ (~ (3 <2) returns the inverse of 3rd and 4 digits from the right side of x.
In this question. we can know that the status j = I-(1 <k), then the status I must be completed by j through the k-sample job. and I> j. when the I state is recursed, the previous status j must have been computed. if the problem is ineffective, we can use dp to solve the problem.
Code:
# Include
# Include
# Include
# Define INF 9999999 using namespace std; const int MAX = 1 <15 + 1; int dp [MAX], t [MAX], pre [MAX], all_t [20], fin_t [20]; // dp [I] is the minimum deduction point in the I state. t [I] is the time spent in the I state. pre [I] is the number of the precursor job that reaches the I state. char str [20] [2, 110]; void out (int x) {if (! X) return; out (x-(1 <
= 0; j --) {// here the reverse order is caused by the order of output in alphabetical order, where the I-1 <j reaches the I state through j, then I-1 <j must be calculated first than j. in fact, j arrived later. this should be self-realized. poor description. int step = 1 <
Dp [I-step] + score) {dp [I] = dp [I-step] + score; t [I] = t [I-step] + fin_t [j]; pre [I] = j ;}} printf ("% d \ n ", dp [tot-1]); out (tot-1);} return 0 ;}