题意: 给出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. mysql优化参数 (汇总)

    1 如下为128G内存32线程处理器的mariadb配置参数优化: [client]#password= your_passwordport= 3306 socket= /tmp/mysql.sock ...

  2. [SPDK/NVMe存储技术分析]007 - 初识UIO

    注: 要进一步搞清楚SSD盘对应的PCI的BAR寄存器的映射,有必要先了解一下UIO(Userspace I/O). UIO(Userspace I/O)是运行在用户空间的I/O技术.在Linux系统 ...

  3. Linux的权限总结

    一般权限和特殊权限可控制 文件所有者.所有组.其他人的读写执行权限, 而隐藏权限则可以进行补充权限,可限制 文件内容只能追加内容,不更新属性等信息 ACL则可以进行让某个用户或组或other拥有指定文 ...

  4. spring 提供了哪些配置方式?

    基于 xml 配置 bean 所需的依赖项和服务在 XML 格式的配置文件中指定.这些配置文件通常 包含许多 bean 定义和特定于应用程序的配置选项.它们通常以 bean 标签开 头. 例如: &l ...

  5. 学习FastDfs(一)

    一.简介 FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器(storage server)和客户端(client)三个部分组成 fastfds有 ...

  6. 数组(Array)和列表(ArrayList)有什么区别

    Array可以包含基本类型和对象类型,ArrayList只能包含对象类型 Array大小固定,ArrayList的大小是动态变化的. ArrayList提供了更多的方法和特性:比如 :addAll() ...

  7. websocket 实现简单网页版wechat

    1.群聊 web - socket--基于TCP/UDP http - 无状态的短链接 长连接:客户端和服务器保持永久性的链接,除非有一方主动断开, 轮询:客户端和服务端不断连接,然后断开,请求响应; ...

  8. Effective Java —— 覆盖equals时遵守通用约定

    本文参考 本篇文章参考自<Effective Java>第三版第十条"Obey the general contract when overriding equals" ...

  9. 路径规划—BUG算法

  10. c/c++中的i++和++i的区别

    使用 i++ vs. ++i i++是先赋值再加1 ++i是先加1再赋值 到目前为止,你已经学习了如何编写下面这样的 C++ for 循环: for (int i = 0; i < 10; i+ ...