Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9123 Accepted Submission(s): 4294
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 MathHintIn 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门课程,每门课程有一个最晚提交时间和完成所需时间。
每超时一天扣一分。问最少扣多少分,输出安排的顺序。
分析:因为n最大是15,那么方案数为15!,所以数组开不下要进行状态压缩。
可以压缩成2^15. 这样的话1代表完成0代表位完成。就可以进行dp了。
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 1<<30
struct tree
{
char name[101];
int deadline;
int t;
}p[50];
int dp[1<<15];
int dpt[1<<15];
int pre[1<<15];
void pf(int n)
{
if(n!=0)
{
pf(n-(1<<pre[n]));
printf("%s\n",p[pre[n]].name);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
memset(pre,-1,sizeof(pre));
memset(dpt,0,sizeof(dpt));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s%d%d",p[i].name,&p[i].deadline,&p[i].t);
}
int n1=1<<n;
for(int i=0;i<n1;i++) //不知道为什么这里加上等号就会WA讲道理没有影响才对
dp[i]=inf;
dp[0]=0;
for(int i=1;i<n1;i++)
{
for(int j=n-1;j>=0;j--) //因为是按字典序输入,所以反过来就能字典序输出,也可以用sort实现
{
int k=1<<j;
if(!(i&k))continue; //当未完成k任务时跳出
int time=dpt[i-k]+p[j].t-p[j].deadline;
if(time<0)
time=0;
if(dp[i]>dp[i-k]+time)
{
dp[i]=dp[i-k]+time;
pre[i]=j;
dpt[i]=dpt[i-k]+p[j].t;
}
}
}
printf("%d\n",dp[n1-1]);
pf(n1-1);
}
}