首页 > 试题广场 >

小选货架

[编程题]小选货架
  • 热度指数:622 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小选线下店最近准备新上架一批长度不等的商品, 用一个数组表示商品的长度,已知货架每一层的长度固定为X。

小选线下店是一个追求生活美学的店铺,为了摆放美观,每一层至多摆放两个商品,而且商品的总长度不能比货架长度长(已知单个商品的长度都不会比货架长)

请问至少需要多少层的货架,才能漂亮的摆放这些商品呢?


输入描述:
共两行
第一行为一个整数,表示货架的长度X
第二行为一组整数数组,由空格分割,数组中的值表示商品的长度


输出描述:
仅一行一个整数表示答案,即最少需要的货架层数
示例1

输入

3
1 2

输出

1

说明

(1,2)摆放在同一层货架即可  
示例2

输入

3
3 3 2 2 1

输出

4

说明

(3)(3)(2,1)(2) 四层货架摆放  
X = int(input())
arr = sorted(list(map(int, input().split(" "))), reverse = True) 
n = 0
 
while len(arr)>1:
    if X>=arr[0]+arr[-1]:
        arr.pop(0) #放个大的
        arr.pop(-1) #放个小的
    else:
        arr.pop(0) #只能放一个大的
    n+=1
 
if len(arr)==1:
    n+=1 #就一个或剩一个,独放一层
print(n)
发表于 2021-09-18 00:59:46 回复(0)
X = int(input())
goods_len = sorted(list(map(int, input().strip().split())))
n = len(goods_len)
count = 0
# 先考虑一层货架放两个商品的情况,要想尽可能少用货架,就用一大搭一小
for i in range(n):
    for j in range(n - 1, -1, -1):
        if goods_len[i] and goods_len[j] and goods_len[i] + goods_len[j] <= X:
            # i和j两个商品能够摆在一层货架上
            count += 1          # 使用货架数自增
            # 这两个商品清除
            goods_len[i] = 0
            goods_len[j] = 0
# 再加上落单的商品,每个商品占一层货架
print(count + len([1 for item in goods_len if item]))

发表于 2021-01-19 13:01:32 回复(1)
import java.util.*;


public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.nextLine();
        int q = Integer.parseInt(n);
        String s = sc.nextLine();
        String[] ss = s.split(" ");
        int[] arr = new int[ss.length];
        int i = 0;
        for(String num: ss){
            arr[i++] = Integer.parseInt(num);
        }
        Arrays.sort(arr);
        int count = 0;
        int m = arr.length-1;
        int k = 0;
        while (m >= 0 && k <= arr.length - 1 && m >= k){
            if(arr[m] == q){
                m--;
                count++;
            }else{
                if(arr[m] + arr[k] <= q){
                    count++;
                    m--;
                    k++;
                }else{
                    count++;
                    m--;
                }
            }
        }
        System.out.println(count);
    }
}

发表于 2021-07-08 05:23:43 回复(0)
这题答案有问题吧... 
51
1 1 2 2 3 3 5 6 6 7 7 8 8 8 10 10 11 11 11 12 13 14 14 14 14 15 16 16 16 16 17 18 19 20 20 22 22 22 23 23 24 25 25 27 27 27 28 28 30 30 30 32 32 33 34 34 35 35 36 37 38 38 38 38 39 39 41 41 42 42 42 43 43 44 46 46 47 47 47 48 49 49 50 50 50 51 51 51
这组数据的最优解是47 不是 48呀

发表于 2021-01-19 21:05:06 回复(1)
n = int(input())
goods = [int(i) for i in input().split()]
#倒序排列
goods = sorted(goods)[::-1]
res = 0
#双指针
left = 0
right = len(goods)-1
while left <= right:
    #如果刚好能装下当前商品
    if goods[left] == n:
        res+=1
    #还有剩余空间,从最右边选一个看看能不能装下
    else:
        if goods[right] <= n - goods[left]:
            right-=1
        res+=1
    left+=1
print(res)
               

发表于 2022-08-20 11:58:19 回复(0)
n=int(input().strip())
A=list(map(int,input().strip().split()))
A=sorted(A,reverse=True)  #降序排序
c=0                       #c记录需要多少个货架
j=0
i=len(A)-1
while (j<len(A) and i>=0 and i>=j):
    if A[j]==n :          #长度刚好相等,需一个货架
        c=c+1                         
        j=j+1
        continue
    else:                             #已经拿完前面相等的
        good=A[j]
        if good+A[i]<=n:              #看看能不能从最后哪里拿一个
            i=i-1
        c=c+1                         #就是不能从后面拿,也需要一个货架
        j=j+1
print(c)

发表于 2021-05-18 16:31:05 回复(1)
max_x = int(input())
arr = list(map(int,input().split(' ')))
arr.sort()
i,j = 0,len(arr)-1
cout=0
while i<j:
    if arr[i]+arr[j]<=max_x:
        i+=1
        j-=1
        cout+=1
    else:
        j-=1
        cout+=1
if i==j:cout+=1
print(cout)
发表于 2021-03-30 10:41:05 回复(0)