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 ...
随机推荐
- oracle 如何在表中有数据的情况下,修改表字段的类型或者增加表字段的长度
场景:项目中某张表的字段长度不够用了,现在要增加其长度 分两种情况: 1.表中没有数据的情况,直接一条sql语句就能解决 alter table 表名 modify(字段名 字 ...
- 记一次Prometheus代理性能优化问题
最近有做一个Prometheus metrics代理的一个小项目,暂称为prom-proxy,目的是为了解析特定的指标(如容器.traefik.istio等指标),然后在原始指标中加入应用ID(当然还 ...
- C249: 'DATA': SEGMENT TOO LARGE“解决方法 | keilC51设置编译模式:SMALL,COMPACT,LARGE
"Keil Cx51编译器提供三条编译模式控制命令:SMALL,COMPACT,LARGE,它们对变量存储器空间的影响如下. SMALL:所有变量都被定义在8051单片机的片内RAM中,对这 ...
- CF1481X Codeforces Round #699
C Fence Painting(构造) 有用的刷子贪心刷,没用的刷子填在后续的有用/已存在的位置(用个栈记一下就行) D AB Graph(图上构造) 把边当做三种类型,aa bb ab m为奇数时 ...
- Linux部署Nacos
此处演示Nacos在Linux(CentOS7)环境中单机版部署,此处演示1.3.0版本. 一.官网下载压缩包 https://github.com/alibaba/nacos/releases 二. ...
- Spring cloud config 客户端连接RabbitMQ 报 socket closed
java.net.SocketException: socket closed at java.net.SocketInputStream.socketRead0(Native Method) ...
- linux管理用户(组)与相关问题处理
相关联文件如下: /etc/passwd/etc/shadow/etc/group ================================= [切换当前用户为root]sudo -i [创建 ...
- 一个tomcat部署两个springboot服务时启动JMX报错
一.问题来源 今天在部署开发好的组件的时候,发现无法启动,检查启动日志,报如下错误: 2022-03-17T10:39:41.823+08:00 ERROR vediomanage.vediomana ...
- 为什么需要消息系统,mysql 不能满足需求吗?
1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据 丢失风险.许多消息队列所采用的& ...
- springBoot集成Elasticsearch抛出Factory method 'restHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restHighLeve ...