比赛链接: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或者状态压缩模板)的更多相关文章

  1. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  2. poj 3311 floyd+dfs或状态压缩dp 两种方法

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6436   Accepted: 3470 ...

  3. 靶形数独 (dfs+预处理+状态压缩)

    #2591. 「NOIP2009」靶形数独 [题目描述] 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们 ...

  4. Codeforces Round #306 (Div. 2), problem: (B) Preparing Olympiad【dfs或01枚举】

    题意: 给出n个数字,要求在这n个数中选出至少两个数字,使得它们的和在l,r之间,并且最大的与最小的差值要不小于x.n<=15 Problem - 550B - Codeforces 二进制 利 ...

  5. 最大联通子数组之和(dfs,记忆化搜索,状态压缩)

    最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...

  6. 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 ...

  7. CF Preparing Olympiad (DFS)

    Preparing Olympiad time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. 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 ...

  9. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

随机推荐

  1. Makefile 9——为依赖关系文件建立依赖关系

    现在我们再对complicated项目做一些更改,增加程序文件间依赖关系的复杂度. /× main.c ×/ #include"foo.h" int main(void) { fo ...

  2. 1.1.18 zabbix监控NFS

    1.1.1 zabbix监控NFS 第一步创建脚本: 添加执行权限 chmod +x   cat /server/scripts/nfs_check.sh [root@web02 scripts]# ...

  3. Thinlphp 模版 foreach 嵌套在 另一个循环出现不能在次循环问题。

    把 foreach 循环改成 volist 就可以重置循环所以的数据.

  4. jQuery控制的不同方向的滑动(横向滑动等)

    引入jquery.js,复制以下代码,即可运行. <style type="text/css"> .slide { position: relative; height ...

  5. MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用

    学习框架一个比较好的路径阅读源码.本文介绍的SqlSessionFactory和SqlSession.可以通过了解SqlSessionFactory接口和SqlSession接口以及两个的实现类入手, ...

  6. 【BZOJ】1030: [JSOI2007]文本生成器(递推+ac自动机)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1030 其实做了1009也不会感到很难了,无非将kmp变成了ac自动机. 设f[i,j]表示前i个串当 ...

  7. gcc编译程序的流程

    >>gcc编译器 gcc编译器:(C语言的编译器gcc/g++) gcc编译程序的流程 源文件(.c)——>预处理(.i)——>编译——>汇编(.s)——>链接(. ...

  8. 开启mysql日志及若干问题

    今天学习了mysql日志功能,以前也有所了解,只不过没有深入的学习,所以趁着“余热”,把我从网上找到的资料与实践 结合起来,总结一下其基本用法.学习从来都不是无趣的,就看你怎么看待学习. 1.查看查询 ...

  9. localStorage变更事件当前页响应新解-awen

    html5的localStorage相信大家都是很熟悉了,但是在chrome等支持该对象的浏览器中(ie10除外),如果你监听storage变更事件你就会发现,当数据发生变化时本页是监听不到stora ...

  10. 经验总结20--C#模拟WEB请求

    非常多语言能够使用代码进行WEB请求,获取到须要的数据. 方便调用别人的接口,自己进行处理. HttpWebRequest request = WebRequest.Create(url) as Ht ...