题目链接
Alyona has recently bought a miniature fridge that can be represented as a matrix with h rows and 2 columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.
                                                                                          
An example of a fridge with h=7 and two shelves. The shelves are shown in black. The picture corresponds to the first example.
Alyona has n bottles of milk that she wants to put in the fridge. The i-th bottle is ai cells tall and 1 cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.

Alyona is interested in the largest integer k such that she can put bottles 1, 2, …, k in the fridge at the same time. Find this largest k.

Input
The first line contains two integers n and h (1≤n≤103, 1≤h≤109) — the number of bottles and the height of the fridge.

The second line contains n integers a1, a2, …, an (1≤ai≤h) — the heights of the bottles.

Output
Print the single integer k — the maximum integer such that Alyona can put the bottles 1, 2, …, k in the fridge at the same time. If Alyona can put all bottles in the fridge, print n. It is easy to see that Alyona can always put at least one bottle in the fridge.

input
5 7
2 3 5 4 1

output
3

input
10 10
9 1 1 1 1 1 1 1 1 1

output
4

input
5 10
3 1 4 2 4

output
5

Note
One of optimal locations in the first example is shown on the picture in the statement.

One of optimal locations in the second example is shown on the picture below.
                                                                                       

One of optimal locations in the third example is shown on the picture below.
                                                                                       

题解:
边读数据边排序,每两个一组

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n,h,sum,a[N];
int main(){scanf("%d%d",&n,&h);for(int i = 0;i < n;i++){scanf("%d",a + i);sort(a,a + i + 1);sum = 0;for(int j = i;j >= 0;j -= 2)sum += a[j];if(sum > h){//如果和(mx)大于高度(h),就退出printf("%d\n",i);break;}} if(sum <= h)//如果高度最后用不完,就说明能全部装下,输出n即可printf("%d\n",n);return 0;
}

B. Alyona and a Narrow Fridge相关推荐

  1. B. Alyona and a Narrow Fridge 【 思维题 】

    B. Alyona and a Narrow Fridge Codeforces Global Round 2 &:这题看到了一种很简单的做法,于是按照敲一波,比原来打的好看多了. 题意:给你 ...

  2. codeforces1119B. Alyona and a Narrow Fridge 贪心

    神奇的传送门 现在给了一个冰箱,有两格宽,H格高,还有N瓶牛奶,每个牛奶都有一个高度,求最多能放进前几个瓶子 二分枚举,贪心验证.当前这些瓶子中,最高的能放下,这组序列才有可能放下. 二分枚举前n个瓶 ...

  3. Codeforces Global Round 2 B. Alyona and a Narrow Fridge(二分)

    题目链接:https://codeforces.com/contest/1119/problem/B        题意是给了n个水瓶,一个高度h,现在要把这n个水瓶按顺序放到h*2的柜子里,柜子里可 ...

  4. B. Alyona and a Narrow Fridge( Codeforces Global Round 2)二分

    题目地址:http://codeforces.com/contest/1119/problem/B #include <iostream> #include <cstdio> ...

  5. Codeforces Global Round 2 B. Alyona and a Narrow Fridge

    题意:有一个冰箱,你可以放若干个隔板,将冰箱分成高度不等的若干层,然后再给你n瓶高度不一样的牛奶,你要按顺序放尽可能多的牛奶,每层牛奶最多只能放两瓶,并且牛奶瓶的高度不能超过该层的长度. 思路:之前两 ...

  6. Codeforces Global Round 2(CF1119)

    这场题目设置有点问题啊,难度:Div.2 A->Div.2 B->Div.2 D->Div.2 C->Div.2 D->Div.1 D-> Div.1 E-> ...

  7. Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题

    A. Alyona and Numbers 题目连接: http://www.codeforces.com/contest/682/problem/A Description After finish ...

  8. B. Alyona and a tree(dsu on tree + bit)

    B. Alyona and a tree(dsu on tree + bit) 给定一颗以111号节点为根的树,每个点有点权aia_iai​,边有边权,如果vvv控制了点uuu,当且仅当uuu是vvv ...

  9. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    传送门 文章目录 题意: 思路: 题意: 你需要确定一个长度为nnn的数组aaa,满足给定的mmm个[l,r][l,r][l,r]限制,需要保证构造出来的aaa数组对于每个[l,r][l,r][l,r ...

最新文章

  1. EFMVC - ASP.NET MVC 3 and Entity Framework 4.1 Code First 项目介绍
  2. 两所大学中的智能车竞赛校内赛
  3. php空格是什么,php删除空格函数是什么
  4. SpringMVC 静态资源CSS,JS访问不了 解决方法
  5. 关于STM32使用RTC时复位后程序死在 RTC
  6. AT3877-[ARC089C]GraphXY【构造】
  7. 大数据分析有什么难题
  8. MySQL导出表结构
  9. springboot mybatis如何打印出查询语句_Java 面试,如何坐等 offer?
  10. C语言程序设计----C语言基础知识
  11. Android对接蓝牙打印机
  12. 生活中的逻辑谬误05.特例谬误
  13. 计算机专业html5的毕业论文范文,5000字计算机专业大专毕业论文参考范文
  14. 金蝶加密服务器出现系统错误,金蝶KIS访问加密服务器失败,可能加密服务器未启动,错误代码5...
  15. 《非理性繁荣》读书笔记优秀作文5000字
  16. 一个技术总监的忠告:精通那么多技术为何还是做不好一个项目?
  17. 淘宝打标API,旺旺打标签接口文档
  18. VMware安装Win11+WSA子系统和使用教程
  19. 【数据库技术课程设计】 电信学院考研信息管理系统 +【Visual FoxPro】
  20. java中flist cannot_关于usr/bin/ld: cannot find -lxxx问题总结

热门文章

  1. 基于RealSense的坐姿检测技术
  2. C#程序崩溃捕获与自动重启方法(简洁有效)
  3. 微信小程序项目实例——食堂吃哪个
  4. 2022年全球市场艰难梭菌的分子诊断总体规模、主要生产商、主要地区、产品和应用细分研究报告
  5. 副族元素从上到下原子半径_原子结构与原子半径.PPT
  6. yota3墨水屏调节对比度_YOTA3墨水屏“直通”技术取代旧“投射”功能,体验全面升级-非常在线...
  7. ffmpeg截取视频片段(傻瓜教程)
  8. acer台式电脑怎么重装系统_宏基台式机装系统 手把手教你宏基台式机装系统方法...
  9. Hbuilder云打包安心打包错误问题总结(非法字符打包失败问题+win7非法选项:RSA导致不能生成签名)
  10. Windows Performance Toolkit(WPT)入门