题意: 给出n个数字,要求在这n个数中选出至少两个数字,使得它们的和在l,r之间,并且最大的与最小的差值要不小于x。n<=15

Problem - 550B - Codeforces

二进制

利用二进制, 第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枚举】的更多相关文章

  1. DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad

    题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...

  2. 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

    题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...

  3. 水题 Codeforces Round #306 (Div. 2) A. Two Substrings

    题目传送门 /* 水题:遍历一边先找AB,再BA,再遍历一边先找BA,再AB,两种情况满足一种就YES */ #include <cstdio> #include <iostream ...

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

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

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

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

  8. Codeforces Round #306 (Div. 2) ABCDE(构造)

    A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...

  9. Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation

    还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门  Problem - D - Codeforces 题意 n个数字,n ...

随机推荐

  1. 关于IIS站点最大并发量分析

    关于IIS站点最大并发量分析,会有如下这个疑问:IIS站点最大并发量是多少? 一般为:   IIS站点最大并发量=队列长度+进程数量[即最大工作进程数] 通过这个公式,可以基本评估出一个IIS站点的最 ...

  2. [环境部署] Windows Server 2016 配置指南 之 安装 Redis3.0

    Redis是一个开源的高级key-value(键-值)缓存与存储,以高性能著称.用于做对象缓存,可以获得极佳的性能体验,可是 Redis 的官方开发团队并没有开发针对 Windows 的版本,不过还好 ...

  3. luoguP6622 [省选联考 2020 A/B 卷] 信号传递(状压dp)

    luoguP6622 [省选联考 2020 A/B 卷] 信号传递(状压dp) Luogu 题外话: 我可能是傻逼, 但不管我是不是傻逼, 我永远单挑出题人. 题解时间 看数据范围可以确定状压dp. ...

  4. B树、B+树、B*树三者的对比详解

    转载至:https://www.2cto.com/database/201805/745822.html 对比 B+树是B树的变体,B*树又是B+树的变体,是一脉相承法治国拉的,不断解决新一阶段的问题 ...

  5. Ls 命令执行什么功能?可以带哪些参数,有什么区别?

    ls 执行的功能:列出指定目录中的目录,以及文件 哪些参数以及区别:a 所有文件 l 详细信息,包括大小字节数,可读可写可执行的权限等

  6. web端,app端,小程序端测试差异详解

    前置解释:1.单纯从功能测试的层面上来讲的话,APP 测试.web 测试和H5测试在流程和功能测试上是没有区别的2.Web项目或pc项目都是在电脑上进行测试的.常见的PC项目架构有BS架构和CS架构的 ...

  7. Zookeeper 有哪几种几种部署模式?

    部署模式:单机模式.伪集群模式.集群模式.

  8. ClassNotFoundException: org.springframework.web.context.ContextLoadServlet

    web.xml中配置 <!-- 配置spring核心监听器,默认会以 /WEB-INF/applicationContext.xml作为配置文件 --> <listener> ...

  9. Springmvc入门基础(一) ---基于idea创建demo项目

    Springmvc是什么 Springmvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来,如下图: Springmvc处理流程 ---- ...

  10. 使用Pycharm获取Resources目录里的内容

    def get_resource_path(path: str) -> str: """\ 获取Resources目录里的资源 :param path: :retu ...