Codeforces Round #683 (Div. 2, by Meet IT)C. Knapsack 贪心

该博客介绍了Codeforces Round #683中的一道题目,涉及到在限制容量的背包中选择物品以达到总重量至少为背包容量一半的贪心策略。博主解释了如何按物品重量从大到小选择,以满足条件,同时也讨论了当无法满足条件时的情况。
摘要由CSDN通过智能技术生成

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
inputCopy
3
1 3
3
6 2
19 8 19 69 9 4
7 12
1 1 1 17 1 1 1
outputCopy
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.

选择几个数字的位置,使他们的和大于⌈W2⌉,小于W,将数字排序后,按大到小选择,如果大于W继续找,一直找到加起来大于⌈W2⌉的位置为止。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
#include <bitset>
#define int long long
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
const int inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int HASH=131;

struct node
{
    int wei,id;
    friend bool operator<(node a,node b)
    {
        return a.wei>b.wei;
    }
}w[maxn];

int vis[maxn];


signed main()
{
    int t;
    cin>>t;
    while(t--)
    {

        int type,weight;
        cin>>type>>weight;
        for(int i=1;i<=type;i++)
        {
            vis[i]=0;
        }
        for(int i=1;i<=type;i++)
        {
            cin>>w[i].wei;
            w[i].id=i;
        }
        sort(w+1,w+1+type);
        int ans=0;
        int cnt=0;
        for(int i=1;i<=type;i++)
        {
            if(ans+w[i].wei<=weight)
            {
                cnt++;
                ans+=w[i].wei;
                vis[i]=1;
            }
        }
        if(weight%2==1) weight++;
        if(cnt==0||ans<weight/2) cout<<-1<<endl;
        else
        {
            cout<<cnt<<endl;
            for(int i=1;i<=type;i++)
            {
                if(vis[i])
                    cout<<w[i].id<<' ';
            }
            cout<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值