【bzoj4800】: [Ceoi2015]Ice Hockey World Championship dfs

【bzoj4800】: [Ceoi2015]Ice Hockey World Championship

N<=40所以如果直接dfs背包会TLE

 1 /* http://www.cnblogs.com/karl07/ */
2 #include <cstdlib>
3 #include <cstring>
4 #include <cmath>
5 #include <cstdio>
6 #include <algorithm>
7 using namespace std;
8 #define ll long long
9
10
11 ll n,m,mid,ans;
12 ll c[50],a[2][1100000],ca[2];
13
14 void dfs(int p,int x,ll cost){
15     if (cost>m) return;
16     if ((x>=mid && p==0) || (x>=n && p==1)){
17         a[p][++ca[p]]=cost;
18         return;
19     }
20     dfs(p,x+1,cost+c[x+1]);
21     dfs(p,x+1,cost);
22 }
23
24 int main(){
25     scanf("%lld%lld",&n,&m);
26     for (int i=1;i<=n;i++) scanf("%lld",&c[i]);
27     mid=n/2;
28     dfs(0,0,0);
29     dfs(1,mid,0);
30     sort(a[0]+1,a[0]+ca[0]+1);
31     sort(a[1]+1,a[1]+ca[1]+1);
32     for (int l=1,r=ca[1];l<=ca[0] && r>=1;ans+=r,l++) while (a[0][l]+a[1][r]>m) r--;
33     printf("%lld\n",ans);
34     return 0;
35 }
View Code

posted @ 2017-03-31 12:29  karl07  阅读(199)  评论(0编辑  收藏  举报