B. Shaass and Bookshelf DP
http://codeforces.com/contest/294/problem/B
据说是贪心,我用了一个复杂度是2e8的dp水过去了。
其实这题就是给你n个数,每个数有两个权值,分成两组,使得第一个权值之和,和第二个权值之和的最大值最小。
那么直接设dp[i][j][k][h]表示前i个数中,选了j个,第一个权值的和是k,第二个权值是h,是否可能。
这里是一定要选出n个的,就是n个数都必须使用,才能滚动数组。(把第二维滚动)
如果是从n个数中选出k个,那么就不能滚动了。
那么第一维是01背包直接省略,j那里可以滚动数组。
总复杂度2e8
然后题解是贪心......
还有要注意,k要大于h,就是底下的书要长于上面的书。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
struct node {
int ti, wi;
}a[];
int dp[][ + ][ + ];
void work() {
int n;
cin >> n;
int DFN = ;
int sumti = , sumwi = ;
for (int i = ; i <= n; ++i) {
cin >> a[i].ti >> a[i].wi;
sumti += a[i].ti;
sumwi += a[i].wi;
}
if (n == ) {
cout << min(a[].ti, a[].wi) << endl;
return;
}
// sort(a + 1, a + 1 + n);
dp[][][] = DFN;
int now = ;
for (int i = ; i <= n; ++i) {
now = !now;
++DFN;
for (int j = sumti; j >= ; --j) {
for (int k = sumwi; k >= ; --k) {
if (j >= a[i].ti && dp[!now][j - a[i].ti][k] == DFN - ) {
dp[now][j][k] = DFN;
}
if (k >= a[i].wi && dp[!now][j][k - a[i].wi] == DFN - ) {
dp[now][j][k] = DFN;
}
}
}
}
int ans = inf;
for (int i = ; i <= sumti; ++i) {
for (int j = ; j <= i; ++j) {
if (dp[now][i][j] == DFN) {
ans = min(ans, max(i, j));
}
}
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
B. Shaass and Bookshelf DP的更多相关文章
- 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 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】
这道题目的意思就是排两排书,下面这排只能竖着放,上面这排可以平着放,使得宽度最小 根据题意可以得出一个结论,放上这排书的Width 肯定会遵照从小到大的顺序放上去的 Because the total ...
- Codeforces 294B Shaass and Bookshelf:dp
题目链接:http://codeforces.com/problemset/problem/294/B 题意: 有n本书,每本书的厚度为t[i],宽度为w[i] (1<=t[i]<=2, ...
- Codeforces K. Shaass and Bookshelf(动态规划三元组贪心)
题目描述: B. Shaass and Bookshetime limit per test 2 secondsmemory limit per test 256 megabytesinput ...
- [CF294B]Shaass and Bookshelf
问题描述 Shaass拥有n本书.他想为他的所有书制作一个书架,并想让书架的长宽尽量小.第i本书的厚度是t[i],且这本书的纸张宽度是w[i].书的厚度是1或2,所有书都有同样的高度(即书架的高是均匀 ...
- Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensi ...
- Codeforces 294B Shaass and Bookshelf(记忆化搜索)
题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...
- [Luogu1848][USACO12OPEN]书架Bookshelf DP+set+决策单调性
题目链接:https://www.luogu.org/problem/show?pid=1848 题目要求书必须按顺序放,其实就是要求是连续的一段.于是就有DP方程$$f[i]=min\{f[j]+m ...
- Codeforces294B - Shaass and Bookshelf(贪心)
题目大意 给你N本书,每本书由一个厚度t[i](1或者2),宽度w[i],高度都是一样,把一些书竖着放,然后一些书横着放在同一层,就像下图那样放: 问你把所有的书放好之后竖着的书的总厚度是多少? 题解 ...
随机推荐
- Microsoft Visual Studio has encountered a problem
VS 2010 断点调试的时候,一运行到断点就报这个错误"Visual Studio has encountered a problem and needs to close", ...
- 新增资产时YTD折旧与累计折旧录入错误如何处理
如新增资产时YTD折旧与累计折旧录入错误,但资产已入账处理,如何处理: 1.需要先报废资产: 2.需要在总账手工帐冲销未冲抵凭证: 3.重新增加资产,录入资产时YTD折旧及累计折旧金额应为0.
- .Net 连接字符串的解释
https://msdn.microsoft.com/zh-cn/library/cc716756.aspx 连接字符串参数 连接字符串的格式是使用分号分隔的键/值参数对列表: keyword1=va ...
- 2016-09-19: linux后台运行
linux后台运行命令两种方式: 1. command & : 后台运行,你关掉终端会停止运行 2. nohup command & : 后台运行,你关掉终端也会继续运行 简介 L ...
- Thinkphp源码分析系列(四)–Dispatcher类
下面我们来分析一下Thinkphp中的url解析和路由调度类.此类主要功能是 // +--------------------------------------------------------- ...
- [转]:C#的ToString如何格式化字符串
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- Session操作
存储API localStorage和sessionStorage通常被当做普通的JavaScript对象使用:通过设置属性来存储字符串值,查询该属性来读取该值.除此之外,这两个对象还提供了更加正式的 ...
- Cordova webapp实战开发(20161207 )
http://www.cnblogs.com/zhoujg/archive/2015/05/28/4534932.html 1.https://www.jetbrains.com/idea/downl ...
- git和nginx安装
原始地址: https://www.zybuluo.com/freeethy/note/192109 git安装 设置git的username和email (注册gitlab的账号密码) $$ git ...
- Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程
原文地址:http://www.osyunwei.com/archives/7378.html 搬运是为了自己找资料方便. 准备篇 一.环境说明: 操作系统:Windows Server 2012 R ...