codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least l and at most r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least x.
Find the number of ways to choose a problemset for the contest.
The first line contains four integers n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ 106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 106) — the difficulty of each problem.
Print the number of ways to choose a suitable problemset for the contest.
3 5 6 1
1 2 3
2
4 40 50 10
10 20 30 25
2
5 25 35 10
10 10 20 10 20
6
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.
题目信息:从n个数种选择至少两个数,这些数的和在l和r之间,并且这些数的最大值-最小值要大于等于x
思路:由于n个数值很少,那么就采用暴力枚举。关与枚举采用了下面的两种方法:dfs 或者 直接状态压缩枚举所有的状况
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#define N 100005
using namespace std;
int a[];
int n, l, r, x;
int cnt;
int ans;
void dfs(int i, int cc, int sum, int minn, int maxn){
if(cc == cnt){
if(sum>=l && sum<=r && maxn - minn>=x)
++ans;
return;
}
if(i>=n) return;
dfs(i+, cc+, sum+a[i], min(minn, a[i]), max(maxn, a[i]));
dfs(i+, cc, sum, minn, maxn);
} int main(){
cin>>n>>l>>r>>x;
for(int i=; i<n; ++i)
cin>>a[i];
for(int i=; i<=n; ++i){
cnt = i;
dfs(, , , , -);
}
cout<<ans<<endl;
return ;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#define N 100005
using namespace std;
int a[];
int n, l, r, x; int main(){
cin>>n>>l>>r>>x;
for(int i=; i<n; ++i)
cin>>a[i];
int s = <<n;
int cnt = ;
for(int i=; i<s; ++i){
int sum = , minn = , maxn = -;
for(int j=; j<n; ++j)
if((<<j)&i){
sum += a[j];
minn = min(minn, a[j]);
maxn = max(maxn, a[j]);
}
if(sum>=l && sum<=r && maxn-minn>=x)
++cnt;
}
cout<<cnt<<endl;
return ;
}
codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)的更多相关文章
- CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)
[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每一个题目有对应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x, ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举
题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...
- hdu 4033 状态压缩枚举
/* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)
题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...
- 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 ...
- Preparing Olympiad---cf550B(DFS或者状态压缩模板)
比赛链接:http://codeforces.com/problemset/problem/550/B 给你n个数,选出来只是2个然后求他们的和在L和R的区间内,并且选出来的数中最大值和最小值的差不得 ...
随机推荐
- windows里面的批处理命令不停地处理同一条命令
最近在设置路由表的时候,为了方便,做了一个批处理 route.bat route delete 0.0.0.0route delete 10.25.0.0route ADD 10.25.0.0 MAS ...
- http://blog.csdn.net/chenriwei2/article/details/38047119
SSP或者说是空间金字塔匹配(spatial pyramid matching or SPM)是BoW的一个扩展,它把一张图片划分为从不同的分辨率级别然后聚合这些不同分辨率的图像,在深度学习之前SPM ...
- Ubuntu12.04下编译OpenCv2.4.9程序
引用地址http://blog.163.com/huai_jing@126/blog/static/171861983201311103411229/ 方法1:直接命令编译: g++ main.cpp ...
- 每天记一些php函数,jQuery函数和linux命令(一)
简介:学习完了php和jQuery之后,对函数的记忆不到位,导致很多函数没记住,所以为了促进自己的记忆,每天花一点时间来写这个博客. 时间:2016-12-18 地点:太原 天气:晴 一. ...
- Python学习之路--Socket
Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...
- 初探Lambda表达式
简单例子 Expression<Func<; 了解Net方法,没有比IL来得更加容易.反编译IL代码如下(截取部分显示) [] <<int32, bool>> ex ...
- swift语言特性
最近苹果推出了他们新的开发语言,swift,他们自己的说法是,swift语言将会更快捷,更安全等等.但是具体的性能,还需要在后面的实践过程中去观察,但是就目前来说swift语言除了将大部分21世纪静态 ...
- map() 函数
map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...
- Visual Studio2013(Update4)无法生成依赖项关系图解决方案
今天为了看别人写的一套框架,就琢磨着有没视图的方式去看别人每个类和方法之间的调用和继承关系.这个时候发现了一个VS2013自带的功能: 带着兴奋的心情小手一抖一点... 我等呀等,等呀等.... 尼玛 ...
- week 4 日志
周一 上上个星期感冒,上个星期看完奇幻森林后痔疮发作,打了整整一礼拜的针,有点背.. 今天看了 css知多少(6)——选择器的优先级 http://www.cnblogs.com/wangfupeng ...