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],也有可能不出现卡 ...
随机推荐
- floor() 和 ceil()函数
在C语言的库函数中,floor函数的语法例如以下: #include <math.h> double floor( double arg ); 功能: 函数返回參数不大于arg的最大整数. ...
- [Angular 2] A Simple Form in Angular 2
When you create a Form in Angular 2, you can easily get all the values from the Form using ControlGr ...
- rk3288 ov8858 camera移植
平台:瑞芯的rk3288 SDK:4.4/5.0/5.1 作者:fulinux *****本文同意转载.只是请注明出处:http://blog.csdn.net/fulinus**** rk3288的 ...
- 第四章:使用Proxy代理让客户端服务端分工合作。
<基于1.8 Forge的Minecraft mod制作经验分享> 别被那个Proxy代理吓到,很简单的. 我们先讨论为什么要用Proxy代理: 像打开新的UI这种操作,比如打开一个背包, ...
- dp 斯特林数 HDU2512一卡通大冒险
这道题其实就是斯特林数,找不同的集合,一共有多少中组法,递推式就是dp[n][k] = dp[n - 1][k - 1] + k * dp[n - 1][k]; 这个式子可以这么解释,dp[n][k] ...
- 检查主机是否存活的shell脚本
#!/bin/bash PREFIX= num= " ]; do echo -en "Pinging ${PREFIX}.${num}..." >& &qu ...
- toString结果
String[] str = new String[] { "a", "b", "c" }; System.out.println(str) ...
- Spring + CXF(REST):webservice not found
可能原因:spring的bean:SpringBus的创建晚于spring的bean:Server的创建 解决办法:指定依赖注入顺序@DependsOn 先写着,留个空,后面补充
- ajax+FormData+javascript 实现无刷新上传附件
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Windows 7 64位下解决不能创建Django项目问题
把djingo-admin.py的全路径写出来 在cmd命令行下直接输入python C:\Python27\Scripts\django-admin.py startproject site(sit ...