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状态选到结束得 ...
随机推荐
- Mysql5.8解压版安装问题:TCP/IP, --shared-memory, or --named-pipe should be configured on NT OS
问题描述: cmd显示如下: .err文件显示: [ERROR] [MY-010131] [Server] TCP/IP, --shared-memory, or --named-pipe shoul ...
- vue 基础的一些字眼及路由
每个框架都有一些自己的写法和一些字眼 摘自 vue 官网 v-bind 缩写 <!-- 完整语法 --> <a v-bind:href="url">...& ...
- 6、jeecg 笔记之 自定义excel 模板导出(一)
1.前言 jeecg 中已经自带 excel 的导出导出功能,其所使用的是 easypoi,尽管所导出的 excel 能满足大部分需求, 但总是有需要用到自定义 excel 导出模板,下文所用到的皆是 ...
- 干了这杯Java之transient关键字
看源码的时候,发现transient这个关键字,不甚理解,查找资料发现:不被序列化 疑问: 静态变量是不是不被序列化? public class User implements Serializabl ...
- NABCD分析---校园服务
N(需求): 大学生活中,很多琐碎的小事浪费同学时间精力.我们的APP本着为同学服务的宗旨,解决生活中各方面的问题,同学们可以在APP上发布各种信息,例如兼职,二手买卖等等. A(做法): 用户打开A ...
- stm32高级定时器的应用——spwm
用过stm32定时器的朋友都知道,定时器的CCR寄存器,可以用来配置PWM的输出,但同样也可以用来配置spwm.废话不多说,直接上代码. 首先,你得考虑一下几个因素: 1.同步调制还是异步调制. 2 ...
- Windows SFTP 的安装
用于Windows系统的免费SFTP服务器-Free SFTP Servers 前不久,有人问我:“怎么从 Linux 系统传文件到 Windows 服务器,不能用 FTP 协议.” 文件数量不大.用 ...
- 我对MVC的理解
1. MVC :M模型 V视图 C控制器 1.1 模型是用来处理业务逻辑的,里面由许多类构成 1.2 视图是用来显示界面的 1.3 控制器是一个中间人,它通过视图的提交方式(post, ...
- net Core TOptions和热更新
TOptions接口 net Core 项目有个appsettings.json文件,程序默认也是读取的这个文件,appsettings.json是一个配置文件 我们可以把appsettings.js ...
- Leetcode: The Maze II
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...