Bookshelf
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7758   Accepted: 3906

Description

Farmer John recently bought a bookshelf for cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

Each of the N cows (1 ≤ N ≤ 20,000) has some height of Hi (1 ≤ Hi ≤ 10,000) and a total height summed across all N cows of S. The bookshelf has a height of B (1 ≤ B ≤ S <
2,000,000,007).

To reach the top of the bookshelf taller than the tallest cow, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height
of the bookshelf. Since more cows than necessary in the stack can be dangerous, your job is to find the set of cows that produces a stack of the smallest number of cows possible such that the stack can reach the bookshelf.

Input

* Line 1: Two space-separated integers: N and B

* Lines 2..N+1: Line i+1 contains a single integer: Hi

Output

* Line 1: A single integer representing the size of the smallest set of cows that can reach the bookshelf.

Sample Input

  1. 6 40
  2. 6
  3. 18
  4. 11
  5. 13
  6. 19
  7. 11

Sample Output

  1. 3

大水题,给出一个数的序列,求最小的数量m,使这序列中m个数的和大于等于给定的数。

从大到小排个序。什么时候大于等于给定数值了,就输出即可。

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. bool cmp(int a,int b)
  8. {
  9. return a>b;
  10. }
  11.  
  12. int main()
  13. {
  14. int N,B,i;
  15. int height[20005];
  16. cin>>N>>B;
  17.  
  18. for(i=0;i<N;i++)
  19. {
  20. cin>>height[i];
  21. }
  22. sort(height,height+N,cmp);
  23.  
  24. int ans=0;
  25. for(i=0;i<N;i++)
  26. {
  27. ans += height[i];
  28. if(ans>=B)
  29. {
  30. cout<<i+1<<endl;
  31. return 0;
  32. }
  33. }
  34. return 0;
  35. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3627:Bookshelf的更多相关文章

  1. Node的关系型数据库ORM库:bookshelf

    NodeJs 关系数据库ORM库:Bookshelf.js bookshelf.js是基于knex的一个关系型数据库的ORM库.简单易用,内置了Promise的支持.这里主要罗列一些使用的例子,例子就 ...

  2. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  3. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  4. POJ 3627 Bookshelf 贪心 水~

    最近学业上堕落成渣了.得开始好好学习了. 还有呀,相家了,好久没回去啦~ 还有和那谁谁谁... 嗯,不能发表悲观言论.说好的. 如果这么点坎坷都过不去的话,那么这情感也太脆弱. ----------- ...

  5. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  6. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  7. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  8. POJ 3281:Dining(最大流)

    http://poj.org/problem?id=3281 题意:有n头牛,f种食物,d种饮料,每头牛有fnum种喜欢的食物,dnum种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种 ...

  9. POJ 3580:SuperMemo(Splay)

    http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...

随机推荐

  1. Nginx 的优势

    Nginx 的优势 在 Java 开发中,Nginx 有着非常广泛的使用,随便举几点: 使用 Nginx 做静态资源服务器:Java 中的资源可以分为动态和静态,动态需要经过 Tomcat 解析之后, ...

  2. 「UVA10559」Blocks

    传送门 Luogu 解题思路 考虑区间 \(\text{DP}\). 设 \(f[i][j][k]\) 表示 \([i,j]\) 这段区间接上后面 \(k\) 个与 \(j\) 颜色相同的块得到的答案 ...

  3. Python 动态从文件中导入类或函数的方法

    假设模块文件名是data_used_to_test.py,放在tests文件夹下 文件夹结构如下: project |-tests |-data_used_to_test.py 文件内包含一个test ...

  4. 记一次菜鸡的低级折腾--WordPress get Webshell(后台文件编辑插马)

    挺简单的一个测试站,开始思路错了,一直去网上找WordPress的漏洞,看有没有什么能利用的,未果,因为这个测试站有些地方并不完善,有的漏洞利用不了,菜鸡的我连弱口令都没猜对,没知识就是这么悲哀. 下 ...

  5. MQTT 协议学习:001-搭建MQTT通信环境,并抓包测试

    背景 目的:了解MQTT 通信的有关概念与流程:方便推算某些数据与文档描述是否一致. 为了能够在保证学习质量的前提下,降低配置环境的门槛,我们将服务器搭建在windwos中,实行内网间的MQTT协议访 ...

  6. POJ 3436:ACM Computer Factory 网络流

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6247   Accepted: 2 ...

  7. 第3节 sqoop:2、sqoop的基本简介和安装

    3. sqoop数据迁移 3.1.概述 sqoop是apache旗下一款“Hadoop和关系数据库服务器之间传送数据”的工具. 导入数据:MySQL,Oracle导入数据到Hadoop的HDFS.HI ...

  8. Day6-T3

    原题目 某个帝国修了一条非常非常长的城墙来抵御外敌,城墙共分N段,每一段用一个整数来描述坚固程度. 过了几年,城墙年久失修,有很多段都己经损坏,于是皇帝决定派你去修理城墙,但是经费有限. 所以你准备先 ...

  9. 10 Class文件结构

  10. Xcode8.0+和最新的Xcode9.0beta安装Alcatraz插件

    1.安装Alcatraz 1.1终端中输入 rm -rf ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/Alcatraz ...