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& ...
随机推荐
- aix 常用命令
官网上的介绍: AIX 常用命令汇总 http://www.ibm.com/developerworks/cn/aix/library/au-dutta_cmds.html 我们先SSH 到AIX 系 ...
- Matlab三维绘图
三维绘图 1 三维绘图指令 类 别 指 令 说 明 网状图 mesh, ezmesh 绘制立体网状图 meshc, ezmeshc 绘制带有等高线的网状图 meshz 绘制带有“围裙”的网状图 曲面图 ...
- Odoo 8.0 new API 之one装饰
one装饰器的作用是对每一条记录都执行对应的方法,相当于traditional-style中的function 应用举例: 定义的columns now = fields.Datetime(compu ...
- tp-04 框架与模板的整合
- php if语句判定my查询是否为空
<?php header("Content-type: text/html; charset=utf-8"); $username=$_GET['username']; $p ...
- 关于VS2013的安装遇到的问题
老师突然说实验一需要用代码实现,我之前配置的cocos的编程环境是cocos+VS2013,是很稳定的 但是,我安装unity5.5的时候,不小心选择了顺带安装了VS2015,就等于我电脑里面有了两个 ...
- Linux 串口编程
今天对应用层串口编程进行了验证.程序来源于以下参考链接,自己进行了一些注释和更改,记录于此. Tony Liu, 2016-6-17, Shenzhen 参考链接 https://www.ibm.co ...
- Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务
Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...
- SpringCloud架构设计
最近一直在针对SpringCloud框架做项目,从中踩了不少的坑,也渐渐梳理出了一些内容,由于SpringCloud作为一个全家桶,其中东西太多,所以这时候就要有所取舍,这里就想把自己比较常用组件及架 ...
- java---springMVC与strutsMVC的区别
spring mvc与struts的区别 标签: strutsspringmvcservletactiontomcat 2011-11-24 17:34 24205人阅读 评论(6) 收藏 举报 分 ...