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],也有可能不出现卡 ...
随机推荐
- Android笔记之 文件保存、压缩与清空删除
这两天改进优化项目中图片上传的代码.考虑到可能有7.8M的比較大的图片,由于要先进行压缩.所以设计到文件的压缩,保存与清空删除操作. 在这里记下笔记. /** * 压缩并另存为,每次先清空再保存 */ ...
- Cobar是提供关系型数据库(MySQL)分布式服务的中间件
简介 Cobar是提供关系型数据库(MySQL)分布式服务的中间件,它可以让传统的数据库得到良好的线性扩展,并看上去还是一个数据库,对应用保持透明. 产品在阿里巴巴稳定运行3年以上. 接管了3000+ ...
- CVE-2014-0196(马拉松赛跑bug)
/* * CVE-2014-0196: Linux kernel <= v3.15-rc4: raw mode PTY local echo race * condition * * Sligh ...
- Underscore.js 常用类型判断以及一些有用的工具方法
1. 常用类型判断以及一些有用的工具方法 underscore.js 中一些 JavaScript 常用类型检查方法,以及一些工具类的判断方法. 首先我们先来谈一谈数组类型的判断.先贴出我自己封装好的 ...
- yii CGridView colum 链接
默认显示的内容是没有链接的,现在想加链接,效果图如下 代码位置就在columns数组里,直接上代码说明 $this->widget('zii.widgets.grid.CGridView', a ...
- HDU -2670 Girl Love Value
这道题是刚好装满的背包问题,刚好选取k个,状态转移方程为dp[i][j] = max( dp[i - 1][j], dp[i - 1][j - 1] + Li - Bi(j - 1) ) dp[i][ ...
- Python迭代--笔记
<python3 程序开发指南> 迭代子.迭代操作 迭代子是一个对象,该对象可提供_next_()方法,该方法依次返回每个相继的数据项,并在没有数据项时产生StopIteration()异 ...
- asp.net设置元素css的属性
controls.style.Add("css名称","css值") 添加class规则 control.cssclass="str_cssname& ...
- 用户 'IIS APPPOOL\ExportExcel' 登录失败。
解决了前两个错误,在成功打开项目后,在访问数据库又越到如下错误 “/”应用程序中的服务器错误. 用户 'IIS APPPOOL\ExportExcel' 登录失败. 说明: 执行当前 Web 请求期间 ...
- mssql定时执行作业。
---2000 企业管理器 --管理 --SQL Server代理 --右键作业 --新建作业 --"常规"项中输入作业名称 --"步骤"项 --新建 --&q ...