题目描述
译自 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;
}