[luogu3067 USACO12OPEN] 平衡的奶牛群

Posted menteur-hxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[luogu3067 USACO12OPEN] 平衡的奶牛群相关的知识,希望对你有一定的参考价值。

传送门

Solution

折半搜索模板题
考虑枚举每个点在左集合和右集合或者不在集合中,然后排序合并即可

Code

//By Menteur_Hxy
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Re register
#define Ms(a,b) memset(a,(b),sizeof(a))
#define Fo(i,a,b) for(Re int i=(a),_=(b);i<=_;i++)
#define Ro(i,a,b) for(Re int i=(b),_=(a);i>=_;i--)
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<16,stdin)),p1==p2?EOF:*p1++)
using namespace std;
typedef long long LL;

char buf[1<<16],*p1,*p2;
inline int read() {
    int x=0,f=1; char c=getchar();
    while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
    while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
    return x*f;
}

inline void writ(int x) {
    if(x>9) writ(x/10);
    putchar(x%10+'0');
}

const int N=25,M=1e8+5;
int n,ans,tt1,tt2;
bool vis[1<<N];
int da[N];
struct Data{int val,cur;}T1[1<<N],T2[1<<N];

void dfs(int pos,int ed,int sum,int S) {
    if(pos>ed) {
        if(ed==n/2) T1[++tt1].val=sum,T1[tt1].cur=S;
        else T2[++tt2].val=sum,T2[tt2].cur=S;//1
        return ;
    }
    dfs(pos+1,ed,sum,S);
    dfs(pos+1,ed,sum+da[pos],S|(1<<(pos-1)));
    dfs(pos+1,ed,sum-da[pos],S|(1<<(pos-1)));
}

bool cmp1(Data a,Data b) {return a.val<b.val;}
bool cmp2(Data a,Data b) {return a.val>b.val;}

int main() {
    n=read();
    Fo(i,1,n) da[i]=read();
    dfs(1,n/2,0,0); dfs(n/2+1,n,0,0);
    sort(T1+1,T1+1+tt1,cmp1);
    sort(T2+1,T2+1+tt2,cmp2);
    int l=1,r=1,las;
    while(l<=tt1&&r<=tt2) {
        while(r<=tt2&&T1[l].val+T2[r].val>0) r++;//2
        las=r;
        while(r<=tt2&&T1[l].val+T2[r].val==0) {
            if(!vis[T1[l].cur|T2[r].cur])
                vis[T1[l].cur|T2[r].cur]++,ans++;
            r++;
        }
        l++;
        if(T1[l-1].val==T1[l].val) r=las;//3
    }
    writ(ans-1);
    return 0;
}

以上是关于[luogu3067 USACO12OPEN] 平衡的奶牛群的主要内容,如果未能解决你的问题,请参考以下文章

luogu P3147 [USACO16OPEN]262144

luogu3146 [USACO16OPEN]248

luogu P2949 [USACO09OPEN]工作调度Work Scheduling

Luogu [USACO08OPEN]寻宝之路Clear And Present Danger

luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题解

luogu P3146 [USACO16OPEN]248