B - Preparing Olympiad
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 109, 1 ≤ 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
Input3 5 6 1
1 2 3Output2Input4 40 50 10
10 20 30 25Output2Input5 25 35 10
10 10 20 10 20Output6Hint
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.
题意:
n个题目,最少取2个使得难度和在l和r之间且极值差不小于x
DFS跑一遍即可。
附AC代码:
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 5 const int INF=1<<30; 6 int n,l,r,x; 7 int a[20]; 8 int ans=0; 9 10 void DFS(int num,int MAX,int MIN,int sum){ 11 if(num==n+1){ 12 return; 13 } 14 if(sum<=r&&sum>=l&&x<=MAX-MIN&&num==n){ 15 ans++; 16 } 17 DFS(num+1,max(MAX,a[num]),min(MIN,a[num]),sum+a[num]);//取 18 DFS(num+1,MAX,MIN,sum);//不取 19 } 20 21 int main(){ 22 cin>>n>>l>>r>>x; 23 for(int i=0;i<n;i++){ 24 cin>>a[i]; 25 } 26 ans=0; 27 DFS(0,0,INF,0); 28 cout<<ans<<endl; 29 return 0; 30 }
p.s.
搜索忘得真是彻底啊摔!
是时候系统的学习一下算法了。