HDU 1074 Doing Homework【状压DP】
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】的更多相关文章
- HDU 1074 Doing Homework 状压dp(第一道入门题)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework (状压DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework 状压DP
由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu_1074_Doing Homework(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:给你n个课程(n<=15)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...
- HDU 5765 Bonds(状压DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...
- 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 ...
- hdu 3681(bfs+二分+状压dp判断)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...
- hdu 4778 Gems Fight! 状压dp
转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...
随机推荐
- Python的基本语法1
一.python的基本数据类型 (1)6种基本数据类型 1.数字类型 int 整数,2,0,-4等 float 浮点数,如1.2,-0.3等 bool 布尔类型,True,False complex ...
- JsonPath如何获取JSON数据中的值
场景: 发送接口请求后,得到请求结果值是Json数据, 需要从Json数据信息中提取字段值. 响应值字符与字符之间有空格,导致用正则表达式方法提取比较麻烦,于是用java的JsonPath方法提取快速 ...
- Hello Json(c#)
第一步:下载的DLL→Newtonsoft.Json 打开链接后下载这个(有可能版本有所更新,选前面点的就是了): 接下来是新建一个Console项目,然后是引用,然后上码 class Progra ...
- Python从入坑到放弃!
Python基础 python基础 python基础之 while 逻辑运算符 格式化输出等 python基础之 基本数据类型,str方法和for循环 python基础之 列表,元组,字典 pyth ...
- webclient 操作超时
Client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/ ...
- 白话skynet第一篇
当你走过一个坐在自己店门前的杂货商面前.走过一个吸着烟斗的守门人面前,走过一个马车夫面前时,请你给我描绘一下这个杂货商.守门人和马车夫,他们的姿态,他们的外貌,要用画家那样的细节描绘出他们的精神本质, ...
- Elasticsearch.安装插件(head)
Elasticsearch.安装插件(head) 环境: Linux 7.x jdk1.8 目录结构(跟目录多了两个文件) /resources ### 存放软件源 /u01/ ...
- web_app框架
web app 建立在asyncio的基础上,因此用aiohttp写一个基本的app.py import logging; logging.basicConfig(level=logging.INFO ...
- mysql杯观锁与乐观锁
悲观锁与乐观锁是两种常见的资源并发锁设计思路,也是并发编程中一个非常基础的概念.本文将对这两种常见的锁机制在数据库数据上的实现进行比较系统的介绍. 悲观锁(Pessimistic Lock) 悲观锁的 ...
- Linux环境上部署Flask
[该文章只涉及个人部署的简单流程,读者可通过其它途径了解详细部署流程] 依个人部署项目可预先安装好需要的环境,这里已提前安装好LNMP环境 1.安装Python环境 安装virtualenv环境 配置 ...