[luogu4799 CEOI2015 Day2] 世界冰球锦标赛(折半搜索)
2024-08-25 08:00:01
Solution
折半搜索裸题,注意\(long long\)
Code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define Re register
#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--)
using namespace std;
typedef long long LL;
inline LL read() {
LL 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;
}
const int N=45;
LL n,m,k,t1,t2,ans;
LL da[N],s1[1050000],s2[1050000];
void dfs1(int pos,LL s) {
if(s>m) return ;
if(pos==k+1) {s1[++t1]=s;return ;}
dfs1(pos+1,s); dfs1(pos+1,s+da[pos]);
}
void dfs2(int pos,LL s) {
if(s>m) return ;
if(pos==n+1) {s2[++t2]=s;return ;}
dfs2(pos+1,s); dfs2(pos+1,s+da[pos]);
}
int main() {
n=read(),m=read();k=n/2;
Fo(i,1,n) da[i]=read();
dfs1(1,0);dfs2(k+1,0);
sort(s1+1,s1+1+t1); sort(s2+1,s2+1+t2);
LL p1=1,p2=t2;
while(p1<=t1&&p2) {
while(p2&&s1[p1]+s2[p2]>m) p2--;
ans+=p2; p1++;
}
printf("%lld\n",ans);
return 0;
}
最新文章
- hive 基本语法
- 【Java每日一题】20161125
- 黑马程序员——【Java基础】——正则表达式
- WindowsPhone-GameBoy模拟器开发五--使用XNA初略实现Gameboy显示系统
- Struts2 Annotation 注解配置
- VC下载文件显示进度条
- Ubuntu 系统 文件操作命令
- JPA 系列教程1-环境搭建
- 最新最全的html5标签集合
- (七)大图展示Demo引出的UIScrollView的使用
- [原创] debian 9.3 搭建seafile企业私有网盘
- python 在.py文件中调用其他.py内的函数
- MySQL 大致测试更新时间
- 重要, 要播放音乐视频等多媒体: 安装fedora23中的多媒体编码器
- docker图形化管理工具portainer
- 科学-天文学-天文观测站:TMT(红外天文望远镜)
- 整理一些常用的前端CND加速库,VUE,Jquery,axios
- ubuntu安装mongo数据库
- [UE4]让AI跑起来
- Vibrator震动
热门文章
- Java判断是否为移动端
- select readonly 不能看到其它选项解决方式
- Hypercall
- IOS 京东相关app 出现“网络请求失败,请检查您的网络设置”的解决办法
- Google Deepmind AI tries it hand at creating Hearthstone and Magic: The Gathering cards
- iOS网络开发工具集----字符串操作和时间操作
- Appium + python - automator定位升级版操作
- Springboot统一跨域配置
- [NOI2015,LuoguP2146]软件包管理器------树剖
- 三星的Knox Warranty Bit原理