小杨买饮料
时间限制:1.0 s
内存限制:128.0 MB
问题描述
小杨来到了一家商店,打算购买一些饮料。这家商店总共出售N种饮料,编号从0至N-1,其中编号为i的饮料售价ci元,容量li毫升。
小杨的需求有如下几点:
1. 小杨想要尽可能尝试不同种类的饮料,因此他希望每种饮料至多购买 1 瓶;
2. 小杨很渴,所以他想要购买总容量不低于L的饮料;
3. 小杨勤俭节约,所以在 1 和 2 的前提下,他希望使用尽可能少的费用。
方便起见,你只需要输出最少花费的费用即可。特别地,如果不能满足小杨的要求,则输出 no solution 。
输入描述
第一行两个整数N,L。
接下来N行,依次描述第i=0,1,...,N-1种饮料:每行两个整数ci,li。
输出描述
输出一行一个整数,表示最少需要花费多少钱,才能满足小杨的要求。特别地,如果不能满足要求,则输出 no
solution 。
特别提醒
在常规程序中,输入、输出时提供提示是好习惯。但在本场考试中,由于系统限定,请不要在输入、输出中附带任何提示信息。
样例输入 1
5 100
100 2000
2 50
4 40
5 30
3 20
样例输出 1
9
样例解释 1
小杨可以购买1,2,4号饮料,总计获得50+40+20=110毫升饮料,花费2+4+3=9元。
如果只考虑前两项需求,小杨也可以购买1,2,4号饮料,它们的容量总和为50+30+20=100毫升,恰好可以满足需求。但遗憾的是,这个方案需要花费2+5+3=10元。
样例输入 2
5 141
100 2000
2 50
4 40
5 30
3 20
样例输出 2
100
样例解释 2
1,2,3,4号饮料总计140毫升,如每种饮料至多购买 1 瓶,则恰好无法满足需求,因此只能花费100元购买0号饮料。
样例输入 3
4 141
2 50
4 40
5 30
3 20
样例输出 3
no solution
数据规模
对于40%的测试点,保证N≤20 ;1≤L≤100 ;li≤100。
对于70%的测试点,保证li≤100。
对于所有测试点,保证1≤N≤500 ;1≤L≤2000 ;1≤ci,li≤106。
相似题推荐
给定一棵二叉树,采用广度优先搜索(BFS) 算法,返回右视图所有节点的值。其中右视图定义为:二叉树的右视图是从树的右侧看过去时可见的节点集合,即右视图中的每个节点都是某一层中最右侧的节点。
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def rightSideView(root):
rightmost_value_at_depth = {}
max_depth = -1
node_queue = []
depth_queue = []
node_queue.append(root)
depth_queue.append(0)
while node_queue:
node = node_queue.pop(0)
depth = depth_queue.pop(0)
if node is not None:
max_depth = max(max_depth, depth)
rightmost_value_at_depth[depth] = node.val
node_queue.append(node.left)
node_queue.append(node.right)
# (1) 填空处
depth_queue.append(depth + 1)
depth_queue.append(depth + 1)
right_view = []
# (2) 填空处
for depth in range(max_depth + 1):
right_view.append(rightmost_value_at_depth[depth])
return right_view
if __name__ == "__main__":
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)
root.right.right = TreeNode(4)
print(rightSideView(root))| A. (1)depth , depth ;(2)0 , max_depth |
B. (1)depth + 1 , depth + 1 ;(2)0 , max_depth + 1 |
| C. (1)depth + 1 , depth + 1 ;(2)1 , max_depth + 1 |
D. (1)depth , depth ;(2)1 , max_depth |
游乐园的过山车每次限坐4 人,用循环队列管理排队(容量MAX=5,空一格判满)。下面代码执行后,循环队列是否已满?rear 的值是多少?
MAX = 5
queue = [None] * MAX
front = 0
rear = 0
def enqueue(x):
global rear, queue
queue[rear] = x
rear = (rear + 1) % MAX
def dequeue():
global front
front = (front + 1) % MAX
if __name__ == "__main__":
enqueue(1)
enqueue(2)
enqueue(3)
enqueue(4)
dequeue()
dequeue()
enqueue(5)
enqueue(6)
print("队列数组: ", queue)
print("front下标位置: ", front)
print("rear下标位置: ", rear)| A. 已满,rear = 1 |
B. 未满,rear = 1 |
| C. 已满,rear = 2 |
D. 未满,rear = 4 |
设二叉树共有n个结点,函数 preorderTraversal 的时间复杂度为O(n),空间复杂度为O(n)。
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def preorder(root, res):
if root is None:
return
res.append(root.val)
preorder(root.left, res)
preorder(root.right, res)
def preorderTraversal(root):
res = []
preorder(root, res)
return res
if __name__ == "__main__":
root = TreeNode(1)
root.right = TreeNode(2)
root.right.left = TreeNode(3)
result = preorderTraversal(root)
print("前序遍历结果: ", result) | A.正确 | B.错误 |
在二叉搜索树(BST)中,若中序遍历的序列为{1, 2, 3, 4, 5},且先序遍历的第一个序列元素为3,则下列说法正确的是()。
| A. 该树一定是一棵完全二叉树。 |
B. 元素4和5不可能是兄弟节点。 |
| C. 元素1所在节点的深度可能大于3(根节点深度为1)。 |
D. 元素2一定是元素1的父节点。 |
