A. Knapsack
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have a knapsack with the capacity of WW. There are also nn items, the ii-th one has weight wiwi.
You want to put some of these items into the knapsack in such a way that their total weight CC is at least half of its size, but (obviously) does not exceed it. Formally, CC should satisfy: ⌈W2⌉≤C≤W⌈W2⌉≤C≤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.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.
The first line of each test case contains integers nn and WW (1≤n≤2000001≤n≤200000, 1≤W≤10181≤W≤1018).
The second line of each test case contains nn integers w1,w2,…,wnw1,w2,…,wn (1≤wi≤1091≤wi≤109) — weights of the items.
The sum of nn over all test cases does not exceed 200000200000.
Output
For each test case, if there is no solution, print a single integer −1−1.
If there exists a solution consisting of mm items, print mm in the first line of the output and mm integers j1j1, j2j2, ..., jmjm (1≤ji≤n1≤ji≤n, all jiji 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.
Example
input
Copy
3 1 3 3 6 2 19 8 19 69 9 4 7 12 1 1 1 17 1 1 1
output
Copy
1 1 -1 6 1 2 3 5 6 7
Note
In the first test case, you can take the item of weight 33 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−1.
In the third test case, you fill the knapsack exactly in half.
贪个心就行
- #include
- using namespace std;
- typedef long long int ll;
-
- typedef struct
- {
- int id;
-
- ll val;
- }xinxi;
-
- xinxi a[200000+10];
- vector<int>v;
- bool cmp(xinxi a,xinxi b)
- {
- return a.val
- }
- int main()
- {
-
- //有在范围内的直接选
- //没有的话说明剩下的都是小于等于的,那么从大到小选择
-
- int t;
-
- cin>>t;
-
- while(t--)
- {
- int n;
-
- cin>>n;
-
- ll w;
-
- cin>>w;
-
- ll ans=0;
- ll left=w/2;
-
- if(w%2)
- left++;
-
- for(int i=1;i<=n;i++)
- {
- cin>>a[i].val;
- a[i].id=i;
- if(a[i].val>=left&&a[i].val<=w)
- {
- ans=i;
-
- }
-
- }
-
- if(ans)
- {
- cout<<1<
- continue;
-
- }
-
- sort(a+1,a+1+n,cmp);
- ll nowsum=0;
- int flag=0;
-
- for(int i=n;i>=1;i--)
- {
- ll temp=nowsum+a[i].val;
-
- if(temp>=left&&temp<=w)
- {
- nowsum=temp;
- flag=1;
- v.push_back(a[i].id);
- break;
- }
- else if(temp
- {
- nowsum=temp;
- v.push_back(a[i].id);
- }
- }
-
- if(flag)
- {
- cout<
size()< -
- for(auto it:v)
- {
- cout<
" "; - }
- cout<
- }
- else
- {
- cout<<-1<
- }
-
- v.clear();
-
- }
-
-
- return 0;
- }
-
相关阅读:
《C和指针》读书笔记(第十四章 预处理器)
【三维目标检测】VoteNet(二)
Darty自养号测评下单支付方式和注册账号手法有哪些要求?
Python 用户输入和字符串格式化指南
ABAP json解析使用引用代替预定义数据结构
SpringBoot开发实用篇(3)—整合第三方技术
terraform简单的开始-vpc cvm创建
IB中文A看人文教育
矿大数据结构实验四 折半查找 二叉搜索树 最短路径 排序
【线性代数 & C++】结合逆矩阵的克拉默法则
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126199771