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.
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
/*
顶多只有15科作业。
此题看题解的,很牛B。
用一个二进制数存储这N份作业的完成情况,第1到n个作业状况分别
对应二进制数的0,1,---n-1。。
数字上限为2^n.
其中2^n-1表示作业全部完成,0表示作业全部没有完成
也就是现在有2^n个状态。那么我们可以循环一遍,对每个状态都
用前面的最优状态去更新他。最终得到该状态的最优解。
在循环的过程中,还得记录前驱、
在最优值无法更新的情况下,更新前驱可能得到更优解,因为
输出要满足字典序最小。
因为给出的作业是按字典序给出的,所以前驱这个数小也就代表着
字典序下
应该用个数组来存完成这些任务。
代码写得很挫。这篇不建议看代码。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1<<15
#define inf 0x3f3f3f3f
int pre[maxn];//显然用来存前驱
int mincost[maxn];//用来存所用天数最少
int minreduce[maxn];//用来存扣分最少
char Name[16][108];
char printname[16][108];
int deadline[16],ccost[16];//死亡日期和完成任务的时间
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s%d%d",Name[i],&deadline[i],&ccost[i]);
}
memset(mincost,0x3f,sizeof(mincost));
memset(minreduce,0,sizeof(minreduce));
int val=1<<n;
pre[1]=-1;
mincost[0]=0;//一个任务都没完成显然所用天数就是0呗
for(int i=1;i<val;i++)
{
int mmreduce=inf;
int cost=0;
int last=0;
for(int j=0;j<n;j++)
{
if(i&(1<<j))//如果他可以由未完成j+1任务转移来
{
int reduce=minreduce[i-(1<<j)];
if(mincost[i-(1<<j)]+ccost[j+1]>deadline[j+1])//如果超过死亡日期,也就是应该——
{
reduce+=mincost[i-(1<<j)]+ccost[j+1]-deadline[j+1];
}
if(mmreduce>reduce||(mmreduce==reduce&&(i-(1<<j)<i-(1<<last-1))))
{
mmreduce=reduce;
last=j+1;
cost=mincost[i-(1<<j)]+ccost[j+1];
}
}
}
mincost[i]=cost;
minreduce[i]=mmreduce;
pre[i]=last;
}
printf("%d\n",minreduce[val-1]);
int p=val-1,k=n;
while(pre[p]!=0)
{
strcpy(printname[k--],Name[pre[p]]);
p=p-(1<<(pre[p]-1));
}
for(int i=1;i<=n;i++)
{
printf("%s\n",printname[i]);
}
}
return 0;
}