Doing Homework
-
原题
Time Limit:1s
Memory Limit:32768K
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(截止日期,不交作业就会dead对吧) 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分,要求最少要扣几分
(有T组测试数据)
-
Solution
(1<=N<=15)明晃晃的状压DP(状压DP专题里来的,逃
我们对于每个状态,都可以向后增加作业,同时保存顺序和已经扣掉的分数,当前的用时只需要预处理即可
那么问题来了,怎么保存顺序,使整个序列字典序最小呢(一本正经,其实直接用数组也是可以的啊)但是会很麻烦(本蒟蒻比较懒),于是想到=437893890380859375,=9223372036854775807,珂以用long long存下!那就偷懒吧...
-
Code
由于有些数组没开LL调了很久,一怒之下就全开了LL,见谅:)
#include <cstdio>
#include <iostream>
#include <cstring>
#define N 16
#define LEN 105
#define LL long long
#define INF 1000000000000000000
using namespace std;
LL n, d[N], c[N], order[1 << N], poi[1 << N], ti[1 << N];//order顺序,poi扣的分,ti时间
char name[N][LEN];
void print(LL x, LL p) {//把order转化并输出
if (p > 0) print(x / n, p - 1);
cout << name[x % n] << endl;//名字按升序给出,直接输出
}
int main() {
LL t;
scanf("%lld", &t);
while (t--) {
scanf("%lld", &n);
for (LL i = 0; i < n; ++i) {
cin >> name[i] >> d[i] >> c[i];
}
LL k = 0;
for (LL i = 1; i < (1 << n); ++i) {//预处理
poi[i] = INF;
if (i == 1 << k) {//只有一项作业
ti[i] = c[k];
order[i] = k;
k++;
}
else {
LL o = i & -i;
ti[i] = ti[o] + ti[i - o];
order[i] = order[o] + order[i - o] * (LL)n;//最大顺序
}
}
for (LL i = 0; i < (1 << n); ++i) {
for (LL j = 0; j < n; ++j) {
if ((i & (1 << j)) == 0) {
LL ne = i | (1 << j);//向后转移的新状态
LL add = max(ti[ne] - d[j], (LL)0);//需要增加的扣掉的分数
LL ord = order[i] * n + j;//顺序吖
if (poi[ne] > poi[i] + add || poi[ne] == poi[i] + add && ord < order[ne]) {//poi为第一关键字,order为第二关键字
poi[ne] = poi[i] + add;
order[ne] = ord;
}
}
}
}
LL end = (1 << n) - 1;//全为1的状态,即所有作业都完成
printf("%lld\n", poi[end]);
print(order[end], n - 1);
}
}