【洛谷 4799】 世界冰球錦標賽 Meet in the Middle 折半搜索

題目描述
譯自 CEOI2015 Day2 T1「Ice Hockey World Championship」

今年的世界冰球錦標賽在捷克舉行。Bobek 已經抵達布拉格,他不是任何團隊的粉絲,也沒有時間觀念。他只是單純的想去看幾場比賽。如果他有足夠的錢,他會去看所有的比賽。不幸的是,他的財產十分有限,他決定把所有財產都用來買門票。

給出 Bobek 的預算和每場比賽的票價,試求:如果總票價不超過預算,他有多少種觀賽方案。如果存在以其中一種方案觀看某場比賽而另一種方案不觀看,則認爲這兩種方案不同。

輸入格式
第一行,兩個正整數 NN 和 M(1 \leq N \leq 40,1 \leq M \leq 10^{18})M(1≤N≤40,1≤M≤10
18
),表示比賽的個數和 Bobek 那家徒四壁的財產。

第二行,NN 個以空格分隔的正整數,均不超過 10^{16}10
16
,代表每場比賽門票的價格。

輸出格式
輸出一行,表示方案的個數。由於 NN 十分大,注意:答案 \le 2^{40}≤2
40

輸入輸出樣例
輸入 #1 複製
5 1000
100 1500 500 500 1000
輸出 #1 複製
8
說明/提示
樣例解釋
八種方案分別是:

一場都不看,溜了溜了
價格 100100 的比賽
第一場價格 500500 的比賽
第二場價格 500500 的比賽
價格 100100 的比賽和第一場價格 500500 的比賽
價格 100100 的比賽和第二場價格 500500 的比賽
兩場價格 500500 的比賽
價格 10001000 的比賽

題意:有m元和n場球賽的價格,問有多少種觀看球賽的方案

思路:

折半搜索的經典例題。爆搜,O(2n)的時間複雜度,這裏N上限40,肯定不行。所以考慮折半(220)。
先對數組內前一半元素進行選or不選的操作,統計出所有可能的結果(費用)並排好序。然後後面一半搜索的時候,相當於合併,在後一半每次找完時,二分查找前面的費用裏面有多少是小於等於當前 m - cost 的。最後累加答案即可。我這裏多一步對原數組從大到小排序,減少遞歸樹根節點附近的分支 。

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 3e4+2;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[50];
ll n,m;
ll half_cost[1<<21];        //統計前一半搜索的結果
ll cnt = 0;
ll ans = 0;

bool cmp(ll a, ll b)        //從大到小排序
{
    return a>b;
}

void dfs1(ll l , ll r, ll cost)     //前半搜
{
    if(cost>m) return ;
    if(l>r)
    {
        half_cost[cnt++] = cost;        //記錄結果
        return ;
    }
    dfs1(l+1,r,cost);       //選 or 不選
    dfs1(l+1,r,cost+a[l]);
}

void dfs2(ll l , ll r, ll cost)     //後半搜
{   
    if(cost>m) return ;
    if(l>r)
    {
         ans += upper_bound(half_cost, half_cost+cnt, m - cost) - half_cost;        //統計前面一半里面有多少種搭配的結果比m - cost要小的 
         return ;
    }
    dfs2(l+1,r,cost);       //同上
    dfs2(l+1,r,cost+a[l]);
}

int main()
{
    n = read(); m = read();
    rep(i,1,n) a[i] = read();
    sort(a+1,a+1+n,cmp);        //從大到小排序,剪枝
    ll mid = n>>1;
    dfs1(1,mid,0);
    sort(half_cost,half_cost+cnt);      //維護結果數組的單調性
    dfs2(mid+1,n,0);
    cout<<ans<<'\n';
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章