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 ...
随机推荐
- wordcloud库 词云
•wordcloud使用方法 常规使用方法 import wordcloud #创建一个词云对象 w = wordcloud.WordCloud(background_color="whit ...
- 盘点|2021年最受欢迎Linux桌面操作系统前十名
镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 阿里云开源镜像站利用云服务上的优势,提供快速.稳定的镜像分发服务.和免费的CDN加速服务.更新频率高,基本上一天一更新,对于Centos/Ubun ...
- CF1404E Bricks (最大权独立集)
考虑把答案进行转化,通过分矩形条,我们能去掉一些夹在#之间的边 那么答案= #个数 - 能去掉的边个数 但去掉是有限制的,同一个#格子的横边和竖边不能同时去掉 把边转化成点,限制变成边. 横竖边的点 ...
- [bzoj3585] Rmq Problem / mex
[bzoj3585] Rmq Problem / mex bzoj luogu 看上一篇博客吧,看完了这个也顺理成章会了( (没错这篇博客就是这么水) #include<cstdio> # ...
- Ubuntu修改登录密码
Ubuntu 修改用户名或密码后无法登陆 解决方法 2017年09月01日 16:48:05 lee_3312 阅读数:2192 解决方法:进入recovery-mode修改登录信息 1)重启系统 ...
- Servlet的会话机制?
因为http协议是无状态协议,又称为一次性连接,所以webapp必须有一种机制 能够记住用户的一系列操作,并且唯一标示一个用户. Cookie: 又称为小饼干,实际就是使用一个短文本保存用户信息, 在 ...
- SQL 语言包括哪几部分?每部分都有哪些操作关键字?
SQL 语言包括数据定义(DDL).数据操纵(DML),数据控制(DCL)和数据查询(DQL) 四个部分. 数据定义:Create Table,Alter Table,Drop Table, Crae ...
- 哪些是重要的bean生命周期方法?你能重载它们吗?
有两个重要的bean 生命周期方法,第一个是setup , 它是在容器加载bean的时候被调用.第二个方法是 teardown 它是在容器卸载类的时候被调用. The bean 标签有两个重要的属性( ...
- Dubbo telnet 命令能做什么?
dubbo 服务发布之后,我们可以利用 telnet 命令进行调试.管理. Dubbo2.0.5 以上版本服务提供端口支持 telnet 命令 连接服务 telnet localhost 20880 ...
- 什么是通用 SQL 函数?
1.CONCAT(A, B) – 连接两个字符串值以创建单个字符串输出.通常用于将两个 或多个字段合并为一个字段. 2.FORMAT(X, D)- 格式化数字 X 到 D 有效数字. 3.CURRDA ...