Doing Homework

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.

题意:

有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小

还是一如既往的不会做dp题,参考了大神的题解,想了很久
我的理解是状压dp,大概就是把状态用二进制数表示来节省空间?

#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define rep(i,a,n) for(int i = a; i < n; ++i)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long ll;
const ll inf = ;
const int INF =0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = ;
int t,n,m ;
int cnt,ans;
struct node{
string name;
int deadline,cost;
}cor[maxn];
struct DP{
int pre,now;
int score,time;
}dp[ << ];
int main(){
ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n;
string s;
int a,b;
for(int i = ; i < n ;i ++){
cin >> s >> a >> b;
cor[i] = node{s,a,b};
}
cnt = << n; // n门课程有cnt = 2^n种排课方案
for(int i = ; i < cnt ; i++) // i = 0 就是什么课都不选
{
dp[i].score = INF; //达到状态i所扣的分
int j = n - ; j >= ; j--)//这里不是很懂
{
int temp = << j; // 将j转化为二进制数
if(i & temp) // 状态i中选了j这门
{
int past = i - temp ; //past --- 没有选j这门的状态
int st = dp[past].time + cor[j].cost - cor[j].deadline; //看会不会超过限定日期而扣多少分
if(st < ) st = ; //因为不会加分
if(st + dp[past].score < dp[i].score)//如果扣的分比原先i状态扣的少
{
dp[i].score = st + dp[past].score;
dp[i].now = j; //以下都是记录
dp[i].pre = past;
dp[i].time = dp[past].time + cor[j].cost ;
}
}
}
}
//以下都是输出
int tmp = cnt - ;
cout << dp[tmp].score <<endl;
stack<int> st;
while(tmp){
st.push(dp[tmp].now);
tmp = dp[tmp].pre;
}
while(!st.empty()){
cout << cor[st.top()].name <<endl;
st.pop();
}
}
}

HDU 1074 Doing Homework【状压DP】的更多相关文章

  1. HDU 1074 Doing Homework 状压dp(第一道入门题)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. HDU 1074 Doing Homework (状压DP)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. HDU 1074 Doing Homework 状压DP

    由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...

  4. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  5. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdu_1074_Doing Homework(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:给你n个课程(n<=15)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...

  7. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  8. HDU 1074 Doing Homework(像缩进DP)

    Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...

  9. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  10. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

随机推荐

  1. VUE组件间数据方法的传递,初步了解

    父组件的数据传递到子组件: 子组件:(其中fMsg是要从父组件传递过来的数据,注意fMsg要在子组件props里先定义) 父组件:(使用v-bind,将自身数据绑定给中转属性fMsg,从而通过 子组件 ...

  2. 关于11G DataGuard 日志传输的案例

    案例探讨 在归档和非归档模式下,配置参数log_archive_dest_2后,DG的备库是否传输日志. 案例环境描述 本次环境如下,一套RAC+单机DG,然后从DG还原出来一个单独的测试库A,测试库 ...

  3. MySQL学习——标识符语法和命名规则

    转自:http://blog.csdn.net/notbaron/article/details/50868485 欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron ...

  4. kendo upload必填验证

    @using Kendo.Mvc.UI @using StudentManage.Common.Helper @model StudentManage.Models.Home.ImportDataFr ...

  5. Objective-C RunTime 学习笔记 之 消息转发流程

    1) 当向某个对象发送消息时,先从cache(cache_t)中查找方法对象(method_t),如果找到则进行回调:否则通过查找对象的类(元类)定义中方法列表,一直追溯到NSObject, 如果找到 ...

  6. vue生命周期钩子函数

    <template> <div> <h2>this is from C.vue</h2> </div> </template> ...

  7. 递归算法+sql三种分页

    using Maticsoft.Common; using System; using System.Collections.Generic; using System.Data; using Sys ...

  8. linux 基本原则和常用命令

    Linux的基本原则:1.由目的单一的小程序组成,组合小程序完成复杂的功能:2.一切皆文件:3.尽量避免捕获用户接口:4.配置文件保存为纯文本格式. CLI接口的命令提示符:#(root)$(普通用户 ...

  9. 如何共享联盟cookie

    接上一篇阿里妈妈账号登录状态如何长时间保存 既然我们获取到了cookie, 如果有多个程序都要使用到联盟帐号的时候, 如果不共享cookie, 那么每个程序都需要登录一次, 真的很浪费资源. 如何共享 ...

  10. insert into TABLE by SELECT ...

    insert into isp_rmi3 ( select r.id, r.blue_id, r.sell_channel,NULL, r.interface_id, NULL, NULL, r.ur ...