CodeForces 550B Preparing Olympiad 暴搜

来源:互联网 发布:心理学书籍推荐 知乎 编辑:程序博客网 时间:2024/05/29 04:40

题意:

给出N个题目的难度:请你出题满足下列要求

1.题目的数量不得少于两道

2.题目的难度不少于L,不超过R

3.最难的题和最简单的题的难度差不小于K

思路:

显然本题的每种状态都是要考录到的所以要暴力。递推不好写,可以用搜索。BFS也行,DFS也行。下面给出BFS的代码

BFS(1900ms)

#include <bits/stdc++.h>using namespace std;set <int> temp;int l,r,di;int a[20];int n;int ans;map <set<int>,bool>vis;const int INF=0x3f3f3f3f;typedef struct Node{    int maxn;int minn;set <int> vis;int sum;    Node(int aa=0,int ii=0,set <int> vv=temp,int ss=0){        maxn=aa;        minn=ii;        vis=vv;        sum=ss;    }}Node;void bfs(){    vis.clear();    temp.clear();    queue <Node> que;    Node st(0,INF,temp,0);    que.push(st);    while(!que.empty()){        Node t=que.front();        if(t.vis.size()>=2&&t.maxn-t.minn>=di&&t.sum<=r&&t.sum>=l){            //cout<<t.maxn<<' '<<t.minn<<' '<<t.sum<<endl;                ans++;        }        que.pop();        for(int i=0;i<n;i++){            Node ne;            ne.maxn=max(a[i],t.maxn);            ne.minn=min(a[i],t.minn);            ne.vis=t.vis;ne.vis.insert(i);            ne.sum=t.sum+a[i];            if(vis[ne.vis]==0){                que.push(ne);                vis[ne.vis]=1;            }        }    }}int main(){    while(cin>>n>>l>>r>>di){        for(int i=0;i<n;i++)            cin>>a[i];        sort(a,a+n);        ans=0;        bfs();        cout<<ans<<endl;    }}

Description

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 Input

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

Hint

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.









0 0
原创粉丝点击