HDU 1074 Doing Homework (状压DP)
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14600 Accepted Submission(s): 7070
Problem Description
Input
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
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
Computer
English
Math Computer
English
Math
Sample Output
Computer
Math
English Computer
English
Math
题目大意
作业就要做不完了,每项作业(已经按照字典序排好)都有一个花费时间和限定时间,在限定时间之后在交的作业,每天扣一分,现在扣分最小值和相应的作业顺序(相同最小值时,以字典序最小的情况输出)
题目分析
注意到最多只有15个作业,所以可以利用一个二进制数来表示当前的完成情况:比如111是全完成,101是第二个没完成
假如有7个作业,那么二进制就是 1111111 也就是 2^7-1 即127 我们枚举从1到127 用dp[i]来表示达到当前状态所扣分数的最小值
然后j从后向前枚举单个作业(至于为什么从后向前之后再说),判断这个作业是不是在当前状态已经完成了的,比如101,就是第一个作业和第三个作业是已经完成了的,那么当前的状态就可以从前一个状态转化过来,比如101 就可以从100或者001转化过来 这样就可以进行更新了,显然的 dp[i]=max(dp[i],dp[前一个状态]+a[j])
现在可以解释为什么从后向前遍历单个作业了,因为根据上面的转移方程,我们可以看出来当前状态是在完成这个作业之后达到的,也就是说,这个作业是最后完成的,而题目中要求,如果遇到最小值相同的时候,要以字典序最小的方案输出,所以必须要从后往前遍历
当然,要输出做题顺序,就要每次都记录当前状态的上一个状态是什么,以及当前完成了什么作业。
代码:
#include <bits/stdc++.h> using namespace std; typedef struct
{
string name;
int dday;
int cost;
}homework; struct
{
int pre;
int name;
int vul;
int time;
}dp[<<]; homework a[];
int tot,t,n,i,j;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
cin>>a[i].name>>a[i].dday>>a[i].cost;
}
tot=<<n;
for(i=;i<tot;i++)
{
dp[i].vul=0x7fffff;
for(j=n-;j>=;j--)
{
int temp=<<j;
if(temp&i)
{
int s=dp[i-temp].time+a[j].cost-a[j].dday;
if(s<)
s=;
if(dp[i].vul>dp[i-temp].vul+s)
{
dp[i].pre=i-temp;
dp[i].name=j;
dp[i].vul=dp[i-temp].vul+s;
dp[i].time=dp[i-temp].time+a[j].cost;
}
}
}
}
cout<<dp[tot-].vul<<endl;
i=tot-;
stack<int>q;
while(dp[i].time)
{
q.push(dp[i].name);
i=dp[i].pre;
}
while(!q.empty())
{
cout<<a[q.top()].name<<endl;
q.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
由于数据量较小,直接二进制模拟全排列过程,进行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状态选到结束得 ...
随机推荐
- 交叉熵和softmax
深度学习分类问题结尾就是softmax,损失函数是交叉熵,本质就是极大似然...
- IDEA 配置热更新
- HTML的状态码
HTML状态码的相关知识 ㈠:含义 HTTP状态码(英语:HTTP Status Code)是用以表示网页服务器超文本传输协议响应状态的3位数字代码. 也就是当浏览者访问一个网页时,浏览者的浏览器会向 ...
- (Java多线程系列六)join()的用法和线程的优先级
join()的用法和线程的优先级 1.join()的用法 join()作用就是让其他线程处于等待状态 先看一个需求:创建一个线程,子线程执行完毕后,主线程才能执行 public class JoinT ...
- Housewife Wind
Housewife Wind 参考博客:POJ2763 Housewife Wind(树剖+线段树) 差不多是直接套线段树+树剖的板子,但是也有一些需要注意的地方 建树: void build() { ...
- JavaWeb_通过xml配置文件配置servlet访问路径
Servlet两种访问路径 一.在Servlet类中通过设置@WebServlet标注访问 二.在web.xml中编写配置文件 第一种能比较直观看到Servlet路径,但当项目发布后不好再对Servl ...
- WordPress过滤器(Filters):apply_filters和add_filter
过滤器(Filters)对于WordPress来说是非常重要的,它极大地扩展了WordPress的定制能力,提高了WordPress的灵活性.无论是制作主题还是开发插件,我们基本上都会或多或少地使用到 ...
- VS2015 ASP.NET MVC5 EntityFramework6 Oracle 环境篇
//来源:https://www.cnblogs.com/lauer0246/articles/9576940.html Asp.Net MVC EF各版本区别 2009年發行ASP.NET MVC ...
- 利用MFC在控件内将txt中的数据画图
1:采集txt文件中的数据测试程序如下: #include "stdafx.h" #include <fstream> #include "iostream& ...
- 如何把java项目打包成war包
用Eclipse手动打包 右击工程名 选择Export… 选择Web → WAR file 点击Browse,选择导出路径 然后war包就被导出来啦~是不是很简单呢 利用Maven的package命令 ...