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. 本地浏览器Websql数据库操作

    前几天看到一个小姐姐问我一个添加修改的我看了一下案例弄了一个出来.... 参考案例:HTML5本地数据库(WebSQL)[转] - 狂流 - 博客园  https://www.cnblogs.com/ ...

  2. java -cp通配符

    JDK6支持java -cp后面跟通配符'*',试了一下发现还是需要注意: 错误方式(Wrong way): java  -cp /data/apps/lib/*.jar com.chinacache ...

  3. Spark入门到精通--(第八节)环境搭建(Hadoop搭建)

    上一节把Centos的集群免密码ssh登陆搭建完成,这一节主要讲一下Hadoop的环境搭建. Hadoop下载安装 下载官网的Hadoop 2.4.1的软件包.http://hadoop.apache ...

  4. CRT乱码问题

    本人在使用CRT过程中遇到乱码问题,经调试发现要把字体调整为"新宋体",编码格式用"UTF-8". 调整字体: Options à Session option ...

  5. spring 数据库多数据源路由

    项目中需要根据不同业务进行分库,首先是将业务不同业务映射到不同过的数据库( biz --> db,可能存在多对一情况), 查看springjdbc源码发现AbstractRoutingDataS ...

  6. 问题记录 --Error parsing column 1 (Function_Num=10 - String)”

    当C#查询数据库出现Error parsing column ## 的时候,首先去看看数据库里面该字段是什么类型,然后在看看你在创建model 的时候是什么类型,如果model的类型和数据库字段类型不 ...

  7. 使用AD画PCB的技能总结(纯属个人笔记,请大神多多指导)

    在参加2017全国电子设计大赛的过程中,我将平时学到的点点滴滴记录下来,作为曾经的回忆吧!(未完待续) ------------------------------------------------ ...

  8. git三、上传项目到github

    1.创建github仓库 2.git clone url (克隆仓库到本地,如profect) 3.将项目复制到本地文件夹profect下 4.git add . (添加项目至缓存区) 5.git c ...

  9. notify.min.js

    /*! * @wcjiang/notify v2.0.11 * JS achieve the browser title flashing , scrolling, voice prompts , c ...

  10. navicat 远程访问mariadb失败,修改配置如下

    1.首先配置允许访问的用户,采用授权的方式给用户权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '121212' WITH GRAN ...