• 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. }

  • 相关阅读:
    OpenCV实现单目相机检测物体尺寸
    JAVA面试技巧之自我介绍
    Java入门-----基本语法
    一个手机ip从这个城市去到另一个城市多久会变
    面试题--找出数列中出现次数为奇数的数
    ant的get任务
    高级数据结构——图
    web前端进阶:<6>一个自我简介的小程序
    K8s复习笔记9--war包部署Jenkins war包k8s部署
    (一)TinyWebServer的环境配置与运行
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126199771