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 <queue> 12 #include <set> 13 #include <string> 14 #include <stack> 15 using namespace std; 16 17 typedef long long ll; 18 const int MAXN = 1e4 + 10; 19 const int INF = 0x3f3f3f3f; 20 int a[20]; 21 bool vis[20]; 22 int n, l, r, x; 23 int ans; 24 25 void DFS(int sum, int cnt, int s, int p) 26 { 27 if (sum >= l && cnt >= 2 && a[p] - a[s] >= x) ans++; 28 for (int i=p+1; i<=n; ++i) 29  { 30 if (sum + a[i] <= r) DFS (sum + a[i], cnt + 1, s, i); 31  } 32 } 33 34 int main(void) //Codeforces Round #306 (Div. 2) B. Preparing Olympiad 35 { 36 while (scanf ("%d%d%d%d", &n, &l, &r, &x) == 4) 37  { 38 ans = 0; 39 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); 40 sort (a+1, a+1+n); 41 for (int i=1; i<n; ++i) DFS (a[i], 1, i, i); 42 printf ("%d\n", ans); 43  } 44 45 return 0; 46 } 47 48 /* 49 3 5 6 1 50 1 2 3 51 4 40 50 10 52 10 20 30 25 53 5 25 35 10 54 10 10 20 10 20 55 */

 

你可能感兴趣的:(codeforces)