BZOJ 4800 折半暴搜

思路:

把它拆成两半  分别搜一发

两部分分别排好序

用two-pointers扫一遍 就可以了.

(读入也要用long long)

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int n,len,top,f,r;
ll m,s[1500500],s2[1500500],a[55],ans;
void dfs(int x,ll wei){
    if(wei>m)return;
    s[top++]=wei;
    for(int i=x+1;i<=len;i++){
        dfs(i,wei+a[i]);
    }
}
int main(){
    scanf("%d%lld",&n,&m);
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    len=n/2;
    dfs(0,0);
    sort(s+1,s+top),r=top-1;
    for(int i=1;i<top;i++)s2[i]=s[i];
    top=0;len=n;dfs(n/2,0);
    sort(s+1,s+top);
    for(int i=0;i<top;i++){
        while(s[i]+s2[r]>m)r--;
        ans+=r+1;
    }
    printf("%lld\n",ans);
}

 

posted @ 2017-04-01 07:47  SiriusRen  阅读(253)  评论(0编辑  收藏  举报