Codeforces K. Shaass and Bookshelf(动态规划三元组贪心)
题目描述:
B. Shaass and Bookshe
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of thei-th book isti and its pages' width is equal towi. The thickness of each book is either1 or2. All books have the same page heights.
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n,(1 ≤ n ≤ 100). Each of the nextn lines contains two integersti andwi denoting the thickness and width of thei-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Sample test(s)
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
思路:
题目是要把书要么横着放,要么竖着放,要求在横着放的宽度不大于竖着放的宽度下的数着宽度的最小值。
刚开始:既然是DP,我就设状态,dp[i][j]表示放前i本书,横着放的宽度不大于j的情况下的,竖着宽度的最小值。
状态出来了,那我就转移吧。dp[i][j] = dp[i-1][j]+v[i](表示第i本书我竖着放),dp[i][j] = min(dp[i][j],dp[i][j]-w[i])(表示第i本书我横着放)。
但是,有问题,这样做求不出正确答案,因为不一低满足上面的数的长度小于等于下面的书的长度,最后就算在dp[n][i]中找满足条件的,但在dp数组计算过程中就可能已经把正确答案消掉了。(如果有dalao是类似于这种定义dp做出来的,请不吝赐教,欢迎留言)
然后:改一种定义方法:dp[i][j]表示当前放低i本书,竖着放的长度是j的情况下,横着放的长度的最小值,转移方程是,dp[i][j] = dp[i-1][j]+w[i](我横着放),dp[i][j]=min(dp[i][j],dp[i-1][j-v[i]])(我竖着放)
最后在dp[n][i]里找满足条件的最小i,就是解
代码:
#include <iostream>
#include <memory.h>
#define max_n 105
#define INF 0x3f3f3f3f
using namespace std;
int w[max_n];
int v[max_n];
int dp[max_n][max_n*];
int n;
int C = ;
int main()
{
cin >> n;
for(int i = ;i<=n;i++)
{
cin >> v[i] >> w[i];
C+=v[i];
}
memset(dp,0x3f,sizeof(dp));
dp[][] = ;
/*for(int i = 0;i<=n;i++)
{
for(int j = 0;j<=C;j++)
{
cout << dp[i][j] << " ";
}
cout << endl;
}*/
int tot = ;
for(int i = ;i<=n;i++)
{
tot+=v[i];
for(int j = ;j<=tot;j++)
{
dp[i][j] = dp[i-][j]+w[i];
if(j>=v[i]) dp[i][j] = min(dp[i][j],dp[i-][j-v[i]]);
}
}
/*for(int i = 0;i<=n;i++)
{
for(int j = 0;j<=C;j++)
{
cout << dp[i][j] << " ";
}
cout << endl;
}*/
int minm = INF;
for(int i = ;i<=C;i++)
{
if(i>=dp[n][i])
{
cout << i << endl;
break;
}
}
return ;
}
还有一种做法,好理解,但内存占用比较大,定义三元组dp[i][j][k],(还好数据小),表示放第i本书后竖着摆长度为j横着摆长度k的状态是否存在,是为1,不是为0
转移方程是在三重循环下,if(j>=v[j]&&dp[i-1][j-v[i]][k]) dp[i][j][k] = 1;if(k>w[i]&&dp[i][j][k-w[i]) dp[i][j][k] = 1;
最后遍历一遍找最小的满足dp[n][j][k]的j
代码:
#include <iostream>
#include <memory.h>
#define max_n 105
#define INF 0x3f3f3f3f
using namespace std;
int w[max_n];
int v[max_n];
int dp[max_n][max_n*][max_n*];
int n;
int C = ;
int main()
{
cin >> n;
for(int i = ;i<=n;i++)
{
cin >> v[i] >> w[i];
C+=v[i];
}
dp[][][] = ;
for(int i = ;i<=n;i++)
{
for(int j = ;j<=C;j++)
{
for(int k = ;k<=;k++)
{
if(j>=v[i]&&dp[i-][j-v[i]][k])
{
dp[i][j][k] = ;
}
if(k>=w[i]&&dp[i-][j][k-w[i]])
{
dp[i][j][k] = ;
}
}
}
}
int flag = ;
for(int j = ;j<=C&&flag;j++)
{
for(int k = ;k<=j&&flag;k++)
{
if(dp[n][j][k])
{
cout << j << endl;
flag = ;
}
}
}
return ;
}
最后,听说可以用贪心来枚举做哦,试一试。因为书的厚度只有两种,1或2,那么将书分类,在每一类中以书的宽度降序排序,最后要使竖着放的宽度最小,就把每类书的前i,j本书竖着放(因为宽度相对大)二重循环枚举出所有i,j,求满足条件的最小值。编码5分钟,调bug两小时,,,结果发现错在了cmp函数上,合着sort的cmp和qsort的还不一样啊,还有就是求和数组的下标让我疯狂。((╯‵□′)╯︵┻━┻)
代码:
#include <iostream>
#include <algorithm>
#define max_n 105
#define INF 0x3f3f3f3f
using namespace std;
int n;
struct book
{
int v;
int w;
};
book bk1[max_n];
book bk2[max_n];
int cnt1 = ;
int cnt2 =;
int sum1[max_n];//记录前i本1类书宽度和
int sum2[max_n];//记录前i本2类书宽度和
int cmp(book a,book b)
{
return a.w>b.w;
}
int main()
{
cin >> n;
int v,w;
for(int i = ;i<n;i++)
{
cin >> v >> w;
if(v==)
{
bk1[cnt1].v = v;
bk1[cnt1].w = w;
cnt1++;
}
if(v==)
{
bk2[cnt2].v = v;
bk2[cnt2].w = w;
cnt2++;
}
}
//cout << cnt1 << " " << cnt2 << endl;
sort(bk1,bk1+cnt1,cmp);
sort(bk2,bk2+cnt2,cmp);
/*for(int i = 0;i<cnt2;i++)
{
cout << bk2[i].w << " " << endl;
}*/
sum1[] = ;
sum1[] = bk1[].w;
for(int i = ;i<=cnt1;i++)
{
sum1[i] = sum1[i-]+bk1[i-].w;
}
sum2[] = ;
sum2[] = bk2[].w;
for(int i = ;i<=cnt2;i++)
{
sum2[i] = sum2[i-]+bk2[i-].w;
}
//cout << sum1[cnt1] << " and " << sum2[cnt2] << endl;
int sum = ;
int ans = INF;
for(int i = ;i<=cnt1;i++)
{
for(int j = ;j<=cnt2;j++)
{
sum = i+*j;
w = sum1[cnt1]-sum1[i]+sum2[cnt2]-sum2[j];
//cout << "i " << i << " j " << j << " sum " << sum << " w " << w << endl;
if(w<=sum&&sum<ans)
{
ans = sum;
}
}
}
cout << ans << endl;
return ;
}
Codeforces K. Shaass and Bookshelf(动态规划三元组贪心)的更多相关文章
- 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 294B Shaass and Bookshelf(记忆化搜索)
题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...
- 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 ...
- 【CF1097E】Egor and an RPG game(动态规划,贪心)
[CF1097E]Egor and an RPG game(动态规划,贪心) 题面 洛谷 CodeForces 给定一个长度为\(n\)的排列\(a\),定义\(f(n)\)为将一个任意一个长度为\( ...
- 【CF183D】T-shirt(动态规划,贪心)
[CF183D]T-shirt(动态规划,贪心) 题面 洛谷 CodeForces 题解 \(O(n^2m)\)的暴力懒得写了,比较容易,可以自己想想. 做法是这样的,首先我们发现一个结论: 对于某个 ...
- 【BZOJ1046】上升序列(动态规划,贪心)
[BZOJ1046]上升序列(动态规划,贪心) 题面 BZOJ 洛谷 题解 我一开始看错题了,一度以为是字典序最小的序列. 最后发现它要求的字典序是位置的字典序最小. 那就很好办了. 设\(f[i]\ ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- 【CF1133E】K Balanced Teams(动态规划,单调队列)
[CF1133E]K Balanced Teams(动态规划,单调队列) 题面 CF 让你把一堆数选一些出来分成不超过\(K\)组,每一组里面的最大值和最小值之差不超过\(5\),求最多有多少个人元素 ...
随机推荐
- python:pytest中的setup和teardown
原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...
- mysql查询之 用户行程的取消率,人流量高峰时段
1.用户行程的取消率 Trips 表中存所有出租车的行程信息.每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键.Status 是枚举类 ...
- Consul ACL集群配置说明以及ACL Token的用法
在上一篇文章里面,我们讲了如何搭建带有Acl控制的Consul集群.这一篇文章主要讲述一下上一篇文章那一大串配置文件的含义. 1.配置说明#1.1 勘误上一篇文章关于机器规划方面,consul cli ...
- python jieba 词云
#!/usr/bin/python # coding:utf-8 # 绘制一个<三体>全集词云 # pip install jieba # pip install matplotlib # ...
- python time模块(13)
python time模块主要包含各种提供日期.时间功能的相关函数.time模块既提供了把日期.时间格式化为字符串的功能,也提供了从字符串恢复日期.时间的功能. 一.前言 在 time 模块内提供了很 ...
- 模块 logging random
模块logging logging模块的主要功能是记录软件调试.操作过程中的各种日志. 默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志, ...
- [SOJ #112]Dirichlet 前缀和
题目大意:给定一个长度为$n$的序列$a_n$,需要求出一个序列$b_n$,满足:$$b_k=\sum\limits_{i|k}a_i$$$n\leqslant10^7$ 题解:$\mathrm{Di ...
- MOOC web前端开发笔记(二)
HTML HTML概述 HTML(HyperText MarkUp Language) "超文本标记语言",以标签的形式规定网页结构,它是制作网页的标准语言 HTML不区分大小写 ...
- WCF NetTcpBinding
服务端: <system.serviceModel> <bindings> <netTcpBinding> <binding portSharingEnabl ...
- iOS - app 进行安全加固
研究了大半年逆向工程了,没在博客做记录,最近看到篇,跟自己的想法不谋而合,摘要下: 运行在越狱设备上的 iOS app,非常容易遭到破解分析,这里我列举一些可以加大破解难度的方法,希望有所帮助. 一些 ...