zoukankan      html  css  js  c++  java
  • TYVJ1340 送礼物

    P1340 送礼物
    时间: 1000ms / 空间: 131072KiB / Java类名: Main

    描述

    作为惩罚,GY被遣送去帮助某神牛给女生送礼物(GY:貌似是个好差事)但是在GY看到礼物之后,他就不这么认为了。某神牛有N个礼物,且异常沉重,但是 GY的力气也异常的大(-_-b),他一次可以搬动重量和在w(w<=2^31-1)以下的任意多个物品。GY希望一次搬掉尽量重的一些物品,请你 告诉他在他的力气范围内一次性能搬动的最大重量是多少。

    输入格式

    第一行两个整数,分别代表W和N。
    以后N行,每行一个正整数表示G[i],G[i]<= 2^31-1。

    输出格式

    仅一个整数,表示GY在他的力气范围内一次性能搬动的最大重量。

    测试样例1

    输入

    20 5
    7
    5
    4
    18
    1

    输出

    19

    备注

    对于20%的数据 N<=26
    对于40%的数据 W<=2^26
    对于100%的数据 N<=45 W<=2^31-1

     不想写题解。被各种卡。卡longlong,卡空间。。。

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <algorithm>
     6 #define min(a, b) ((a) < (b) ? (a) : (b))
     7 #define max(a, b) ((a) > (b) ? (a) : (b)) 
     8 
     9 inline void read(long long &x)
    10 {
    11     x = 0;char ch = getchar(), c = ch;
    12     while(ch < '0'|| ch > '9')c = ch, ch = getchar();
    13     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
    14     if(c == '-')x = -x; 
    15 }
    16 
    17 const int MAXN = 45 + 10;
    18 
    19 long long W,n,g[MAXN],num1[(1 << 23) + 100],num2[(1 << 23) + 100],cnt1,cnt2,ans,mid;
    20 
    21 void dfs1(long long step, long long now)
    22 {
    23     if(now > W)return;
    24     if(step > mid)
    25     {
    26         if(now)num1[++cnt1] = now;
    27         return;
    28     }
    29     dfs1(step + 1, now + g[step]);
    30     dfs1(step + 1, now);
    31 }
    32 
    33 void dfs2(long long step, long long now)
    34 {
    35     if(now > W)return;
    36     if(step > n)
    37     {
    38         if(now)num2[++cnt2] = now;
    39         return;
    40     }
    41     dfs2(step + 1, now + g[step]);
    42     dfs2(step + 1, now);
    43 }
    44 
    45 int main()
    46 {
    47     read(W), read(n);
    48     mid = n/2;
    49     for(register long long i = 1;i <= n;++ i)
    50         read(g[i]);
    51     dfs1(1, 0);
    52     std::sort(num1 + 1, num1 + 1 + cnt1);
    53     dfs2(mid + 1, 0);
    54     std::sort(num2 + 1, num2 + 1 + cnt2);
    55     ans = max(num1[cnt1], num2[cnt2]);
    56     long long l = 1;
    57     for(register long long i = cnt2;i >= 1;-- i)
    58     {
    59         while(num2[i] + num1[l] <= W && l <= cnt1)++ l;
    60         -- l;
    61         ans = max(ans, num2[i] + num1[l]);
    62     }
    63     printf("%lld", ans);
    64     return 0;
    65 } 
    TYVJ1340
  • 相关阅读:
    团队冲刺第一天
    leetcode整理
    eclipse 中JSP环境搭建
    java期末小结
    桌面宠物online------------------面对对象程序综合设计2020年
    java
    4.3 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20分)
    选择
    算法---分支限定0/1背包--蚁群算法
    博客园特效
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7423948.html
Copyright © 2011-2022 走看看