zoukankan      html  css  js  c++  java
  • BZOJ2679: [Usaco2012 Open]Balanced Cow Subsets

    2679: [Usaco2012 Open]Balanced Cow Subsets

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 256  Solved: 103
    [Submit][Status][Discuss]

    Description

    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(1≤N≤20)个数M(i) (1 <= M(i) <= 100,000,000),在其中选若干个数,如果这几个数可以分成两个和相等的集合,那么方案数加1。问总方案数。

    Input

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

    Output

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

    Sample Input

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

    Sample Output

    3
    OUTPUT DETAILS: 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}.

    HINT

     

    Source

    题解:论文(搜索问题中的meet in the middle技巧 )题
       此篇论文的核心思想就是:将一些可以缩小规模再合并出结果的搜索进行优化;
       例如:x1+x2+x3+x4+x5+x6==0(x1--6<=D) 求解的方案数
          我们可以暴力dfs,但是时间复杂度过不了,于是我们考虑变化一下:x1+x2+x3==x4+x5+x6
          于是我们就可以缩小规模,只需暴力出前3个,将其放入hash中,再暴力处后三个,在hash中询问即可,
          时间复杂度由6^D变为6^(D/2)级别,获得了极大的优化!
       对于此题我们在dfs过程中同时记录下,每个数字的使用情况即可!
    代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 #define N     2000005
     7 #define mod 1000007
     8 using namespace std;
     9 int now[mod],Pre[N],A[N],B[N],tot;
    10 int n,a[25],bin[25],ans;
    11 bool vis[N];
    12 int read()
    13 {
    14     int x=0,f=1; char ch;
    15     while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') f=-1;
    16     while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9');
    17     return x*f;
    18 }
    19 void insert(int P,int PP){
    20     int y=abs(P)%mod;
    21     for (int p=now[y]; p; p=Pre[p])
    22     {
    23         if (A[p]==P && B[p]==PP) return;
    24     }
    25     ++tot; Pre[tot]=now[y]; now[y]=tot; A[tot]=P; B[tot]=PP;
    26 }
    27 void query(int P,int PP){
    28     int y=abs(P)%mod;
    29     for (int p=now[y]; p; p=Pre[p])
    30     {
    31         if (A[p]==P) vis[PP+B[p]]=1;
    32     }
    33 }
    34 void dfs1(int x,int P,int PP)
    35 {
    36     if (x==(n/2+1)){insert(P,PP); return ;
    37     }
    38     dfs1(x+1,P+a[x-1],PP+bin[x-1]);
    39     dfs1(x+1,P-a[x-1],PP+bin[x-1]);
    40     dfs1(x+1,P,PP);
    41 }
    42 void dfs2(int x,int P,int PP)
    43 {
    44     if (x==(n+1)){query(P,PP); return ;
    45     }
    46     dfs2(x+1,P+a[x-1],PP+bin[x-1]);
    47     dfs2(x+1,P-a[x-1],PP+bin[x-1]);
    48     dfs2(x+1,P,PP);
    49 }
    50 int main()
    51 {
    52     bin[0]=1; for (int i=1;i<21;i++) bin[i]=bin[i-1]<<1;
    53     n=read(); for (int i=0; i<n; i++) a[i]=read();
    54     dfs1(1,0,0); dfs2(n/2+1,0,0); 
    55     for (int i=1; i<(1<<n); i++) if (vis[i]) ans++;
    56     printf("%d
    ",ans);
    57     return 0;
    58 }
    View Code
        
  • 相关阅读:
    WPF 获取本机所有字体拿到每个字符的宽度和高度
    WPF 自己封装 Skia 差量绘制控件
    WPF 漂亮的现代化控件 新 ModernWPF 界面库
    dotnet 在 UOS 国产系统上使用 MonoDevelop 创建 GTK 全平台带界面应用
    dotnet 在 UOS 国产系统上使用 MonoDevelop 进行拖控件开发 GTK 应用
    dotnet 在 UOS 国产系统上安装 MonoDevelop 开发工具
    通过java采集PC麦克风音频及播放wav音频文件
    nginx-http之ssl(九)
    nginx-http之proxy(八)
    nginx-http之upstream(七)
  • 原文地址:https://www.cnblogs.com/HQHQ/p/5991954.html
Copyright © 2011-2022 走看看