【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D - Doing Homework
https://vjudge.net/contest/68966#problem/D
http://blog.csdn.net/u010489389/article/details/19218795
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=;
int n;
struct hmwk
{
char ch[maxn];
int d;
int c;
}p[]; struct DP
{
int time;
int reduced;
int fa;
}dp[(<<)]; char s[][]; void Init()
{
for(int i=;i<(<<);i++)
{
dp[i].fa=-;
dp[i].time=;
dp[i].reduced=INF;
}
dp[].reduced=; }
//按字典序增序输出
bool cmp(const hmwk&x,const hmwk& y)
{
return strcmp(x.ch,y.ch)<=;
}
//递归输出,x为当前,fa为前一个
void Print(int x,int fa)
{
int flag;
if(x==)
{
for(int i=;i<n;i++)
{
if(fa&(<<i))
{
flag=i;
break;
}
}
printf("%s\n",p[flag].ch);
return;
}
Print(dp[x].fa,x);
for(int i=;i<n;i++)
{
if((x&(<<i))!=(fa&(<<i)))
{
flag=i;
break;
}
}
if(x==)
{
printf("%s\n",p[flag].ch);
}
else
{
printf("%s\n",p[flag].ch);
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{ scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s%d%d",p[i].ch,&p[i].d,&p[i].c);
}
sort(p,p+n,cmp);
Init();
//每个i代表一个状态,i的每一位代表一个科目
for(int i=;i<(<<n);i++)
{
for(int k=;k<n;k++)
{
//如果是1跳过
if(i&(<<k))
{
continue;
}
//now为转移后的状态
int now=i+(<<k);
dp[now].time=dp[i].time+p[k].c;
//dp[now].time-p[k].d<0说明没有延期,就是0
if(dp[i].reduced+max(,dp[now].time-p[k].d)<dp[now].reduced)
{
dp[now].reduced=dp[i].reduced+max(,dp[now].time-p[k].d);
dp[now].fa=i;
}
}
}
//最终结果就是111111(n个1),对应的数就是(1<<n)-1
printf("%d\n",dp[(<<n)-].reduced);
Print(dp[(<<n)-].fa,(<<n)-);
}
return ;
}
【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D - Doing Homework的更多相关文章
- 【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus
A - Max Sum Plus Plus https://vjudge.net/contest/68966#problem/A http://www.cnblogs.com/kuangbin/arc ...
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 C - Monkey and Banana
https://vjudge.net/contest/68966#problem/C [参考]http://blog.csdn.net/qinmusiyan/article/details/79862 ...
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 G - 免费馅饼
https://vjudge.net/contest/68966#problem/G 正解一: http://www.clanfei.com/2012/04/646.html #include< ...
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 F - Piggy-Bank 【完全背包问题】
https://vjudge.net/contest/68966#problem/F http://blog.csdn.net/libin56842/article/details/9048173 # ...
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 E - Super Jumping! Jumping! Jumping!
https://vjudge.net/contest/68966#problem/E http://blog.csdn.net/to_be_better/article/details/5056334 ...
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV
http://www.cnblogs.com/joeylee97/p/6616039.html 引入一个cnt,输入元素与上一个元素相同,cnt增加,否则cnt减少,当cnt为零时记录输入元素,因为所 ...
- [kuangbin带你飞]专题十二 基础DP1
ID Origin Title 167 / 465 Problem A HDU 1024 Max Sum Plus Plus 234 / 372 Problem B HDU 1 ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题十二 HDU 1176 免费馅饼
题意: 中文题意不解释…… 思路: 先把x,T存到矩阵里 然后像数塔一样从最底层走一边就行了 dp[i][j]代表在时间为j时 第i个位置最多能吃到多少个馅饼 最后输出第0时刻的5位置的馅饼数量就好了 ...
- [kuangbin带你飞]专题十五 数位DP
ID Origin Title 62 / 175 Problem A CodeForces 55D Beautiful numbers 30 / 84 Problem B HD ...
随机推荐
- Android开发使用的常见第三方框架
1.SlidingMenu 官网:https://github.com/jfeinstein10/SlidingMenu 网友使用:http://blog.csdn.net/yangyu2012122 ...
- php错误的处理
错误的分类 通常分3种: 语法错误: 程序运行之前,都要先检查语法.如果语法有错误,就会立即报错,并且不会去执行程序. 运行时错误: 就是在程序语法检查通过后,,开始运行程序并在此过程中遇到的错误.常 ...
- 关于统一资源标志符URL的理解
URL由三部分构成 A:B:C A:URL使用的领域 B:在此领域的类型名称 C:标识资源的具体位置 C可以是一个绝对路径,也可以是端口号加上资源名称,总之是可以唯一标示资源的标识 例: 在网址中 S ...
- (20)IO流之SequenceInputStream 序列流
序列流,对多个流进行合并. SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直 ...
- 【翻译】CSS水平和垂直居中的12种方法
英语原文链接 在CSS中有许多不同的方法能够做到水平和垂直居中,但很难去选择合适的那个.我会向你展示我所看到的所有的方法,帮助你在所面对的情境下选择最棒的那一个. 方法1 此方法将只能垂直居中单行文本 ...
- .Net轻量级ORM-NPoco的使用方法-摘自NPoco国外官方Wiki
文章引用自NPoco官方Wiki,地址:https://github.com/schotime/NPoco/wiki,因公司网络不稳定,有时无法访问,特将其摘抄. Home Adam Schroder ...
- Java程序员应当知道的10个面向对象设计原则
面向对象设计原则是OOPS编程的核心, 但我见过的大多数Java程序员热心于像Singleton (单例) . Decorator(装饰器).Observer(观察者) 等设计模式,而没有把足够多的注 ...
- ioS开发之CoreLocation(GPS定位)
1.概述 在iOS开发中,要想加入地图和定位功能这2大功能,必须基于2个框架进行开发 (1)Map Kit :用于地图展示 (2)Core Location :用于地理定位 2个热门专业术语: LBS ...
- CSS -- 练习(待续优化)
啊啊啊 错误百出啊 错点总结: 字符全角半角.清楚浮动.结尾</div>有点乱找不到对应的了.注释要写好. <!DOCTYPE html> <html lang=&qu ...
- 关于SQL的一些小知识
在代码中调用存储过程的时,必须先测试存储过程,存储过程测试成功之后再去java中去调用!!@!@#!@!@! 以后自己写的存储过程写一个本地保存一个.!~~~!!(这个很关键) 以后在代码中的SQL都 ...