Preparing Olympiad---cf550B(DFS或者状态压缩模板)
比赛链接:http://codeforces.com/problemset/problem/550/B
给你n个数,选出来只是2个然后求他们的和在L和R的区间内,并且选出来的数中最大值和最小值的差不得小于x,求共有多少种选法
下面是dfs搜出来的;
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define met(a, b) memset(a, b, sizeof(a))
using namespace std;
#define N 110
#define INF 0x3f3f3f3f int n, l, r, x, ans, a[N];
///sum是当前的和,cnt是选了几个数了, s是起始位置,e是终止位置;
void dfs(int sum, int cnt, int s, int e)
{
if(sum > r)return ; if(sum<=r && sum>=l && a[e]-a[s]>=x && cnt>= )ans++; for(int i = e+; i<=n; i++) dfs(sum + a[i], cnt+, s, i);
} int main()
{
while(scanf("%d %d %d %d", &n, &l, &r, &x)!=EOF)
{
ans = ;
for(int i=; i<=n; i++)
scanf("%d", &a[i]); sort(a+, a+n+); for(int i=; i<=n; i++)
dfs(a[i], , i, i); printf("%d\n", ans);
}
return ;
}
暴力枚举所有情况,判断是否符合条件即可; 第一次写状态压缩的题
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define met(a, b) memset(a, b, sizeof(a))
using namespace std;
#define N 15
#define INF 0x3f3f3f3f int main()
{
int n, L, R, x, a[N]; while(scanf("%d %d %d %d", &n, &L, &R, &x) !=EOF )
{
met(a, ); for(int i=; i<n; i++)
scanf("%d", &a[i]); int cnt = (<<n)-, ans = ; for(int i=; i <= cnt; i++)
{
int Min = INF, Max = -INF, num, sum = ; for(int j=; j<n; j++)
{
if( i & (<<j) )
{
sum += a[j];
Min = min(Min, a[j]);
Max = max(Max, a[j]);
num ++;
}
}
if(sum >= L && sum <= R && Max - Min >= x && num >= )
ans++;
}
printf("%d\n", ans); }
return ;
}
Preparing Olympiad---cf550B(DFS或者状态压缩模板)的更多相关文章
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- 靶形数独 (dfs+预处理+状态压缩)
#2591. 「NOIP2009」靶形数独 [题目描述] 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们 ...
- Codeforces Round #306 (Div. 2), problem: (B) Preparing Olympiad【dfs或01枚举】
题意: 给出n个数字,要求在这n个数中选出至少两个数字,使得它们的和在l,r之间,并且最大的与最小的差值要不小于x.n<=15 Problem - 550B - Codeforces 二进制 利 ...
- 最大联通子数组之和(dfs,记忆化搜索,状态压缩)
最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...
- Codeforces Round #306 (Div. 2) B. Preparing Olympiad dfs
B. Preparing Olympiad Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550 ...
- CF Preparing Olympiad (DFS)
Preparing Olympiad time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
随机推荐
- js to json字符串
var last=obj.toJSONString(); //将JSON对象转化为JSON字符 或者 var last=JSON.stringify(obj); //将JSON对象转化为JSON字符
- 高德地图API INVALID_USER_SCODE问题以及keystore问题
今天这篇文章会给大家介绍三个问题: 1,接入API时出现invalid_user_scode问题 首先进行第一个大问题,接入高德地图API时出现invalid_user_scode问题 因为项目需要接 ...
- vue 单元测试
单元测试 配置和工具 任何兼容基于模块的构建系统都可以正常使用,但如果你需要一个具体的建议,可以使用Karma进行自动化测试.它有很多社区版的插件,包括对webpack和browserify的支持. ...
- sama5d36 can0 can1 测试
1 删除/bin/ip 保留/sbin/ip 2 ip link set can0 type can bitrate 125000 ip link set can1 type can bitrate ...
- 文件模式设置用户ID/设置组ID/sticky bit_转
S_ISUID (04000) set-user-ID (set process effective user ID on execve(2))S_ISGID (02000) set-grou ...
- 小贝_redis 高级应用-事务
redis高级应用-事务 一.redis的事务 二.redis实现事务 三.redis事务问题 一.redis的事务 事务提供了一种"将多个命令打包,然后一次性.按顺序地运行"的机 ...
- msyql的内存计算
本文将讨论MySQL内存相关的一些选项,包括: 单位都是b,不是kb,即1B=1/(1024*1024*1024)G 1)全局的buffer,如innodb_buffer_pool_size: 2)线 ...
- Cocos2d-x-Lua (2.x)脚本开发之 Lua语言基础
从今天開始,往后将陆续更新Lua教程,主要是搭载Cocos2dx ,有不论什么疑惑或者不正确的地方.尽情指正.交流.探讨. 那么首先肯定是Lua语言基础的知识点.以下直接附上代码,凝视已经非常清楚.无 ...
- 基于WebSocket实现网页版聊天室
WebSocket ,HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议,其使用简单,应用场景也广泛,不同开发语言都用种类繁多的实现,仅Java体系中,Tomcat,Jetty,Sp ...
- Linux环境下Apache配置多个虚拟主机挂载多站点同时运行
博客地址: http://blog.csdn.net/ClydeKuo/article/details/69569474 这篇博客讲的很详细,很详细.