Codeforces Round #306 (Div. 2) B

题意

给一个长度为n的数组(n<=15)。

从中选出至少两个数,要求满足:

①最大和最小的差不能少于x

②选出的所有数的和在l和r之间

思路

n太小了。sort一下,然后直接枚举最小和最大值,接着dfs中间剩下的即可,发现一个可行的就cnt++。

注意dfs中使用一个状态参数来说明是否考虑sum的检测,从而避免了used数组。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 16;
int s[maxn];
int n;
int mi,ma,x;
int cnt = 0;
void dfs(int st,int t,int sum,int k)
{
    //k=0表示考虑当前的sum
    //k=1表示不考虑当前的sum 因为之前已经考虑过它了
    if(!k && mi<=sum && sum<=ma) cnt++;
    if(st > t) return;
    dfs(st+1,t,sum+s[st],0);
    dfs(st+1,t,sum,1);
}
int main()
{
    scanf("%d",&n);
    scanf("%d%d%d",&mi,&ma,&x);
    for(int i = 0 ; i < n ; i ++) scanf("%d",&s[i]);
    sort(s,s+n);
    for(int i = 0 ; i < n-1 ; i ++) {
        for(int j = i+1 ; j < n ; j ++) {
            if(s[j]-s[i] < x || s[i]+s[j] > ma) continue;
            if(j == i+1) {
                if(s[i]+s[j] >= mi) {
                    //printf("Hello");
                    cnt ++;
                }
                continue;
            }
            dfs(i+1,j-1,s[i]+s[j],0);
        }
    }
    printf("%d\n",cnt);
    return 0;
}
时间: 2024-08-06 07:16:55

Codeforces Round #306 (Div. 2) B的相关文章

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

题目传送门 1 /* 2 DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <cstring> 7 #include <cmath> 8 #include <algorithm> 9 #include <vector> 10 #include <map> 11 #include

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

题目传送门 1 /* 2 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11

Codeforces Round #306 (Div. 2) (ABCE题解)

比赛链接:http://codeforces.com/contest/550 A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and &

Codeforces Round #306 (Div. 2)

Two Substrings 题意:问是否存在不重叠的串AB和BA. 思路:注意ABABA.BABAB这两种情况都应该是YES.所以可以找第一个AB,最后一个BA,如果两者不重叠(即两者不是ABA和BAB这样)可以确保一定是YES,同样如果找第一个BA和最后一个AB可以不重叠一样也是YES. 在python中,str的方法s1.find(s2)可以从字符串s1查找s2第一次出现的位置,找不到则返回-1.rfind()是查找最后一次出现位置. s=raw_input() x1,y1=s.find(

Codeforces Round #306 (Div. 2)——C模拟——Divisibility by Eight

You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes. Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit

Codeforces Round #306 (Div. 2) D.E. 解题报告

D题:Regular Bridge 乱搞.构造 这题乱搞一下就行了.构造一个有桥而且每个点的度数都为k的无向图.方法很多,也不好叙述.. 代码如下: #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #define INF 0x

Codeforces Round #306 (Div. 2)——A——Two Substrings

You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order). Input The only line of input contains a string s of length between 1 an

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

A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全,最后只能很蠢的暴力,把所有的AB和BA的位置求出来,能有一对AB和BA不重叠即可. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 char a[100005]; 5 vector<int> ab; 6 vector<i

Codeforces Round #306 (Div. 2) A

题意 给一个字符串(长度<=10^5).问当中有没有一个"BA"和一个"AB"呢?假设都有而且它们不反复(即ABA不算),输出YES.否则输出NO. 思路 一開始想简单了-.. 我们扫一遍,把全部"AB"字符串中A的索引放入一个vector a,把全部"BA"字符串中B的索引放入还有一个vector b.最后扫一遍两个vector.假设发现一个b的值既不是一个a的值+1,也不是那个a的值-1,那么肯定就存在不反复的&qu