• A. Knapsack


    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.

    贪个心就行

    1. #include
    2. using namespace std;
    3. typedef long long int ll;
    4. typedef struct
    5. {
    6. int id;
    7. ll val;
    8. }xinxi;
    9. xinxi a[200000+10];
    10. vector<int>v;
    11. bool cmp(xinxi a,xinxi b)
    12. {
    13. return a.val
    14. }
    15. int main()
    16. {
    17. //有在范围内的直接选
    18. //没有的话说明剩下的都是小于等于的,那么从大到小选择
    19. int t;
    20. cin>>t;
    21. while(t--)
    22. {
    23. int n;
    24. cin>>n;
    25. ll w;
    26. cin>>w;
    27. ll ans=0;
    28. ll left=w/2;
    29. if(w%2)
    30. left++;
    31. for(int i=1;i<=n;i++)
    32. {
    33. cin>>a[i].val;
    34. a[i].id=i;
    35. if(a[i].val>=left&&a[i].val<=w)
    36. {
    37. ans=i;
    38. }
    39. }
    40. if(ans)
    41. {
    42. cout<<1<
    43. continue;
    44. }
    45. sort(a+1,a+1+n,cmp);
    46. ll nowsum=0;
    47. int flag=0;
    48. for(int i=n;i>=1;i--)
    49. {
    50. ll temp=nowsum+a[i].val;
    51. if(temp>=left&&temp<=w)
    52. {
    53. nowsum=temp;
    54. flag=1;
    55. v.push_back(a[i].id);
    56. break;
    57. }
    58. else if(temp
    59. {
    60. nowsum=temp;
    61. v.push_back(a[i].id);
    62. }
    63. }
    64. if(flag)
    65. {
    66. cout<size()<
    67. for(auto it:v)
    68. {
    69. cout<" ";
    70. }
    71. cout<
    72. }
    73. else
    74. {
    75. cout<<-1<
    76. }
    77. v.clear();
    78. }
    79. return 0;
    80. }

  • 相关阅读:
    《C和指针》读书笔记(第十四章 预处理器)
    【三维目标检测】VoteNet(二)
    Darty自养号测评下单支付方式和注册账号手法有哪些要求?
    Python 用户输入和字符串格式化指南
    ABAP json解析使用引用代替预定义数据结构
    SpringBoot开发实用篇(3)—整合第三方技术
    terraform简单的开始-vpc cvm创建
    IB中文A看人文教育
    矿大数据结构实验四 折半查找 二叉搜索树 最短路径 排序
    【线性代数 & C++】结合逆矩阵的克拉默法则
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126199771