Preparing Olympiad

来源:互联网 发布:剑雨江湖仙器进阶数据 编辑:程序博客网 时间:2024/05/22 22:40
B. Preparing Olympiad
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.

A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least l and at most r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least x.

Find the number of ways to choose a problemset for the contest.

Input

The first line contains four integers nlrx (1 ≤ n ≤ 151 ≤ l ≤ r ≤ 1091 ≤ x ≤ 106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 106) — the difficulty of each problem.

Output

Print the number of ways to choose a suitable problemset for the contest.

Sample test(s)
input
3 5 6 11 2 3
output
2
input
4 40 50 1010 20 30 25
output
2
input
5 25 35 1010 10 20 10 20
output
6
Note

In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.

In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.

In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.

题意:

    就是给出和的最大值与最小值来规定范围,然后要在选的数中找出最大值与最小值相减大于等于x。求出有几种选择的方案。此题有两种解法,一种dfs去遍历,另外一种就是位运算暴力解法。因为从1 - 2<<15的范围内每个数的位数0,1组合是不同的,不会出现重复的现象。所以适合用是1就加数。不懂我在说什么的可以先看看位运算之类的知识先。

AC代码:

#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<set>using namespace std;typedef __int64 ll;#define T 20int a[T],bo[T],n,rn,ln,xn,cnt;void dfs(int i,int sum){int k;if(sum>rn)return;if(sum>=ln&&sum<=rn){int mi=0x3f3f3f3f,ma=0;for(int j=0;j<n;++j){if(bo[j])mi=min(mi,bo[j]);ma=max(ma,bo[j]);}//一开始少了if结果mi取0了(bo初值为0)if(ma-mi>=xn)cnt++;}for(k=i;k<n;++k){if(!bo[k]){   bo[k]=a[k];   dfs(k+1,sum+a[k]);   bo[k]=0;}}}int main(){   /* freopen("input.txt","r",stdin);*/int i;while(~scanf("%d%d%d%d",&n,&ln,&rn,&xn)){for(i=0;i<n;++i){scanf("%d",&a[i]),bo[i]=0;}cnt=0;dfs(0,0);printf("%d\n",cnt);}    return 0;}


位运算暴力:

#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <math.h>#include <algorithm>using namespace std;const int inf = 0x3f3f3f3f;int main(){    int n, l, r, x;    int a[20];    while(~scanf("%d%d%d%d",&n,&l,&r,&x))    {    for(int i = 0; i < n; i ++)    {    scanf("%d",&a[i]);    }    int ans = 0;        for(int i = 1; i <= (1 << n); i ++)        {        int cnt = 0;        int sum = 0;        int maxn = 0;        int minn = inf;        for(int j = 0; j < n; j ++)        {        if((1 << j) & i)        {        cnt ++;        sum += a[j];        minn = min(minn, a[j]);        maxn = max(maxn, a[j]);        }        }        if(cnt >= 2 && maxn - minn >= x && sum >= l && sum <= r)        ans ++;        }        printf("%d\n", ans);    }    return 0;}



0 0