ACdream1726-A Math game+(DFS+二分)+(DFS+前缀和)
官方题解:http://acdream.info/topic?tid=4246
参考:https://www.cnblogs.com/nowandforever/p/4492428.html
题意:在给定的n个数中,能否找到几个数使得这几个和等于H;
思路:注意这道题的条件 0<n<=40, 0<=H<10^9, 0<=a[i]<10^9,其中 H 和 A [i] 给的比较大,dp的空间开不下,而n比较小,只有40,所以可以把所给的数分成20、20两部分,用dfs或者直接状态压缩从0至2^(n/2)循环来搞出每组所有可能的和(dfs+剪枝应该比循环快),然后再在第二部分找(H-第一部分的可能和)就可以了。卡了一下map和set。可以用hash或者二分来找
ac代码(600+ms):
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = ; //比2的20次大一点 int n,h,a[],d[],c[maxn],cnt,flag; int check(int s)
{
int le=,ri=cnt;
while(le<=ri)
{
int mid=(le+ri)>>;
if(c[mid]==s)return ;
if(c[mid]>s)
ri = mid - ;
else le = mid + ;
}
return ;
} void dfs(int sum,int cur,int nn,int on)//利用on使得两次dfs放在了一起
{
if(flag)return;
if(cur==nn+)
{
if(on)
c[++cnt]=sum;
else flag = check(h-sum);
return;
}
for(int i=;i<;i++)
{
int t=sum+d[cur]*i;
if(t > h)return;
dfs(t,cur+,nn,on);
}
}
int main(){
//freopen("in","r",stdin);
while(~scanf("%d%d",&n,&h))
{
for(int i = ; i <= n; i++)
{
scanf("%d",&a[i]);
}
cnt = flag = ;
int n1 = (n>>), n2=n-n1;//(n>>1)没加括号WA了几次
for(int i=; i<=n1; i++)
{
d[i]=a[i];
}
dfs(,,n1,); //第一次先算出前一半的所有可能和;
sort(c+,c+cnt+);
for(int i=n1+;i<=n;i++)
{
d[i-n1]=a[i];
}
dfs(,,n2,); //第二次求后一半的和 ,在用二分在前一半中找有没有对应的值
if(flag)puts("Yes");
else puts("No");
} return ;
}
我一开始注意到n比较小,就直接dfs暴力,但是超时了;
不过,我又看到有人没有用二分,只用了dfs+前缀和,0ms;
自己写的(12+ms)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = ;
int h,a[maxn],sum[maxn],n,ans=;
void dfs(int cur,int s)
{
if(ans)return;
if(s>sum[cur])return;
if(sum[cur]==s||s==){ans=;return;} for(int i=n;i>=;i--)
{
if(ans)return;
if(a[i]<=s)
{
dfs(i-,s-a[i]);
}
}
}
int main(){
// freopen("in","r",stdin);
while(~scanf("%d%d",&n,&h))
{
sum[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+,a++n); //不sort一直超时
for(int i=;i<=n;i++)
{
sum[i]=sum[i-]+a[i];
}
ans=;
dfs(n,h);
if(ans)puts("Yes");
else puts("No");
} return ;
}
ACdream1726-A Math game+(DFS+二分)+(DFS+前缀和)的更多相关文章
- Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- Java实现 LeetCode 655 输出二叉树(DFS+二分)
655. 输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一 ...
- [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】
题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...
- Codeforces Round #299 (Div. 2)A B C 水 dfs 二分
A. Tavas and Nafas time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 51nod1307(暴力树剖/二分&dfs/并查集)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...
- Chapter2二分与前缀和
Chapter 2 二分与前缀和 +++ 二分 套路 如果更新方式写的是R = mid, 则不用做任何处理,如果更新方式写的是L = mid,则需要在计算mid是加上1. 1.数的范围 789 #in ...
- ACdream 1726 A Math game (dfs+二分)
http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...
- 【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1146 第一种做法(时间太感人): 第二种做法(rank5,好开心) ================ ...
随机推荐
- 【iOS】判断苹果的设备是哪种
有时候需要判断苹果的设备是 iPhone 还是 iPad 等其他设备,示例代码如下: if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUs ...
- 记一次使用LR测试UDP和TCP的过程
背景 最近项目要做性能测试,要出要一份性能报告,让我出一个有关Tcp和Udp的功能模块的测试,流程大概是这样,先走TCP协议协商一下会话,协商成功后走Udp收发数据. 有点简单啊,自己写个功能模块测一 ...
- python基础之循环与迭代器
循环 python 循环语句有for循环和while循环. while循环while循环语法 while 判断条件: 语句 #while循环示例 i = 0 while i < 10: i += ...
- sqoop增量导数据
sqoop要实现增量导入参数到hive或者hdfs 1.需要在mysql中创建可以自动更新的字段,当插入数据时和更新数据时改字段自动更新,如图中update_time,当数据插入时会记录更新为插入时间 ...
- PythonDay04
## 第四章 ### 今日内容 - 列表- 元组- range ### 列表 列表相比于字符串,不仅可以储存不同的数据类型,而且可以储存大量数据,是一种可变的数据类型 64位python的限制是 11 ...
- 【Java例题】4.3 3. 使用Gauss消元法求解n元一次方程组的根,
3. 使用Gauss消元法求解n元一次方程组的根,举例,三元一次方程组:0.729x1+0.81x2+0.9x3=0.6867x1+x2+x3=0.83381.331x1+1.21x2+1.1x3=1 ...
- ccf 201809-4 再卖菜
这题一开始不知道剪枝这种操作,只会傻傻地dfs. 然后dfs递归写80分超时,非递归写70分超时(纳尼?我一直以为非递归算法在时间上会更优秀一些,为什么会这样?!!) 剪一下枝就都能过了 #inclu ...
- Web前端开发工程师课程大纲
PHP程序员雷雪松整理出来的一套独一无二的Web前端开发课程.本套Web前端开发课程专门为想励志成为优秀web前端工程师的学习者而总结归纳的,本套Web前端课程舍弃了一些不常用的即将废弃的HTML标签 ...
- Java 调用http接口(基于OkHttp的Http工具类方法示例)
目录 Java 调用http接口(基于OkHttp的Http工具类方法示例) OkHttp3 MAVEN依赖 Http get操作示例 Http Post操作示例 Http 超时控制 工具类示例 Ja ...
- 在centos6系列vps装Tomcat8.0
In the following tutorial you will learn how to install and set-up Apache Tomcat 8 on your CentOS 6 ...