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& ...
随机推荐
- Handler实例
Handler使用例1这个例子是最简单的介绍handler使用的,是将handler绑定到它所建立的线程中.本次实验完成的功能是:单击Start按钮,程序会开始启动线程,并且线程程序完成后延时1s会继 ...
- 存储管理器实验——SDRAM
序言:2440有nand和nor两种启动方式,在裸机部分,都是使用的nand启动. 现在,我们想在nand flash启动的时候,通过SRAM访问存储在外设SDRAM中的程序. nand启动,先把前4 ...
- 基于jquery和svg超炫的网页动画
今天给大家分享一款基于jquery和svg超炫的网页动画.这款动画效果非常炫.下面还有重播.慢速.和反向动画按钮.效果非常漂亮.一起看下效果图: 在线预览 源码下载 实现的代码. html代码: ...
- java strtus2 拦截器(Interceptors)
在strtus2 中有一个比较重要的东西就是拦截器(Interceptors) 拦截器可以做到在已有的业务中插入一块共通的,比如在一个业务中,直接插入一串登录功能,就不用去每个页面一个个去显示是否登录 ...
- Python解析xml文件遇到的编码解析的问题
使用python对xml文件进行解析的时候,假设xml文件的头文件是utf-8格式的编码,那么解析是ok的,但假设是其它格式将会出现例如以下异常: xml.parsers.expat.ExpatErr ...
- [Eth]网络查看命令:route
最近在调试网络,出现问题,两个网口分别接外网内网,结果不同 http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html
- Extjs GridPanel 中放入 Combox显示问题
http://weijun8611-126-com.iteye.com/blog/566201 在项目中使用了extjs的editorgridpanel,但是其中的combobox在选择了相应的选项后 ...
- C++ 类的对象管理模型初讲
//类的对象管理模型初讲 #include<iostream> using namespace std; class PointA{ private: int x;//占据4个字节大小的内 ...
- 第二百五十二节,Bootstrap项目实战-首页
Bootstrap项目实战-首页 html <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
- ThinkPHP项目笔记之模板篇
顾名思义:模板就是网页页面,有的是静态,有的的动态 基本语法: 1. <li><a href="{:U('User/searchlist')}">返回列表& ...