洛谷 P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S…

P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S…

题目描述

Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk each day (1 <= M(i) <= 100,000,000). FJ wants to streamline the process of milking his cows every day, so he installs a brand new milking machine in his barn. Unfortunately, the machine turns out to be far too sensitive: it only works properly if the cows on the left side of the barn have the exact same total milk output as the cows on the right side of the barn!

Let us call a subset of cows "balanced" if it can be partitioned into two groups having equal milk output. Since only a balanced subset of cows can make the milking machine work, FJ wonders how many subsets of his N cows are balanced. Please help him compute this quantity.

给n个数,从中任意选出一些数,使这些数能分成和相等的两组。

求有多少种选数的方案。

输入输出格式

输入格式:

 

* Line 1: The integer N.

* Lines 2..1+N: Line i+1 contains M(i).

 

输出格式:

 

* Line 1: The number of balanced subsets of cows.

 

输入输出样例

输入样例#1: 复制
4 
1 
2 
3 
4 
输出样例#1: 复制
3 

说明

There are 4 cows, with milk outputs 1, 2, 3, and 4.

There are three balanced subsets: the subset {1,2,3}, which can be partitioned into {1,2} and {3}, the subset {1,3,4}, which can be partitioned into {1,3} and {4}, and the subset {1,2,3,4} which can be partitioned into {1,4} and {2,3}.

思路:

第一遍gg,敲完交上以后发现读错题了。。。然后就急匆匆的写了个暴力。

#include
#include
#include
#include
using namespace std;
int num[25];
int n,ans;
void dfs(int now,int lsum,int rsum,int tot){
    if(lsum==rsum&&tot!=0){ ans++;return ; }
    if(now==n+1)    return ;
    dfs(now+1,lsum+num[now],rsum,tot+1);
    dfs(now+1,lsum,rsum+num[now],tot+1);
    dfs(now+1,lsum,rsum,tot);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)    scanf("%d",&num[i]);
    sort(num+1,num+1+n);
    dfs(1,0,0,0);
    cout<2;
}
19分暴力,不兹道为什么wa了3个点

 思路:meet int the middle,不知道为什么,总有一个点卡不过去,只有开开O2优化才能过。

#include
#include
#include
#include
#define MAXN 21
using namespace std;
int n,nn,ans,numl,numr;
int num[21],vis[1<<MAXN];
struct nond{ int sum,stat; }lft[1<1<<MAXN];
int cmp1(nond a,nond b){ return a.sum<b.sum; }
int cmp2(nond a,nond b){ return a.sum>b.sum; }
void read(int &x){
    x=0;int f=1;char c=getchar();
    while(c<'0'&&c>'9'){ if(c=='-') f=-1;c=getchar(); } 
    while(c>='0'&&c<='9'){ x=x*10+c-'0';c=getchar();  }
    x*=f;
}
void dfs(int now,int tot,int val,int nowstact){
    if(now>tot){
        if(tot==nn){ lft[++numl].sum=val;lft[numl].stat=nowstact; }
        else{ rght[++numr].sum=val;rght[numr].stat=nowstact; }
        return ;
    }
    dfs(now+1,tot,val,nowstact);
    dfs(now+1,tot,val-num[now],nowstact+(1<<(now-1)));
    dfs(now+1,tot,val+num[now],nowstact+(1<<(now-1)));
}
int main(){
    scanf("%d",&n);nn=n/2;
    for(int i=1;i<=n;i++)    scanf("%d",&num[i]);
    dfs(1,nn,0,0);sort(lft+1,lft+1+numl,cmp1);
    dfs(nn+1,n,0,0);sort(rght+1,rght+1+numr,cmp2);
    int l=1,r=1;
    while(l<=numl&&r<=numr){
        while(lft[l].sum+rght[r].sum>0&&r<=numr)    r++;
        int pre=r;
        while(lft[l].sum+rght[r].sum==0&&r<=numr){
            if(!vis[lft[l].stat|rght[r].stat]){ vis[lft[l].stat|rght[r].stat]=1;ans++; }
            r++;
        }
        if(lft[l].sum==lft[l+1].sum)    r=pre; l++;
    }
    printf("%d",ans-1);
}

 

转载于:https://www.cnblogs.com/cangT-Tlan/p/8747803.html

你可能感兴趣的:(洛谷 P3067 [USACO12OPEN]平衡的奶牛群Balanced Cow S…)