Skip to content

Commit 0a1b51b

Browse files
committed
added day7 binary search solutions
1 parent 7e0c491 commit 0a1b51b

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/*
3+
4+
Submission link: https://codeforces.com/contest/1538/submission/356096805
5+
6+
TC - O(N \log N)
7+
SC - O(N)
8+
9+
Approach :
10+
Sort the numbers first so they are in increasing order. This makes searching easier.
11+
Take one number and look for another number after it such that their sum lies between l and r.
12+
Find the range of numbers that can be added to the current number to stay within l and r.
13+
Count how many numbers fall in that range and add this count to the answer.
14+
15+
*/
16+
17+
#include <iostream>
18+
#include <vector>
19+
#include <algorithm>
20+
21+
using namespace std;
22+
long long countPairsUnder(const vector<int>& a, int limit) {
23+
long long count = 0;
24+
int left = 0;
25+
int right = a.size() - 1;
26+
while (left < right) {
27+
if (a[left] + a[right] <= limit) {
28+
count += (right - left);
29+
left++;
30+
} else {
31+
right--;
32+
}
33+
}
34+
return count;
35+
}
36+
37+
void solve() {
38+
int n, l, r;
39+
cin >> n >> l >> r;
40+
vector<int> a(n);
41+
for (int i = 0; i < n; i++) {
42+
cin >> a[i];
43+
}
44+
sort(a.begin(), a.end());
45+
long long result = countPairsUnder(a, r) - countPairsUnder(a, l - 1);
46+
cout << result << "\n";
47+
}
48+
49+
int main() {
50+
int t;
51+
cin >> t;
52+
while (t--) {
53+
solve();
54+
}
55+
return 0;
56+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Submission link: https://codeforces.com/contest/1777/submission/356097157
3+
- Tc: O(n log n + n√max(a))
4+
- Sc: O(m)
5+
6+
Approach :
7+
Sort the students by smartness so they are in increasing order.
8+
Use two pointers to form a window of students, adding one student at a time from the right.
9+
Each student can cover some topics, and we keep track of which topics are covered inside the window.
10+
When all topics are covered, try removing students from the left to make the smartness difference as small as possible.
11+
*/
12+
13+
#include <iostream>
14+
#include <vector>
15+
#include <algorithm>
16+
17+
using namespace std;
18+
const int MAXA = 100005;
19+
vector<int> divisors[MAXA];
20+
void precompute() {
21+
for (int i = 1; i < MAXA; i++) {
22+
for (int j = i; j < MAXA; j += i) {
23+
divisors[j].push_back(i);
24+
}
25+
}
26+
}
27+
28+
void solve() {
29+
int n, m;
30+
cin >> n >> m;
31+
vector<int> a(n);
32+
for (int i = 0; i < n; i++) cin >> a[i];
33+
34+
sort(a.begin(), a.end());
35+
36+
vector<int> count(m + 1, 0);
37+
int covered_topics = 0;
38+
int min_diff = 1e9;
39+
int left = 0;
40+
41+
for (int right = 0; right < n; right++) {
42+
for (int d : divisors[a[right]]) {
43+
if (d > m) break;
44+
if (count[d] == 0) covered_topics++;
45+
count[d]++;
46+
}
47+
while (covered_topics == m) {
48+
min_diff = min(min_diff, a[right] - a[left]);
49+
50+
for (int d : divisors[a[left]]) {
51+
if (d > m) break;
52+
count[d]--;
53+
if (count[d] == 0) covered_topics--;
54+
}
55+
left++;
56+
}
57+
}
58+
59+
if (min_diff == 1e9) cout << -1 << "\n";
60+
else cout << min_diff << "\n";
61+
}
62+
63+
int main() {
64+
precompute();
65+
66+
int t;
67+
cin >> t;
68+
while (t--) {
69+
solve();
70+
}
71+
return 0;
72+
}

0 commit comments

Comments
 (0)