50分求助

P4799 [CEOI2015 Day2] 世界冰球锦标赛

caoshaoxuan @ 2023-05-08 13:17:10

#include<bits/stdc++.h>
using namespace std;
int n;
long long m,sum,l[1048576],r[1048576],a[45];
int ans1=1,ans2=1,t1,t2;
int main()
{
    cin>>n>>m;
    int n1=n/2,n2=n-n/2;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    for(int i=1;i<=n1;i++)
    {
        ans1=ans1*2;
    }
    for(int i=1;i<=n2;i++)
    {
        ans2=ans2*2;
    }
    for(int i=0;i<ans1;i++)
    {
        int sum1=0,k=i,w=1;
        while(k>0)
        {
            if(k%2==1)
            {
                sum1=sum1+a[w];
            }
            w++;
            k/=2;
        }
        if(sum1<=m)l[++t1]=sum1;
    }
    for(int i=0;i<ans2;i++)
    {
        int sum2=0,k=i,w=n/2+1;
        while(k>0)
        {
            if(k%2==1)sum2=sum2+a[w];
            w++;
            k/=2;
        }
        if(sum2<=m)r[++t2]=sum2;
    }
    sort(l+1,l+t1+1);
    sort(r+1,r+t2+1);
    for(int i=1;i<=t1;i++)
    {
        int ll=1,rr=t2;
        while(ll+1<rr)
        {
            int mid=(ll+rr)/2;
            if(r[mid]>m-l[i])rr=mid;
            else ll=mid;
        }
        while(r[ll+1]<=m-l[i]&&ll<=t2)ll++;
        while((r[ll]>m-l[i]||ll>t2)&&ll>0)ll--;
        sum=sum+ll;
    }
    cout<<sum;
    return 0;
}

by caoshaoxuan @ 2023-05-08 17:39:54

找到原因了,sum1和sum2要定义成long long型,希望后人不要犯这样低级的错误


|