4800: [Ceoi2015]Ice Hockey World Championship
2017-04-10 18:42
302 查看
4800: [Ceoi2015]Ice Hockey World Championship
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 257 Solved: 135
[Submit][Status][Discuss]
Description
有n个物品,m块钱,给定每个物品的价格,求买物品的方案数。Input
第一行两个数n,m代表物品数量及钱数第二行n个数,代表每个物品的价格
n<=40,m<=10^18
Output
一行一个数表示购买的方案数(想怎么买就怎么买,当然不买也算一种)
Sample Input
5 1000100 1500 500 500 1000
Sample Output
8HINT
Source
[Submit][Status][Discuss]
采取meet-in-middle的思想,每20个数字分一组,搜出所有组合,然后统计前缀和就行了#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = (1 << 21);
typedef long long LL;
int n,tp;
LL m,Ans,A[55],B[55],a[2][maxn];
void Dfs(int x,LL sum,int now)
{
if (x == tp + 1)
{
a[now][++a[now][0]] = sum;
return;
}
Dfs(x + 1,sum,now);
if (B[x] <= m - sum) Dfs(x + 1,sum + B[x],now);
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m;
for (int i = 1; i <= n; i++) scanf("%lld",&A[i]);
if (n == 1)
{
cout << (A[1] <= m ? 2 : 1) << endl;
return 0;
}
int N = n / 2; for (int i = 1; i <= N; i++) B[++tp] = A[i]; Dfs(1,0,0);
tp = 0; for (int i = N + 1; i <= n; i++) B[++tp] = A[i]; Dfs(1,0,1);
sort(a[0] + 1,a[0] + a[0][0] + 1);
sort(a[1] + 1,a[1] + a[1][0] + 1); int pos = a[1][0];
for (int i = 1; i <= a[0][0]; i++)
{
while (pos && a[1][pos] > m - a[0][i]) --pos;
Ans += 1LL * pos;
}
cout << Ans << endl;
return 0;
}
相关文章推荐
- bzoj [4800] [Ceoi2015]Ice Hockey World Championship
- 【bzoj4800】[Ceoi2015]Ice Hockey World Championship 折半搜索
- [BZOJ 4800][Ceoi2015]Ice Hockey World Championship:双向搜索
- [bzoj4800][Ceoi2015]Ice Hockey World Championship
- 【BZOJ】4800 [Ceoi2015]Ice Hockey World Championship 折半查找
- BZOJ_4800_[Ceoi2015]Ice Hockey World Championship_双指针
- BZOJ 4800([Ceoi2015]Ice Hockey World Championship-meet in the middle)
- 【BZOJ4800】[Ceoi2015]Ice Hockey World Championship Meet in the Middle
- BZOJ4800 [Ceoi2015]Ice Hockey World Championship
- bzoj 4800: [Ceoi2015]Ice Hockey World Championship meet in the middle
- 4800: [Ceoi2015]Ice Hockey World Championship
- BZOJ 4800: [Ceoi2015]Ice Hockey World Championship meet_in_the_middle
- [BZOJ4800][Ceoi2015][Meet In Middle]Ice Hockey World Championship
- BZOJ 4800 [Ceoi2015]Ice Hockey World Championship ……
- bzoj4800 [Ceoi2015]Ice Hockey World Championship
- BZOJ4800 [Ceoi2015]Ice Hockey World Championship
- BZOJ 4800: [Ceoi2015]Ice Hockey World Championship
- [BZOJ]4800: [Ceoi2015]Ice Hockey World Championship dfs+二分
- BZOJ 4800 Ice Hockey World Championship
- CEOI2015 Day1Task2 Calvinball championship