Codeforces Round #306 (Div. 2), problem: (B) Preparing Olympiad【dfs或01枚举】
题意: 给出n个数字,要求在这n个数中选出至少两个数字,使得它们的和在l,r之间,并且最大的与最小的差值要不小于x。n<=15
二进制
利用二进制, 第i位为1则加上a[i], 为0则不加,
#include<iostream>
#include <algorithm>
#include <cmath>
#include<map> using namespace std;
typedef long long LL;
int a[20];
int main()
{
int n,l,r,x, res=0;
cin >> n >> l >> r >>x;
for(int i = 0; i < n; i ++)cin >> a[i];
for(int i =1; i < (1<<n); i ++)
{
int idx = 0, sum = 0;
int minn = 999999999, maxx = 0;
for(int j = i; j; j >>=1, idx ++)
if(j&1==1)
{
sum += a[idx];
maxx = max(maxx,a[idx]);
minn = min(minn, a[idx]);
} if(maxx -minn >= x && sum <= r && sum >= l)
res ++;
} cout << res <<'\n'; return 0;
}
dfs方法
#include<iostream>
#include <algorithm>
#include <cmath>
#include<map> using namespace std;
typedef long long LL;
int a[20];
int n,l,r,x, res=0; void dfs(int sum, int maxx, int minn, int num)
{
if(maxx - minn >= x && sum >= l && sum <= r && num==n)
res ++; if(num==n)return;
dfs(sum + a[num], max(maxx, a[num]), min(minn, a[num]), num +1); dfs(sum, maxx, minn, num+1); }
int main()
{ cin >> n >> l >> r >>x;
for(int i = 0; i < n; i ++)cin >> a[i]; dfs(0,0,999999999,0); cout << res <<'\n'; return 0;
}
Codeforces Round #306 (Div. 2), problem: (B) Preparing Olympiad【dfs或01枚举】的更多相关文章
- DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad
题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- 水题 Codeforces Round #306 (Div. 2) A. Two Substrings
题目传送门 /* 水题:遍历一边先找AB,再BA,再遍历一边先找BA,再AB,两种情况满足一种就YES */ #include <cstdio> #include <iostream ...
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- 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 ...
- Codeforces Round #306 (Div. 2)
A. Two Substrings You are given string s. Your task is to determine if the given string s contains t ...
- Codeforces Round #306 (Div. 2)A B C D 暴力 位/暴力 暴力 构造
A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation
还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门 Problem - D - Codeforces 题意 n个数字,n ...
随机推荐
- DAViCal 跨站请求伪造漏洞
受影响系统:DAViCal DAViCal <= 1.1.8描述:CVE(CAN) ID: CVE-2019-18346 DAViCal是一款日历共享服务器. DAViCal 1.1.8及之前版 ...
- CF1452E Two Editorials (思维题,暴力+差分)
题目描述: 给你m个位于[1,n]的区间p,现在有长度为K的区间b和c.设对于区间$p[i]$,定义$a[i]$为$p[i]$分别与b,c相交长度的较大值,现在问区间b和c位于何处时,$\sum p_ ...
- Mybatis框架基础入门(六)--动态sql
主要是通过mybatis提供的各种标签方法实现动态拼接sql. 1.if标签 <!-- 根据条件查询用户 --> <select id="queryUserByWhere& ...
- Elasticsearch 是如何实现 Master 选举的?
1.Elasticsearch 的选主是 ZenDiscovery 模块负责的,主要包含 Ping(节点之 间通过这个 RPC 来发现彼此)和 Unicast(单播模块包含一个主机列表以控制哪 些节点 ...
- IOC 的优点是什么?
IOC 或 依赖注入把应用的代码量降到最低.它使应用容易测试,单元测试不再需 要单例和 JNDI 查找机制.最小的代价和最小的侵入性使松散耦合得以实现.IOC 容器支持加载服务时的饿汉式初始化和懒加载 ...
- 学习Kvm(二)
一.走进云计算 云计算:云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问, 进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这些资源能够被快速提供, ...
- C++ | 再探智能指针(shared_ptr 与 weak_ptr)
上篇博客我们模拟实现了 auto_ptr 智能指针,可我们说 auto_ptr 是一种有缺陷的智能指针,并且在C++11中就已经被摈弃掉了.那么本章我们就来探索 boost库和C++11中的智能指针以 ...
- Numpy使用Matplotlib实现可视化绘图
Numpy使用Matplotlib实现可视化绘图 可以直接将Numpy的数组传给Matplotlib实现可视化绘图: 曲线图 饼图 柱状图 直方图 1. 绘制正弦曲线 2. 绘制饼图 3. 柱状图 4 ...
- 无需Flash录视频——HTML5中级进阶
前言 HTML5的权限越来越大了,浏览器可以直接调用摄像头.麦克风了,好激动啊.我们要用纯洁的HTML代码造出自己的天地. 视频采集 本篇介绍的栗子 都是在chrome 47 版本以上的,低版本的可能 ...
- H5页面实现下载文件(apk、txt等)的三种方式
需求描述 接到的原始需求是这样的,有一个H5页面,页面中有个"点击下载"的按钮,点击之后,完成下载特定的apk.大概是下面这样的: 需求分析 接到需求的时候我偷乐了一下,这个H5页 ...