codeforces 294B Shaass and Bookshelf

题意:给你n本书,你可以把它竖着摆放,然后也可以横着摆在竖着的书上面,但不能超过竖着摆放的边缘,且不可以堆叠。问你竖着摆放的最小宽度是多少。

解题思路:dp,dp[i][j] 代表 第i个 ,用竖着摆放为 j 的书横着摆放的最小值

解题代码:

 1 /************************************************************
 2  * Author : darkdream
 3  * Email : [email protected]
 4  * Last modified : 2015-03-09 20:32
 5  * Filename : 294b.cpp
 6  * Description :
 7  * *********************************************************/
 8 // File Name: 294b.cpp
 9 // Author: darkdream
10 // Created Time: 2015年03月09日 星期一 20时04分53秒
11
12 #include<vector>
13 #include<list>
14 #include<map>
15 #include<set>
16 #include<deque>
17 #include<stack>
18 #include<bitset>
19 #include<algorithm>
20 #include<functional>
21 #include<numeric>
22 #include<utility>
23 #include<sstream>
24 #include<iostream>
25 #include<iomanip>
26 #include<cstdio>
27 #include<cmath>
28 #include<cstdlib>
29 #include<cstring>
30 #include<ctime>
31 #define LL long long
32
33 using namespace std;
34 struct node{
35   int t , w;
36 }a[200];
37 int dp[200][300];
38 int main(){
39     int n;
40     scanf("%d",&n);
41     memset(dp,-1,sizeof(dp));
42     int total = 0 ;
43     for(int i = 1;i <= n;i ++)
44     {
45       scanf("%d %d",&a[i].t,&a[i].w);
46       total += a[i].t ;
47     }
48     dp[0][0] = 0 ;
49     int mi = 1e9 ;
50     for(int i = 1;i <= n;i ++)
51     {
52         for(int j = 0 ;j <= total;j ++)
53         {
54            dp[i][j]  = dp[i-1][j];
55         }
56         for(int j = 0 ;j <= total;j ++)
57         {
58            if(dp[i-1][j] != -1)
59            {
60               if(dp[i-1][j+a[i].t] == -1)
61                   dp[i][j+a[i].t] = dp[i-1][j] + a[i].w;
62               else dp[i][j+a[i].t] =  min(dp[i][j+a[i].t],dp[i-1][j] + a[i].w);
63            }
64         }
65     }
66     for(int i = 1;i <= n;i ++)
67     {
68         for(int j= 0 ;j <= total ;j ++)
69         {
70          // printf("%d ",dp[i][j]);
71           if(dp[i][j] <= total -j && dp[i][j] != -1)
72               mi = min(mi,total - j );
73         }
74         //printf("\n");
75
76     }
77     printf("%d\n",mi);
78 return 0;
79 }

时间: 2024-08-06 03:47:08

codeforces 294B Shaass and Bookshelf的相关文章

Codeforces 294B Shaass and Bookshelf(记忆化搜索)

题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dp[110][210][210];//dp[第几个][厚度][宽度] int n; int a[110],b[110]; int rec(int i,int th,

Codeforces 294B Shaass and Bookshelf:dp

题目链接:http://codeforces.com/problemset/problem/294/B 题意: 有n本书,每本书的厚度为t[i],宽度为w[i] (1<=t[i]<=2, 1<=w[i]<=100). 然后让你将所有书按照下面的方式摆放: 在下面放一本书会占用下面t[i]的长度. 在上面放一本书会占用上面w[i]的长度. 最终要保证上面的总长度不超过下面的总长度. 问你下面的总长度最小是多少. 题解: 表示状态: dp[i][j] = min length 表示已经

CodeForces 294B Shaass and Bookshelf 【规律 &amp; 模拟】或【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

codeforces A. Shaass and Oskols 题解

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delici

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 dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2.

CodeForces - 294A Shaass and Oskols

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delici

[CF294B]Shaass and Bookshelf

问题描述 Shaass拥有n本书.他想为他的所有书制作一个书架,并想让书架的长宽尽量小.第i本书的厚度是t[i],且这本书的纸张宽度是w[i].书的厚度是1或2,所有书都有同样的高度(即书架的高是均匀的). Shaass以以下的方式摆放这些书籍. 1.他选择了一些书并竖直摆放它们. 2.他将剩余的书籍水平纺织于竖直的书上面. 水平放置的书的宽度和不能多于竖直放置的书的总厚度.图中描绘了书籍的样本排列. 帮助Shaass找到可以达到的书架长度最小值. 输入格式 输入的第一行包含一个int型的整数n

Codeforces 294E Shaass the Great 树形dp(水

题目链接:点击打开链接 题意: 给定n个点的树,任意拆掉一条边,得到2个子树,再用刚拆掉的边把这两个子树连起来. 得到新的树,这个树的权值为任意两个点间的距离和. 使得新的树权值最小.输出这个权值. 枚举拆掉的边(u,v) 得到2个以u为根的子树和以v为根的子树 计算每条边对答案的贡献,拆掉的边贡献就是siz[u]*siz[v]*edge[u,v].dis 剩下的就是计算如何连接2个子树使得权值和最小. 对于子树中的一条边x, y,若已知两端的节点数为i,j,则这条边对答案的贡献就是 i*j*e

#Shaass and Lights:CodeForces - 294C

1 /******************************************************************************************************************************************************************************* 2 题目大意:给出一列灯,初始有一些点亮的灯,每次只能点亮与已经点亮的灯相邻的灯,问点亮所有灯的方案有多少种 3 题解: 4 首先利用初始已经