-
Notifications
You must be signed in to change notification settings - Fork 4
/
Main.java
72 lines (60 loc) · 1.88 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
private static int N;
private static int L;
private static int R;
private static int X;
private static int[] problems;
private static int answer = 0;
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
N = sc.nextInt();
L = sc.nextInt();
R = sc.nextInt();
X = sc.nextInt();
problems = new int[N];
for(int i = 0; i < N; i ++){
problems[i] = sc.nextInt();
}
Arrays.sort(problems);
backTracking(new ProblemSet(), 0);
System.out.println(answer);
}
public static void backTracking(ProblemSet problemSet, int idx){
if(L <= problemSet.difficultySum){
if(problemSet.get(problemSet.size() - 1) - problemSet.get(0) >= X){
answer += 1;
}
}
for(int i = idx; i < N; i ++){
if(problemSet.difficultySum + problems[i] <= R) {
problemSet.addProblem(problems[i]);
backTracking(problemSet, i + 1);
problemSet.popProblem();
}
}
}
static class ProblemSet{
private List<Integer> problems = new ArrayList<>();
public int difficultySum = 0;
public int addProblem(int difficulty){
difficultySum += difficulty;
problems.add(difficulty);
return difficultySum;
}
public void popProblem(){
Integer poped = problems.remove(problems.size() - 1);
difficultySum -= poped.intValue();
}
public int get(int idx){
return problems.get(idx).intValue();
}
public int size(){
return problems.size();
}
}
}