Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12145    Accepted Submission(s): 5851

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.

 
Author
Ignatius.L
 
题意:有n门课,每门课有截止时间和完成所需的时间,
如果超过规定时间完成,每超过一天就会扣1分,
问怎样安排做作业的顺序才能使得所扣的分最小
思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况
例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,
那么我们可以枚举1~1<<n便可以得出所有的状态
然后对于每一门而言,其状态是t = 1<<i,
我们看这门在现在的状态s下是不是完成,
可以通过判断s&t是否为1来得到
当得出t属于s状态的时候,我们便可以进行DP了,
在DP的时候要记录路径,方便之后的输出
 
#include<stdio.h>
#include<string.h>
#include<memory>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long LL;
#define INF 1<<30;
struct node
{
string name;
int dead,cost;
}a[];
struct kode
{
int time,score,pre,now;
}dp[<<];
int main()
{
int t,n;
cin>>t;
while(t--)
{
memset(dp,,sizeof(dp));
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i].name>>a[i].dead>>a[i].cost;
}
int End=<<n;
for(int s=;s<End;s++)
{
dp[s].score=INF;
for(int i=n-;i>=;i--)//输出字典序小的方案,所以应该倒序循环当前作业
{
int tem=<<i;
if(s&tem)
{
int past=s-tem;
int st=dp[past].time+a[i].cost-a[i].dead;
if(st<)
st=;
if(st+dp[past].score<dp[s].score)
{
dp[s].score=st+dp[past].score;
dp[s].now=i;
dp[s].pre=past;
dp[s].time=dp[past].time+a[i].cost;
}
}
}
}
stack<int> S;
int tem=End-;
cout<<dp[tem].score<<endl;
while(tem)
{
S.push(dp[tem].now);
tem=dp[tem].pre;
}
while(!S.empty())
{
cout<<a[S.top()].name<<endl;
S.pop();
}
}
return ;
}
/* 题意:有n门课,每门课有截止时间和完成所需的时间,
如果超过规定时间完成,每超过一天就会扣1分,
问怎样安排做作业的顺序才能使得所扣的分最小 思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况 例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,
那么我们可以枚举1~1<<n便可以得出所有的状态
然后对于每一门而言,其状态是t = 1<<i,
我们看这门在现在的状态s下是不是完成,
可以通过判断s&t是否为1来得到
当得出t属于s状态的时候,我们便可以进行DP了,
在DP的时候要记录路径,方便之后的输出 */
 
 

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

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

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

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

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

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

  5. 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 ...

  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 3681(bfs+二分+状压dp判断)

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

  9. hdu 4778 Gems Fight! 状压dp

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

随机推荐

  1. yii page title, CMenu 中文不显示

    Page title: <?php echo CHtml::encode(iconv('gbk','utf-8',$this->pageTitle)); ?> CMenu: fram ...

  2. 原生爬虫小Demo

    import re from urllib import request class Spider(): url = 'https://www.panda.tv/cate/lol' #[\s\S]匹配 ...

  3. js 中文长字符截短&关键字符隐藏 自定义过滤器

    两个非常简单的过滤器:隐藏关键字符和字符截短.同样也可以迁移到ng和原生js直接使用(去掉avalon.filters声明即可).后期还有不错的过滤器,还往这里面加 keyword:avalon,js ...

  4. ShowDoc

    ShowDoc 摘自ShowDoc 每当接手一个他人开发好的模块或者项目,看着那些没有写注释的代码,我们都无比抓狂.文档呢?!文档呢?!Show me the doc !! 程序员都很希望别人能写技术 ...

  5. Django之环境搭建

    安装django pip install django 安装完django之后就有了可用的管理工具django-admin.py,我们可以用它来创建我们的项目. django-admin的语法: dj ...

  6. java vector的多线程安全是否有用

    在网上搜了不少文章,发现有不少没讲清楚的,也有不少好文,本文希望更易懂地描述该问题.如有不对的地方,请多多指正~~ vector的使用主要有如下两种场景:(1)vector所谓的多线程安全,只是针对单 ...

  7. C++发展概述、优缺点及应用领域

    个人观点(C++虽功能强大,但是底层还是调用C,C++为了吸引更多的C程序员,功能过于丰富且复杂,一定程度上反而降低了可编程的实用性.但是不可否认C++ 也是一门艺术.) C++是一门以C为基础发展而 ...

  8. 使用ModelForm表单验证

    1.定义model.py model中定义的字段类型,只有在通过form进行验证的时候才有效,数据库中的字段类型与其并不完全一致,如数据库中并没有ipaddress类型.如果不通过form对字段进行验 ...

  9. Centos7 apache2.4.29(httpd) 安装

    重点参考文章:https://blog.csdn.net/MrDing991124/article/details/78829184  写的很详细了,自己按着改博文走了不遍,不错! 一.配置安装环境 ...

  10. Centos7 教程收集ing

    CentOS7 常用命令集合 https://blog.csdn.net/o0darknessyy0o/article/details/52072054#t1 1.centOS7下实践查询版本/CPU ...