BZOJ 4800: [Ceoi2015]Ice Hockey World Championship meet_in_the_middle
来源:互联网 发布:whatsapp mac 编辑:程序博客网 时间:2024/05/17 06:01
4800: [Ceoi2015]Ice Hockey World Championship
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 439 Solved: 237
[Submit][Status][Discuss]
Description
有n个物品,m块钱,给定每个物品的价格,求买物品的方案数。
Input
第一行两个数n,m代表物品数量及钱数
第二行n个数,代表每个物品的价格
n<=40,m<=10^18
Output
一行一个数表示购买的方案数
(想怎么买就怎么买,当然不买也算一种)
Sample Input
5 1000
100 1500 500 500 1000
100 1500 500 500 1000
Sample Output
8
吧n的数据范围看成m的了。。。心中跑过无数草泥马
在看数据范围前想背包
看完之后又不小心GG
偷偷翻题解 meet in the middle
复杂度
#include<cmath>#include<ctime>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<bitset>#include<queue>#include<set>#include<map>using namespace std;typedef long long ll;inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}return x*f;}void print(ll x){if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}const int N=45,M=1200000;int n,midst,cnt1,cnt2;ll m,a[N],res1[M],res2[M];void dfs(int step,int aim,ll sum){if(step>aim){aim^midst?res2[++cnt2]=sum:res1[++cnt1]=sum;return ;}dfs(step+1,aim,sum);dfs(step+1,aim,sum+a[step]);}inline bool cmp(ll x,ll y){return x>y;}void solve(){ll ans=0;sort(res1+1,res1+cnt1+1);sort(res2+1,res2+cnt2+1,cmp);register int i=1,j=1;while(i<=cnt1){while(res1[i]+res2[j]>m)j++;if(j>cnt2)break;ans+=cnt2-j+1;i++;}print(ans);puts("");}int main(){n=read();m=read();for(int i=1;i<=n;++i)a[i]=read();midst=(n>>1);dfs(1,midst,0);dfs(midst+1,n,0);solve();return 0;}/*5 1000100 1500 500 500 10008*/
阅读全文
0 0
- BZOJ 4800: [Ceoi2015]Ice Hockey World Championship meet_in_the_middle
- BZOJ 4800: [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 ……
- [BZOJ]4800: [Ceoi2015]Ice Hockey World Championship dfs+二分
- 4800: [Ceoi2015]Ice Hockey World Championship
- BZOJ 4800([Ceoi2015]Ice Hockey World Championship-meet in the middle)
- bzoj 4800: [Ceoi2015]Ice Hockey World Championship meet in the middle
- bzoj4800: [Ceoi2015]Ice Hockey World Championship
- CEOI2015 Day1Task2 Calvinball championship
- [CEOI2015]Calvinball championship
- BZOJ 2679: [Usaco2012 Open]Balanced Cow Subsets meet_in_the_middle / 折半搜索
- 2010 World Rally Championship WRC 赛历
- WELCOME TO `ICE WORLD`
- ICE: Hello, World
- ICE C++ Hello World
- ICE入门之hello world
- 自然语言处理(六)词向量
- 二维线段树专题
- tornado压测
- 微信图灵机器人
- 配置Nginx正向代理
- BZOJ 4800: [Ceoi2015]Ice Hockey World Championship meet_in_the_middle
- 真-ViewPager与ScrollView间的ViewPager消失及滑动冲突关系
- TensorBoard使用
- 每天一个linux命令(48):watch命令
- 未来的模式等着我们开启,hello world!
- 栈和队列之仅用递归函数和栈操作逆序一个栈
- POJ 2279 Mr. Young's Picture Permutations(杨氏矩阵和钩子公式)
- 判断STL list是否为空的小技巧
- Javascript程序设计思维