大奖赛

来源:互联网 发布:js input不可编辑属性 编辑:程序博客网 时间:2024/04/30 09:57

30暴力搜
60 01背包
100 折半搜索

以上三个做法都很裸

顺便Lancelot+Morgan
长江骑士和王姐
怕不是某呆毛王要提着咖喱棒来打架

60分背包
忘了+0
结果一直少1

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>#include<climits>#include<queue>#define LL long longusing namespace std;int n,k,i,j,pr[45];LL dp[10005];int main(){    freopen("champion.in","r",stdin);    freopen("champion.out","w",stdout);     scanf("%d%d",&n,&k);    for(i=1;i<=n;i++)        scanf("%d",&pr[i]);    dp[0]=1;        for(i=1;i<=n;i++)        for(j=k;j>=pr[i];j--)            dp[j]+=dp[j-pr[i]];    for(i=0;i<k;i++) dp[k]+=dp[i];      printf("%lld",dp[k]);    return 0;}

100分正解

#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<climits>#include<cstdlib>#include<ctime>#include<cstring>#include<queue>#define LL long longusing namespace std;LL tot1,sum1[1100000],n1,n2,n,k,ans,a[45];void dfs1(LL x,LL tot){    if(x==n1+1)    {        tot1++;        sum1[tot1]=tot;        return;    }    if(tot+a[x]<=k) dfs1(x+1,tot+a[x]);    dfs1(x+1,tot);}void dfs2(LL x,LL tot){    if(x==n+1)    {        LL p,pos;        p=k-tot;        pos=upper_bound(sum1+1,sum1+tot1+1,p)-sum1;        ans+=pos-1;        return;    }    if(tot+a[x]<=k) dfs2(x+1,tot+a[x]);    dfs2(x+1,tot);}void predfs1(){    n1=n/2;    dfs1(1,0);    sort(sum1+1,sum1+tot1+1);}void predfs2(){    n2=n-n1;    dfs2(n1+1,0);}int main(){    freopen("champion.in","r",stdin);    freopen("champion.out","w",stdout);     LL i;    scanf("%lld%lld",&n,&k);    for(i=1;i<=n;i++)        scanf("%lld",&a[i]);    predfs1();    predfs2();    printf("%lld",ans);    return 0;}
原创粉丝点击