HDU-1047(DP-二进制状态压缩)
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.
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.
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
Computer
Math
English
3
Computer
English
Math
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
using namespace std; #define N 15 typedef struct
{
char name[];
int deadline;
int cost;
}Course; typedef struct
{
int timeUsed; //到此状态总共花费的时间
int minPoint; //此状态最小的被罚分数
int preState; //此状态对应的前一状态
int lastCourse; //从上一状态转移到此状态完成的course
}State; Course homework[];
State states[<<N];
int n; //课程数 void outputResult()
{
stack<int> s;
int tempState;
int tempCourse;
char courseName; tempState = (<<n) - ;
printf("%d\n", states[tempState].minPoint);
while(tempState != )
{
tempCourse = states[tempState].lastCourse;
s.push(tempCourse);
tempState = states[tempState].preState;
} while(!s.empty())
{
tempCourse = s.top();
s.pop();
printf("%s\n", homework[tempCourse].name);
}
} void processDP()
{
int i, j, maxStateN;
int temp;
int reduce;
int preState; states[].lastCourse = ;
states[].minPoint = ;
states[].preState = -;
states[].timeUsed = ; maxStateN = <<n; for(i=; i<maxStateN; i++)
{
states[i].minPoint = 0x7fffffff;
for(j=n-; j>=; j--) //这里j从n-1递减到0,是为了保证当罚分一样时,顺序按照字母顺序递增的方式排列
{
temp = <<j;
if(i & temp) //状态i中包含第j门课
{ preState = i - temp;
reduce = states[preState].timeUsed+homework[j].cost-homework[j].deadline;
if(reduce < )
{
reduce = ;
} if(states[i].minPoint > states[preState].minPoint + reduce)
{
states[i].minPoint = states[preState].minPoint + reduce;
states[i].lastCourse = j;
states[i].preState = preState;
states[i].timeUsed = states[preState].timeUsed + homework[j].cost;
}
}
}
}
} int main()
{
int i;
int T; scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(i=; i<n; i++)
{
scanf("%s%d%d", homework[i].name, &homework[i].deadline, &homework[i].cost);
} processDP();
outputResult();
}
return ;
}
HDU-1047(DP-二进制状态压缩)的更多相关文章
- hdu 1429 bfs+二进制状态压缩
开始时候只用了BFS,显然超时啊,必然在结构体里加一个数组什么的判重啊,开始用的一个BOOL数组,显然还是不行,复杂度高,每次都要遍历数组来判重:后百度之,学习了二进制状态压缩,其实就用一个二进制数来 ...
- HDU 3001 Travelling(状态压缩DP+三进制)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上 ...
- [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)
[BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...
- POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53312 Accepted: 16050 Des ...
- # 最短Hamilton路径(二进制状态压缩)
最短Hamilton路径(二进制状态压缩) 题目描述:n个点的带权无向图,从0-n-1,求从起点0到终点n-1的最短Hamilton路径(Hamilton路径:从0-n-1不重不漏的每个点恰好进过一次 ...
- HDU 4511 (AC自动机+状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...
- hdu 2825(ac自动机+状态压缩dp)
题意:容易理解... 分析:在做这道题之前我做了hdu 4057,都是同一种类型的题,因为题中给的模式串的个数最多只能为10个,所以我们就很容易想到用状态压缩来做,但是开始的时候我的代码超时了dp时我 ...
- HDU 1074 Doing Homework (状态压缩 DP)
题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...
- Hdu 4778 Gems Fight! (状态压缩 + DP)
题目链接: Hdu 4778 Gems Fight! 题目描述: 就是有G种颜色,B个背包,每个背包有n个宝石,颜色分别为c1,c2............两个人轮流取背包放到公共容器里面,容器里面有 ...
- [HDU 4336] Card Collector (状态压缩概率dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...
随机推荐
- MySQL主从复制与lvs+keepalived单点写入读负载均衡高可用实验【转】
一.环境Master(主机A):192.168.1.1Slave(主机B) :192.168.1.2 W-VIP(写入) :192.168.1.3 R-VIP(读取) :192.168.1.4 ...
- [Angular 2] Passing Template Input Values to Reducers
Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...
- 一个int类型究竟占多少个字节
一个int占多少个字节? 这个问题我们往往得到的答案是4. 可是int究竟占多少个字节,却跟你的机器环境有关. As you can see, the typical data type sizes ...
- css布局之负margin妙用及其他实现
相信大家在项目的开发中都遇到过这样的需求,一行放X(X>1)个块且相邻块之间的间距相同. 大概就是上面这个样子,下面介绍几种实现的方式. 1.负margin大法 设置好元素的宽度和留白占满父级的 ...
- WebBrowser控件使用相关
修改WebBrowser控件的内核解决方案 http://www.cnblogs.com/sung/p/3391264.html C#中的WebBrowser控件的使用 http://www.cnbl ...
- GIt/Github常用命令
1)git init:初始化本地仓库 2)创建文件:touch read.txt 3)当操作本地的文件时,使用常用的命令,如(mv,ls..)就可以操作,当操作暂存区的文件时需要在命令前家git,并且 ...
- MySql中的时间类型datetime,timestamp,date,year比较
MySQL日期类型.日期格式.存储空间.日期范围比较.日期类型 存储空间 日期格式 日期范围------------ --------- ...
- Delphi 做ActiveX的详细过程
1.新建 如下图 点击OK 依然点击OK 出现了如上图的节面,就像窗体一样. 然后 你就想干什么干什么. 这个做好之后, 这个是我设计的窗体. 然后 就添加 外部可以调用的接口了. 如果你不想让外部调 ...
- JavaScript 类、构造函数、原型
类.构造函数.原型 :本质均为函数 利用的原理是:词法作用域,调用对象及作用域链 闭包 属性查找方式 设计和new运算符一起使用的函数叫做构造函数. 构造函数的工作:初始化一个新创建的对象 ...
- 【转】解读Qt 事件处理机制(上篇)
[转自]:http://mobile.51cto.com/symbian-272812.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生 ...