CodeForces 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】
这道题目的意思就是排两排书,下面这排只能竖着放,上面这排可以平着放,使得宽度最小
根据题意可以得出一个结论,放上这排书的Width 肯定会遵照从小到大的顺序放上去的
Because the total thickness of vertical books is fixed it's good to calculate the minimum possible total width of horizontal books.
那么只需要模拟一遍放书的过程即可,不会TLE
不过正统解法是Dp
Dp Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ; int dp[][][]; int main(){
std::ios::sync_with_stdio(false);
int i, j, t, k, u, v, x, y, numCase = ;
int n, a, b;
dp[][][] = ;
cin >> n;
int cur = ;
for(i = ; i < n; ++i){
cin >> a >> b;
for(j = ; j < cur + ; ++j){
for(k = ; k < ; ++k){
dp[i + ][j + a][k] |= dp[i][j][k];
dp[i + ][j][k + b] |= dp[i][j][k];
}
}
cur += a;
}
for(i = ; i < ; ++i){
for(j = ; j < i + ; ++j){
if(dp[n][i][j]){
cout << i << endl;
return ;
}
}
} return ;
}
My Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ; int a[][];
int one, two, n; int main(){
std::ios::sync_with_stdio(false);
int i, j, t, k, u, v, x, y, numCase = ;
cin >> n;
for(u = ; u < n; ++u){
cin >> x >> y;
if(x == ){
a[][one++] = y;
} else{
a[][two++] = y;
}
}
sort(a[], a[] + one);
sort(a[], a[] + two);
int ans = INF;
for(i = ; i <= one; ++i){
for(j = ; j <= two; ++j){
int cur = ;
for(k = ; k < one - i; ++k){
cur += a[][k];
}
for(k = ; k < two - j; ++k){
cur += a[][k];
}
if((i + * j) < ans && (i + * j) >= cur){
ans = i + * j;
}
}
}
cout << ans << endl; return ;
}
CodeForces 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】的更多相关文章
- Codeforces 294B Shaass and Bookshelf:dp
		
题目链接:http://codeforces.com/problemset/problem/294/B 题意: 有n本书,每本书的厚度为t[i],宽度为w[i] (1<=t[i]<=2, ...
 - Codeforces  294B Shaass and Bookshelf(记忆化搜索)
		
题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...
 - Codeforces K. Shaass and Bookshelf(动态规划三元组贪心)
		
题目描述: B. Shaass and Bookshetime limit per test 2 secondsmemory limit per test 256 megabytesinput ...
 - Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP
		
题目链接:http://codeforces.com/contest/294/problem/B B. Shaass and Bookshelf time limit per test 1 secon ...
 - Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
		
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
 - Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
		
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
 - codeforces Good bye 2016 E 线段树维护dp区间合并
		
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
 - Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
		
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
 - HDU - 4722 Good Numbers 【找规律 or 数位dp模板】
		
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number ...
 
随机推荐
- 合理设计C代码 函数笔记
			
本文首先用判断一个数字是否为素数使用For循环实现做例子,之后用函数完成多个功能. 什么是素数? 素数又成质数,不包含1和0.通俗的去说就是它除了能表示为它自己和1的乘积以外,不能表示为任何其它两个整 ...
 - JVM典型配置
			
堆大小设置: JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存 限制.32位系统下,一般限制在1.5G~2G:64为 ...
 - synchronized和vilatile
			
第一个程序 public class Test06 implements Runnable{ public int a = 0; public static void main(String[] ar ...
 - JS中的RegExp对象常用属性和方法
			
JavaScript提供了一个RegExp对象来完成有关正则表达式的操作和功能,每一条正则表达式模式对应一个RegExp实例.有两种方式可以创建RegExp对象的实例. 使用RegExp的显式构造函数 ...
 - 转: css3动画简介以及动画库animate.css的使用
			
~~~ transition animation 和 animate.css 在这个年代,你要是不懂一点点css3的知识,你都不好意思说你是个美工.美你妹啊,请叫我前端工程师好不好.呃..好吧,攻城 ...
 - AttributeError: 'module' object has no attribute 'Thread'
			
$ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last): File "th ...
 - 用Mediawiki做百科网站资源大参考
			
MediaWiki简易安装教程**关于mediawiki 一些好的资料: http://codex.wordpress.org.cn/Mediawiki%E5%BB%BA%E7%AB%99%E7%BB ...
 - live555学习经验链接一
			
live555学习经验链接:http://xingyunbaijunwei.blog.163.com/blog/#m=0&t=1&c=fks_084071082087086069082 ...
 - 外网訪问内网应用实现之无公网IP、多port、固定port、UDP等应用的实现方法
			
有公网IP时,能够通过路由映射来实现外网訪问内网.然,当没有公网IP时,怎样实现外网訪问内网应用? 硬件路由方法因为无公网不可行,能够使用软件port映射的方法.如开放的NAT123全port映射. ...
 - nodejs学习笔记_nodejs和PHP在基础架构上的差别--共享状态的并发
			
绝大多数对于Node.js的讨论都把关注点放在了处理高并发能力上,做开发的时候一定要明确node内部做出的权衡,以及node应用性能好的原因. node 为javascript引入了一个复杂的概念,: ...