Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13267 Accepted Submission(s): 6402
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.
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.
题意:
给定若干作业
作业有名,作业截止时间,完成作业需要的时间
超时完成会被扣分,每超一天扣一分
求完成所有作业最少被扣多少分
解析:
111代表三门都写了,110代表写了2,3门,没写第1门
10101&(1<<x) [x是从0开始的]
当x=1,3是等于0
用状压dp暴力枚举每一种情况i (1<<n)
每个情况再遍历是否包含第j个作业
如果i&(1<<j)==1,则包含,反之不包含
如果包含就找到这个情况下去 没完成j的情况
原来的情况:i-(1<<j)
扣掉的分数 ==写完前一门的时间 + 需要写的时间 - 要交的天数 (注意不能为负)
if(dp[没有j的情况]+reduce(写j扣的分)<dp[当前情况]),求dp
ac:
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1<<15
int dead[20],cost[20],dp[MAXN],t[MAXN],pre[MAXN];
string sub[20];
void output(int x)
{
if(!x)
return ;
output(x-(1<<pre[x]));
cout<<sub[pre[x]]<<endl;
}
int main()
{
int ti,n;
scanf("%d",&ti);
while(ti--)
{
scanf("%d",&n);
int bit=1<<n;
for(int i=0;i<n;i++)
cin>>sub[i]>>dead[i]>>cost[i];
for(int i=1;i<bit;i++)//枚举所有情况
{
dp[i]=1<<28; //我们求最小,所以要初始化极大
for(int j=n-1;j>=0;j--)//遍历每门课,要求字典序小,所以倒序
{
int temp=1<<j,reduce;
if(!(i&temp)) continue;
temp=i-temp; //前一种状态
reduce=t[temp]+cost[j]-dead[j];//扣掉的分数 = 前一门的时间+需要的时间-要求上交的时间
if(reduce<0)//扣掉的分数不能为负
reduce=0;
if(dp[temp]+reduce<dp[i])
{
dp[i]=dp[temp]+reduce;//记录扣掉的分数
t[i]=t[temp]+cost[j];//记录完成所需要时间
pre[i]=j;
}
}
}
printf("%d\n",dp[bit-1]);
output(bit-1);
}
return 0;
}