题目

记忆化搜索(深搜+记录状态)

感谢JLGG

//记忆话搜索
//一本书2中状态,竖着放或者横着放
//初始先都竖着放,然后从左边往右边扫
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[][][];//dp[第几个][厚度][宽度]
int n;
int a[],b[]; int rec(int i,int th,int w)
{
if(dp[i][th][w]!=-)return dp[i][th][w];
int res;
if(i==n)res=th;
else if(th-a[i]<w+b[i])res=rec(i+,th,w);//判断合不合法
else res=min(rec(i+,th-a[i],w+b[i]),rec(i+,th,w));
return dp[i][th][w]=res;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int th=;
for(int i=;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
th+=a[i];
}
memset(dp,-,sizeof(dp));
int ans=rec(,th,);
printf("%d\n",ans);
}
return ;
}

Codeforces 294B Shaass and Bookshelf(记忆化搜索)的更多相关文章

  1. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...

  2. Codeforces 900D Unusual Sequences:记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/900/D 题意: 给定x,y,问你有多少个数列a满足gcd(a[i]) = x 且 ∑(a[i]) = y ...

  3. Codeforces 1079C Playing Piano(记忆化搜索)

    题目链接:Playing Piano 题意:给定长度为n的序列$a_i$,要求得到同样长度的序列$b_i$.满足一下条件: if $a_i < a_{i+1}$ then $b_i < b ...

  4. Codeforces 294B Shaass and Bookshelf:dp

    题目链接:http://codeforces.com/problemset/problem/294/B 题意: 有n本书,每本书的厚度为t[i],宽度为w[i] (1<=t[i]<=2, ...

  5. CodeForces 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】

    这道题目的意思就是排两排书,下面这排只能竖着放,上面这排可以平着放,使得宽度最小 根据题意可以得出一个结论,放上这排书的Width 肯定会遵照从小到大的顺序放上去的 Because the total ...

  6. Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜

    题意及思路:https://blog.csdn.net/bossup/article/details/37076965 代码: #include <bits/stdc++.h> #defi ...

  7. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  8. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  9. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

随机推荐

  1. Python常用内建模块

    Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...

  2. AIDL与service

    Service:Local service,一个进程中的多线程服务. AIDL:remote service,不同进程间通信. Service启动方法: startService():调用方destr ...

  3. Windows Phone动画

    从事Windows Phone开发已经有一段时间了,但是一直没有好好的静下心来梳理一下自己这段时间的知识,一是怕自己学问不到家,写不出那些大牛一般的高屋建瓴:二是以 前一直没有写博客的习惯:好了废话不 ...

  4. Java之有病的policy配置

    使用-Djava.security.policy=xxx.policy启动安全策略, 你会想到codesource的配置如此蛋疼么? grant CodeBase "file:////D:/ ...

  5. PCRE正则库的使用

    使用pcre编写C或C++程序,然后编译. 对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcre file.c ...

  6. go语言示例-Timer计时器的用法

    计时器用来定时执行任务,分享一段代码: package main import "time" import "fmt" func main() { //新建计时 ...

  7. [转]Squid中的日志出现TCP_CLIENT_REFRESH_MISS的问题排除

    转自:http://www.php-oa.com/2008/07/15/tcp_client_refresh_miss.html 今天检查Squid发现大量的日志出现TCP_CLIENT_REFRES ...

  8. iOS9之Bitcode

    Bitcode是被编译程序的一种中间形式的代码. 更新Xcode7后需要将”Build Settings”->”Enable Bitcode”设为NO,保证第三方库能真机运行项目.       ...

  9. P3384: [Usaco2004 Nov]Apple Catching 接苹果

    一道DP题, f[i,j,k] 表示 第 k 时刻 由 1 位置 变换 j 次 到达 当前 i 棵树 注意也要维护 变换 0 次的情况. var i,j,k,t,w,now:longint; tree ...

  10. 立即执行函数(IIFE)的理解与运用

    作为JavaScript的常用语法,立即执行函数IIFE(Immediately-Invoked Function Expression)是值得我们认真去学习探究的. 一.创建函数的两种方式 我们先从 ...