-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB550_CF.java
More file actions
136 lines (92 loc) · 3.06 KB
/
Copy pathB550_CF.java
File metadata and controls
136 lines (92 loc) · 3.06 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.Scanner;
public class B550_CF {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
int l=scn.nextInt();
int r=scn.nextInt();
int x=scn.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)arr[i]=scn.nextInt();
int ans=0;
for(int mask=0;mask<(1<<n);mask++){
int sum=0;
int count=0;
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i=0;i<n;i++){
if((mask & (1<<i))!=0){
count++;
sum+=arr[i];
min=Math.min(min,arr[i]);
max=Math.max(max,arr[i]);
}
}
if(count>=2 && sum>=l && sum<=r && (max-min)>=x){
ans++;
}
}
System.out.println(ans);
}
}
/*
PROBLEM INTUITION (BITMASKING)
The problem asks us to count all valid problem sets.
Observation:
Every problem has only TWO choices:
1. Include it
2. Don't include it
Since each problem has a Yes/No choice, every possible selection of
problems is simply a SUBSET.
Constraint:
n <= 15
Total possible subsets = 2^n.
For n = 15,
2^15 = 32768
which is small enough to check every subset.
------------------------------------------------------------
HOW BITMASKING HELPS
We represent every subset using a binary number (called a mask).
Example:
Problems = [10, 20, 30]
Mask 000 -> {}
Mask 001 -> {10}
Mask 010 -> {20}
Mask 011 -> {10,20}
Mask 100 -> {30}
Mask 101 -> {10,30}
Mask 110 -> {20,30}
Mask 111 -> {10,20,30}
Every bit represents one problem.
Bit = 1 -> Problem is selected
Bit = 0 -> Problem is not selected
------------------------------------------------------------
OUTER LOOP
for(mask = 0; mask < (1<<n); mask++)
(1<<n) = 2^n
The outer loop visits EVERY possible subset exactly once.
------------------------------------------------------------
INNER LOOP
For each mask, we check every problem.
if ((mask & (1<<i)) != 0)
means:
"Is the i-th problem selected in the current subset?"
If YES:
- Add its difficulty to sum
- Increase count
- Update minimum difficulty
- Update maximum difficulty
------------------------------------------------------------
After processing one subset, check if it satisfies:
1. At least 2 problems
2. l <= sum <= r
3. max - min >= x
If all conditions are true,
answer++
------------------------------------------------------------
KEY IDEA TO REMEMBER
Bitmasking is NOT used to calculate the answer.
Bitmasking is only used to GENERATE EVERY SUBSET efficiently.
After generating one subset, we simply compute the required
values (sum, count, min, max) and check the conditions.
*/