公司组织团建活动,到某漂流圣地漂流,现有如下情况:
员工各自体重不一,第 i 个人的体重为 people[i],每艘漂流船可以承载的最大重量为 limit。
每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit。
为节省开支,麻烦帮忙计算出载到每一个人所需的最小船只数(保证每个人都能被船载)。
第一行输入参与漂流的人员对应的体重数组,
第二行输入漂流船承载的最大重量
所需最小船只数
1 2 3
1
left
和right
指针向中间搜索left==right
,只剩下一个人,count+1
limit
,两个指针向中间移动,count+1
count+1
nums = list(map(int,input().split())) limit = int(input()) count = 0 nums = sorted(nums) left,right = 0,len(nums)-1 while left <= right: if left == right : count += 1 break elif nums[left]+nums[right]<=limit: left += 1 right -= 1 else: right -= 1 count += 1 print(count)
import math import bisect people = sorted(map(int, input().strip().split())) weight = int(input().strip()) count = 0 while people: w = people.pop(0) if not people: count += 1 break ind = bisect.bisect_right(people, weight - w) if ind > 0: people.pop(ind - 1) count += 1 elif ind == 0: # 没找到合适的人,自己乘一辆 count += 1 print(count)