CF 1447 C. Knapsack (贪心)

You have a knapsack with the capacity of W. There are also n items, the i-th one has weight wi.

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: ⌈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 t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains integers n and W (1≤n≤200000, 1≤W≤1018).

The second line of each test case contains n integers w1,w2,…,wn (1≤wi≤109) — weights of the items.

The sum of n over all test cases does not exceed 200000.

Output
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 j1, j2, …, jm (1≤ji≤n, all ji 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
3
1 3
3
6 2
19 8 19 69 9 4
7 12
1 1 1 17 1 1 1
output
1
1
-1
6
1 2 3 5 6 7
Note
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.

题意
给你一个背包和若干物品,要求将背包至少塞一半,且不能超过背包容量,输出你选择放入的索引。

思路
用pair或者结构体记录id,将其从大到小排序,然后能放的就放进去,这样就行了,贪心。

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod =1e9 + 7;
using namespace std;
namespace fastIO {
    inline void input(ll& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 1e6+5;
ll Case,n,w;
ll b[N];
struct node{
	ll a;
	ll id;
}zr[N];
bool cmp(node a,node b){
	return a.a>b.a;
}
int main(){
	Case=1;
	input(Case);
	while(Case--){
		int cnt = 0;
		input(n);input(w);
		ll t = w;
		for(int i=1;i<=n;i++) input(zr[i].a),zr[i].id=i;
		sort(zr+1,zr+n+1,cmp);
		for(int i=1;i<=n;i++) {
			if(zr[i].a<=w){
				w-=zr[i].a,b[cnt++]=zr[i].id;
			}
		}
		if(cnt==0||w>t/2) printf("-1");
		else {
			printf("%d\n",cnt);
			for(int i=0;i<cnt;i++) printf("%lld ",b[i]);
		}puts("");
		
		
	}
	return 0;
}
/*
3
1 3
3
6 2
19 8 19 69 9 4
7 12
1 1 1 17 1 1 1
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值