【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)

题干:

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. 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 next nlines contains two integers ti and wi denoting the thickness and width of the i-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.

Examples

Input

5
1 12
1 3
2 15
2 5
2 1

Output

5

Input

3
1 10
2 1
2 4

Output

3

题目大意:

给出n本书,每本书有厚度t[i]和书的宽度w[i],每本书的高度相同。现在想要制作一个书架,为了节省材料,尽量将书竖着放,剩下的书可以横着放在之前竖着的书的上面(当然宽度的那个面朝自己)。求书架的最小宽度。

解题报告:

因为又是分成了两组,所以又是一道组内贪心组间dp。跟这题很像

思路是这样的:考虑答案,肯定是一些宽度1的和一些宽度2的。而放置下面的书的时候肯定是宽度大的先放,so先组内排个序,然后暴力枚举放在下面的第一组的数量和第二组的数量,用交换法可以证明贪心的正确性。模拟完题意之后,看是否满足要求,满足就更新ans。

注意求前缀和不能读入的时候求,,别偷懒,,因为那时候还没排序呢、、、

这题也可以枚举down的长度,然后贪心填充,然后check一下是否up和down是否满足就可以了。当然check函数中还要暴力枚举选择的宽度为1的个数(也就是On的check),反正还不如第一种方法简单。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX=105;
int n;
int d1[MAX],d2[MAX],sum1[MAX],sum2[MAX];
int main()
{int t,q;int op,w,c1=0,c2=0;int s1,s2,ans=0x3f3f3f3f;cin>>n;for(int i = 1; i<=n; i++){scanf("%d%d",&op,&w);if(op==1) d1[++c1]=w;//,sum1[c1] = d1[c1] + sum1[c1-1];else d2[++c2]=w;//,sum2[c2] = d2[c2] + sum2[c2-1];}sort(d1+1,d1+c1+1,greater<int>()); sort(d2+1,d2+c2+1,greater<int>());for(int i = 1; i<=c1; i++) sum1[i] = d1[i] + sum1[i-1];for(int i = 1; i<=c2; i++) sum2[i] = d2[i] + sum2[i-1];for(int i = 0; i<=c1; i++) {int down = 0;for(int j = 0; j<=c2; j++) {down = i+j*2;int up = (sum1[c1] - sum1[i]) + (sum2[c2] - sum2[j]);if(down >= up) ans = min(ans,down);}}printf("%d\n",ans);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440491.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mac php开发集成环境,MAC OS X下php集成开发环境mamp

之前苦于mac上搭建本地服务器之艰辛&#xff0c;找寻好久都没找到一款类似windows上集成的本地服务器环境&#xff0c;诸如phpstudy&#xff0c;xampp,appserv,虽说xampp也有mac版&#xff0c;但不知为何不是Apache启动不了&#xff0c;这里小编为大家分享了MAC OS X 下php集成…

php获取手机目录,php如何获取手机型号

手机App中判断平台&#xff0c;可以根据$_SERVER[HTTP_USER_AGENT]中的内容来判断浏览器类型或手机平台。(推荐学习&#xff1a;PHP编程从入门到精通)iPhone UA&#xff1a;Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, l…

【CodeForces - 920E】Connected Components? (dsu,补图连通块,STLset+map,bfs 或bitset)

题干&#xff1a; You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices…

【牛客 - 551F】CSL 的神奇序列(推公式,猜结论,母函数)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/551/F 来源&#xff1a;牛客网 题目描述 CSL 有一个神奇的无穷实数序列&#xff0c;他的每一项满足如下关系&#xff1a; 对于任意的正整数 n &#xff0c;有 , 并且 。 CSL 很清楚这样的序列是唯…

java什么时候创建进程,Java创建进程

Java创建进程1 进程的概念 11.1 进程的概念 11.2 进程的特征 11.3 进程与线程区别 12 进程的创建 12.1 JAVA进程的创建 12.1.1 ProcessBuilder 22.1.2 Runtime 32.1.3 Process 42.2 实例 52.2.1 创建子进程 52.2.2 进程阻塞问题 72.2.3 在java中执行java程序 111 进程的概念1.1…

【牛客 - 157E】青蛙(floyd最短路,建图)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/157/E 来源&#xff1a;牛客网 题目描述 有一只可爱的老青蛙&#xff0c;在路的另一端发现了一个黑的东西&#xff0c;想过去一探究竟。于是便开始踏上了旅途 一直这个小路上有很多的隧道&#xff0…

php移动端url,什么是PC和移动端URL路径规范化

什么叫PC和手机端URL途径规范性在网址百度搜索引擎提升的全过程中&#xff0c;会牵涉到途径方位的难题。网址中的同一个网页页面只相匹配一个网站地址。一个规范化和简易的网站地址有利于检索和捕捉客户的记忆力&#xff0c;回绝好几条途径&#xff0c;偏向同一个网页页面&…

matlab的diray在哪,matlab笔记

matlab笔记 目录 P5第一章——matlab 概述与格式 P10eps 浮点相对精度inf 无穷大i 或 j 虚数单位pi 圆周率nan 非数nargin 函数输入变量数目nargout 函数输出变量数目realmax 最大正实数realmin 最小正实数real( ) 实部imag( ) 虚部abs( ) 绝对值angle( ) 复数的相位角**matlab…

【CodeForces - 190E】Counter Attack (补图bfs,卡常,dsu)

题干&#xff1a; 无向图中给定n个顶点&#xff0c;m条不存在的边(除了这m条边&#xff0c;其余都存在)&#xff0c;求图的连通分量&#xff0c;及每个连通分量的大小。 解题报告&#xff1a; https://codeforces.com/blog/entry/4556 AC代码&#xff1a; #include<cstd…

matlab 多径 时变 信道 冲击响应,无线信道—时变冲激响应

图1无线信道的作用可以分成大尺度效应和小尺度效应。大尺度的效应就是改变了信号的平均功率&#xff0c;即B点的功率是A点的1/L。因此可以将图1等效成图2图2其中C点的平均功率等于B点的平均功率。L的数值可根据传播模型确定。影响接收机性能的只是信噪比&#xff0c;因此&#…

matlab中均线交易策略,【每日一策】Matlab量化交易策略之 均线选股策略

策略名称&#xff1a;均线选股策略策略说明&#xff1a;对沪深300全市场扫描买入条件&#xff1a;1 短均线大于长均线2 最近N个交易日短均线大于长均线的次数满足某个阈值3 当前交易日的长均线值处于某个高位出场条件&#xff1a;止损&#xff1a;价格跌破入场价的一定百分比止…

docker php 乱码,如何解决docker安装zabbix5.0界面乱码

如何解决docker安装zabbix5.0界面乱码&#xff1f;zabbix图形界面乱码如下&#xff1a;解决&#xff1a;docker部署zabbix-web和源码安装zabbix-web一样&#xff0c;字体都是存储在/usr/share/zabbix/assets/fonts/1、从windown拷贝simkai.ttf(楷体)文件到docker的zabbix-web里…

【POJ - 1330】Nearest Common Ancestors(lca,模板题)

题干&#xff1a; A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor o…

java redis 流水线,Redis系列(1) —— 流水线

写在前面去年下半年&#xff0c;出于学习Redis的目的&#xff0c;在看完《Redis in Action》一书后&#xff0c;开始尝试翻译Redis官方文档。尽管Redis中文官方网站有了译本&#xff0c;但是看别人翻译好的和自己翻译英文原文毕竟还是有很大的不同。这一系列文章之前发布在GitB…

【HDU - 6187】Destroy Walls(思维,最大生成树)

题干&#xff1a; Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area. Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castl…

【HDU - 6184】Counting Stars(三元环计数,二分,优化暴力,O(m*sqrt(m)),图论)

题干&#xff1a; Little A is an astronomy lover, and he has found that the sky was so beautiful! So he is counting stars now! There are n stars in the sky, and little A has connected them by m non-directional edges. It is guranteed that no edges connec…

php 取oracle图片,在PHP中将图片存放ORACLE中_php

我这里提供一个用php操纵blob字段的例子给你&#xff0c;希望能有所帮助&#xff01;这个例子是把用户上传的图片文件存放到BLOB中。假设有一个表&#xff0c;结构如下&#xff1a;CREATE TABLE PICTURES (ID NUMBER,http://www.gaodaima.com/44856.html在PHP中将图片存放oracl…

【HDU - 6183】Color it(CDQ分治 或 动态开点线段树)

题干&#xff1a; Do you like painting? Little D doesnt like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The speci…

php create()方法,ThinkPHP中create()方法自动验证实例

ThinkPHP中create()方法自动验证实例2020-06-16 04:24:32自动验证是ThinkPHP模型层提供的一种数据验证方法&#xff0c;可以在使用create创建数据对象的时候自动进行数据验证。原理&#xff1a;create()方法收集表单($_POST)信息并返回&#xff0c;同时触发表单自动验证&#x…

【蓝桥杯官网试题 - 历届试题】格子刷油漆(dp)

题干&#xff1a; 问题描述 X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形&#xff08;如下图所示&#xff09;&#xff0c;现需要把这些格子刷上保护漆。   你可以从任意一个格子刷起&#xff0c;刷完一格&#xff0c;可以移动到和它相邻的格子&#xff08;对角相邻也…