标签:sed ++ block color header std isp string main
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 }
标签:sed ++ block color header std isp string main
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7423948.html