A13929 | Knapsack
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You have a knapsack with the capacity of $W$ . There are also $n$ items, the $i$ -th one has weight $w_i$ .
You want to put some of these items into the knapsack in such a way that their total weight $C$ is at least half of its size, but (obviously) does not exceed it. Formally, $C$ should satisfy: $\lceil \frac{W}{2}\rceil \le C \le W$ .
Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible.
If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
You want to put some of these items into the knapsack in such a way that their total weight $C$ is at least half of its size, but (obviously) does not exceed it. Formally, $C$ should satisfy: $\lceil \frac{W}{2}\rceil \le C \le W$ .
Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible.
If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 10^4$ ). Description of the test cases follows.
The first line of each test case contains integers $n$ and $W$ ( $1 \le n \le 200\,000$ , $1\le W \le 10^{18}$ ).
The second line of each test case contains $n$ integers $w_1, w_2, \dots, w_n$ ( $1 \le w_i \le 10^9$ ) — weights of the items.
The sum of $n$ over all test cases does not exceed $200\,000$ .
The first line of each test case contains integers $n$ and $W$ ( $1 \le n \le 200\,000$ , $1\le W \le 10^{18}$ ).
The second line of each test case contains $n$ integers $w_1, w_2, \dots, w_n$ ( $1 \le w_i \le 10^9$ ) — weights of the items.
The sum of $n$ over all test cases does not exceed $200\,000$ .
输出格式
For each test case, if there is no solution, print a single integer $-1$ .
If there exists a solution consisting of $m$ items, print $m$ in the first line of the output and $m$ integers $j_1$ , $j_2$ , ..., $j_m$ ( $1 \le j_i \le n$ , all $j_i$ are distinct) in the second line of the output — indices of the items you would like to pack into the knapsack.
If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
If there exists a solution consisting of $m$ items, print $m$ in the first line of the output and $m$ integers $j_1$ , $j_2$ , ..., $j_m$ ( $1 \le j_i \le n$ , all $j_i$ are distinct) in the second line of the output — indices of the items you would like to pack into the knapsack.
If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
输入输出样例
输入 #1
3 1 3 3 6 2 19 8 19 69 9 4 7 12 1 1 1 17 1 1 1
输出 #1
1 1 -1 6 1 2 3 5 6 7
In the first test case, you can take the item of weight $3$ and fill the knapsack just right.
In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $-1$ .
In the third test case, you fill the knapsack exactly in half.
In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $-1$ .
In the third test case, you fill the knapsack exactly in half.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted