zoj 3631 J - Watashi's BG (双向暴力)

J - Watashi's BG
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status

Description

Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer training camp, students are divided into several groups and each day one of the groups will design some problems to hold a contest. Today students of Group C are required to design the problems, and they spent the whole night to check to test data which made them very tired. Watashi decides to give some money as a reward to group C so that they can buy the lunch for free.

There are N days in the training schedule, and all students have booked their lunch for N days so we know how much money they will spend in each day. Now the leader of group C needs to decide how to use Watashi's money. Since the money is limited, it may not be possible that they can have free lunch every day. So each day the leader can choose to pay for the whole group's lunch by themselves or use Watashi's money. Of course, the leader wants to spend Watashi's money as much as possible, but he is too busy to write a program to calculate the maximum money he can spend from Watashi's reward. Can you help him?

Input

The input contains multiple test cases ( no more than 50 test cases ).
In each test case, first there are two integer, N ( 1 <= N <=30 ) , which is the number of training days, M ( 0 <= M <=10000000 ) , which is the reward money from Watashi.
Then there is a line containing N positive integers with the ith integer indicating the money group C need to pay for the lunch of the ithday. All these integers are no more than 10000000 and integers are seperated by a space.

Output

For each test case, output one line with an integer which is the maximum money group C can spend from Watashi's reward

Sample Input

3 10
8 4 5

Sample Output

9

不认真看数据范围QAQ
以为是01背包。。。写。。TLE了。。。
然后虽然觉得会TLE。。还是写了发暴力(转化成二进制,每一位对应取或者不取),果然TLE了。。。。
一筹莫展。。。
然后不保有什么希望的。。。
想优化一下。。。
把整个的分成两组。。。
然后分别搞。。再组合在一起。。。
竟然A了!!!!!!!!!!!

太开心。
搜了下题解貌似是dfs?
==
  1 /*************************************************************************
  2     > File Name: code/zoj/r3631.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  5     > Created Time: 2015年10月19日 星期一 19时36分20秒
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21                  
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define ms(a,x) memset(a,x,sizeof(a))
 25 using namespace std;
 26 const int dx4[4]={1,0,0,-1};
 27 const int dy4[4]={0,-1,1,0};
 28 typedef long long LL;
 29 typedef double DB;
 30 const int inf = 0x3f3f3f3f;
 31 int n;
 32 int a[3][35];
 33 int m;
 34 const int N=1<<16;
 35 int b[N],c[N];
 36 
 37 int solve ( int x,int k)
 38 {
 39     int res = 0 ;
 40     int posi = 0 ;
 41     while (x)
 42     {
 43     if (x%2==1) res = res + a[k][posi];
 44     posi++;
 45     if (res>m) return -1;
 46     x = x/2;
 47     }
 48     return res;
 49 }
 50 int main()
 51 {
 52   #ifndef  ONLINE_JUDGE 
 53    freopen("in.txt","r",stdin);
 54   #endif
 55 
 56    while (scanf("%d %d",&n,&m)!=EOF)
 57    {
 58        int half = n/2;
 59        for ( int i =  0; i < half ; i++) scanf("%d",&a[0][i]);
 60        for ( int i = half ; i < n ; i++) scanf("%d",&a[1][i-half]);
 61 
 62        int MAXN = 1<<half;
 63        int cnt1 =0,cnt2 = 0 ;
 64        ms(b,0);
 65        ms(c,0);
 66 
 67        for ( int i = 0 ; i < MAXN ; i++)
 68        {
 69         int tmp = solve(i,0);
 70         if (tmp!=-1)
 71         {
 72         b[cnt1] = tmp;
 73         cnt1++;
 74         }
 75        }
 76 
 77        int MAXN2 = 1<<(n-half);
 78        for ( int i = 0 ; i <MAXN2  ; i++)
 79     {
 80        int tmp = solve(i,1);
 81        if (tmp!=-1)
 82         {
 83         c[cnt2] = tmp;
 84         cnt2++;
 85         }
 86 
 87     }
 88        sort(b,b+cnt1);
 89        sort(c,c+cnt2);
 90     
 91        int ans = -1;
 92        for ( int i = 0 ; i < cnt1 ; i ++)
 93     {
 94         for ( int j =  0 ; j < cnt2 ; j++)
 95         {
 96 
 97         if (b[i] + c[j] > m) break;
 98         ans = max(ans,b[i]+c[j]);
 99         }
100         if (ans==m) break;
101     }
102        printf("%d\n",ans);
103    }
104   
105    
106  #ifndef ONLINE_JUDGE  
107   fclose(stdin);
108   #endif
109     return 0;
110 }
View Code

 



posted @ 2015-10-19 21:28  111qqz  阅读(205)  评论(0编辑  收藏  举报