Codeforces Round #683 (Div. 2) C. Knapsack(贪心)

秦学林
2023-12-01

题目链接:https://codeforc.es/contest/1447/problem/C

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

题意

有 n 个物品,第 i 个物品重量为 wi ,背包大小为 W ,问能否装到 W / 2 ~ W 的范围(W / 2 向上取整)。

分析

先找出不能的情况:

  1. 物品中最小值大于 W
  2. 物品重量的和小于 W / 2

若在输入时我们发现有物品的重量在 W / 2 ~ W 的区间内,后面直接输出这个答案就可以

上面这些情况都不符合的情况下,我们给所有物品按重量排序,从最轻的开始放入背包,直到背包中的物品重量大于 W / 2 ,这个时候会有两种情况:

  1. 背包中物品重量大于 W ,则输出 -1
  2. 背包中物品重量小于等于 W ,即答案

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int t;
ll n,W;
ll w[200007];
struct node{
	ll id;
	ll w;
}ww[200007];

bool cmp(node x,node y)
{
	return x.w < y.w;
}

bool cmp1(node x,node y)
{
	return x.id < y.id;
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		ll sum = 0;
		scanf("%lld%lld",&n,&W);
		ll half = W / 2;
		if(W % 2 != 0) half++;
		ll flag = 0;
		ll minn = 1000000007;
		ll psb = 0;
		for(int i=1;i<=n;i++)
		{
			scanf("%lld",&w[i]);
			minn = min(minn, w[i]);
			if(w[i] >= half && w[i] <= W) psb = i;
			sum += w[i];
		}
		if(minn > W)
		{
			printf("-1\n");
			continue;
		}
		if(psb > 0)
		{
			printf("1\n%lld\n",psb);
			continue;
		}
		if(sum >= half && sum <= W)
		{
			printf("%lld\n",n);
			for(int i=1;i<=n;i++) printf("%d ",i);
			printf("\n");
		}
		else if(sum < half) printf("-1\n");
		else
		{
			for(int i=1;i<=n;i++)
			{
				ww[i].id = i;
				ww[i].w = w[i];
			}
			sort(ww + 1, ww + 1 + n, cmp);
			ll cnt = 0;
			ll tmp = 0;
			while(tmp < half)
				tmp += ww[++cnt].w;
			if(tmp > W)
			{
				printf("-1\n");
			}
			else
			{
				sort(ww + 1, ww + 1 + cnt, cmp1);
				printf("%lld\n",cnt);
				for(int i=1;i<=cnt;i++)
					printf("%lld ",ww[i].id);
				printf("\n");
			}
		}
	}
	return 0;
}
 类似资料: