aboutsummaryrefslogtreecommitdiff
path: root/02_sorting_and_searching
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-x02_sorting_and_searching/a.outbin0 -> 18400 bytes
-rw-r--r--02_sorting_and_searching/apartments_1084.cpp27
-rw-r--r--02_sorting_and_searching/collecting_numbers_2216.cpp46
-rw-r--r--02_sorting_and_searching/concert_tickets_1091.cpp25
-rw-r--r--02_sorting_and_searching/distinct_numbers_1621.cpp13
-rw-r--r--02_sorting_and_searching/ferris_wheel_1090.cpp17
-rw-r--r--02_sorting_and_searching/maximum_subarray_sum_1643.cpp19
-rw-r--r--02_sorting_and_searching/missing_coin_sum_2183.cpp16
-rw-r--r--02_sorting_and_searching/movie_festival_1629.cpp21
-rw-r--r--02_sorting_and_searching/playlist_1141.cpp22
-rw-r--r--02_sorting_and_searching/restaurant_customers_1619.cpp24
-rw-r--r--02_sorting_and_searching/room_allocation_1164.cpp38
-rw-r--r--02_sorting_and_searching/stick_lengths_1074.cpp16
-rw-r--r--02_sorting_and_searching/sum_of_two_values_1640.cpp27
-rw-r--r--02_sorting_and_searching/towers_1063.cpp17
15 files changed, 328 insertions, 0 deletions
diff --git a/02_sorting_and_searching/a.out b/02_sorting_and_searching/a.out
new file mode 100755
index 0000000..1156f0d
--- /dev/null
+++ b/02_sorting_and_searching/a.out
Binary files differ
diff --git a/02_sorting_and_searching/apartments_1084.cpp b/02_sorting_and_searching/apartments_1084.cpp
new file mode 100644
index 0000000..b5ee47f
--- /dev/null
+++ b/02_sorting_and_searching/apartments_1084.cpp
@@ -0,0 +1,27 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5std::vector<int> readn(int n) {
6 std::vector<int> v(n);
7 for (int i = 0; i < n; i++)
8 std::cin >> v[i];
9 return v;
10}
11
12int main() {
13 int n, m, k;
14 std::cin >> n >> m >> k;
15 auto a = readn(n);
16 auto b = readn(m);
17 std::sort(a.begin(), a.end());
18 std::sort(b.begin(), b.end());
19
20 size_t s{0}, i{0}, j{0};
21 while (i < a.size() && j < b.size()) {
22 if (b[j] > a[i] + k) i++;
23 else if (b[j] < a[i] - k) j++;
24 else { s++; i++; j++; }
25 }
26 std::cout << s << "\n";
27}
diff --git a/02_sorting_and_searching/collecting_numbers_2216.cpp b/02_sorting_and_searching/collecting_numbers_2216.cpp
new file mode 100644
index 0000000..1adcf28
--- /dev/null
+++ b/02_sorting_and_searching/collecting_numbers_2216.cpp
@@ -0,0 +1,46 @@
1#include <iostream>
2#include <vector>
3
4int main() {
5 size_t n, x, s{1}, l{0};
6 std::cin >> n;
7 std::vector<size_t> b(n);
8 for (size_t i = 0; i < n; i++) {
9 std::cin >> x;
10 b[x-1] = i;
11 }
12 for (size_t i = 0; i < n; i++) {
13 s += b[i] < l;
14 l = b[i];
15 }
16 std::cout << s << "\n";
17}
18
19
20// The code below solves a different problem: it finds the minimum number
21// of ascending chains needed to partition the given list of numbers.
22
23#if 0
24
25#include <algorithm>
26#include <iostream>
27#include <vector>
28
29int main() {
30 size_t n;
31 std::vector<size_t> s;
32 std::cin >> n;
33 for (size_t i = 0; i < n; i++) {
34 size_t x;
35 std::cin >> x;
36 auto it = std::lower_bound(
37 s.begin(), s.end(), x, std::greater<size_t>());
38 if (it != s.end())
39 *it = x;
40 else
41 s.push_back(x);
42 }
43 std::cout << s.size() << std::endl;
44}
45
46#endif
diff --git a/02_sorting_and_searching/concert_tickets_1091.cpp b/02_sorting_and_searching/concert_tickets_1091.cpp
new file mode 100644
index 0000000..20333a6
--- /dev/null
+++ b/02_sorting_and_searching/concert_tickets_1091.cpp
@@ -0,0 +1,25 @@
1#include <algorithm>
2#include <iostream>
3#include <set>
4#include <vector>
5
6int main() {
7 size_t n, m;
8 std::cin >> n >> m;
9 std::vector<int> hv(n), t(m);
10 for (size_t i = 0; i < n; i++)
11 std::cin >> hv[i];
12 for (size_t i = 0; i < m; i++)
13 std::cin >> t[i];
14
15 std::multiset<int, std::greater<int>> h(hv.begin(), hv.end());
16
17 for (auto c : t) {
18 if (auto x = h.lower_bound(c); x == h.end()) {
19 std::cout << "-1\n";
20 } else {
21 std::cout << *x << "\n";
22 h.erase(x);
23 }
24 }
25}
diff --git a/02_sorting_and_searching/distinct_numbers_1621.cpp b/02_sorting_and_searching/distinct_numbers_1621.cpp
new file mode 100644
index 0000000..9c39aa3
--- /dev/null
+++ b/02_sorting_and_searching/distinct_numbers_1621.cpp
@@ -0,0 +1,13 @@
1#include <iostream>
2#include <set>
3
4int main() {
5 int n, x;
6 std::set<int> s;
7 std::cin >> n;
8 for (int i = 0; i < n; i++) {
9 std::cin >> x;
10 s.insert(x);
11 }
12 std::cout << s.size() << "\n";
13}
diff --git a/02_sorting_and_searching/ferris_wheel_1090.cpp b/02_sorting_and_searching/ferris_wheel_1090.cpp
new file mode 100644
index 0000000..f64b33b
--- /dev/null
+++ b/02_sorting_and_searching/ferris_wheel_1090.cpp
@@ -0,0 +1,17 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int main() {
6 int n, x;
7 std::cin >> n >> x;
8 std::vector<int> a(n);
9 for (int i = 0; i < n; i++)
10 std::cin >> a[i];
11 std::sort(a.begin(), a.end());
12 size_t i{0}, j{a.size()-1}, s{0};
13 for (; j > i; j--, s++)
14 i += a[i] + a[j] <= x;
15 if (i == j) s++;
16 std::cout << s << "\n";
17}
diff --git a/02_sorting_and_searching/maximum_subarray_sum_1643.cpp b/02_sorting_and_searching/maximum_subarray_sum_1643.cpp
new file mode 100644
index 0000000..24ac320
--- /dev/null
+++ b/02_sorting_and_searching/maximum_subarray_sum_1643.cpp
@@ -0,0 +1,19 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int main() {
6 size_t n;
7 std::cin >> n;
8 std::vector<long long> a(n);
9 for (size_t i = 0; i < n; i++)
10 std::cin >> a[i];
11
12 long long scur{0}, smax{0};
13 for (size_t j = 0; j < n; j++) {
14 scur = std::max(0LL, scur + a[j]);
15 smax = std::max(smax, scur);
16 }
17 if (smax == 0) smax = *std::max_element(a.begin(), a.end());
18 std::cout << smax << "\n";
19}
diff --git a/02_sorting_and_searching/missing_coin_sum_2183.cpp b/02_sorting_and_searching/missing_coin_sum_2183.cpp
new file mode 100644
index 0000000..ed7a74b
--- /dev/null
+++ b/02_sorting_and_searching/missing_coin_sum_2183.cpp
@@ -0,0 +1,16 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int main() {
6 size_t n, s{1};
7 std::cin >> n;
8 std::vector<size_t> a(n);
9 for (size_t i = 0; i < n; i++)
10 std::cin >> a[i];
11 std::sort(a.begin(), a.end());
12 for (auto c : a)
13 if (s < c) break;
14 else s+= c;
15 std::cout << s << std::endl;
16}
diff --git a/02_sorting_and_searching/movie_festival_1629.cpp b/02_sorting_and_searching/movie_festival_1629.cpp
new file mode 100644
index 0000000..4ec1fca
--- /dev/null
+++ b/02_sorting_and_searching/movie_festival_1629.cpp
@@ -0,0 +1,21 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int main() {
6 size_t n;
7 std::cin >> n;
8 std::vector<std::pair<int, int>> v(n);
9 for (size_t i = 0; i < n; i++)
10 std::cin >> v[i].first >> v[i].second;
11 std::sort(v.begin(), v.end());
12 int e{-1}, s{0};
13 for (auto a : v) {
14 if (a.first >= e) {
15 e = a.second;
16 s++;
17 }
18 e = std::min(e, a.second);
19 }
20 std::cout << s << "\n";
21}
diff --git a/02_sorting_and_searching/playlist_1141.cpp b/02_sorting_and_searching/playlist_1141.cpp
new file mode 100644
index 0000000..ff963f7
--- /dev/null
+++ b/02_sorting_and_searching/playlist_1141.cpp
@@ -0,0 +1,22 @@
1#include <algorithm>
2#include <iostream>
3#include <map>
4#include <vector>
5
6int main() {
7 size_t n;
8 std::cin >> n;
9 std::vector<int> v(n);
10 for (size_t i = 0; i < n; i++)
11 std::cin >> v[i];
12
13 size_t l{0}, s{0};
14 std::map<int, size_t> m;
15 for (size_t r = 0; r < n; r++) {
16 if (m.contains(v[r]))
17 l = std::max(l, m.at(v[r])+1);
18 m[v[r]] = r;
19 s = std::max(s, r-l+1);
20 }
21 std::cout << s << "\n";
22}
diff --git a/02_sorting_and_searching/restaurant_customers_1619.cpp b/02_sorting_and_searching/restaurant_customers_1619.cpp
new file mode 100644
index 0000000..8d97e96
--- /dev/null
+++ b/02_sorting_and_searching/restaurant_customers_1619.cpp
@@ -0,0 +1,24 @@
1#include <algorithm>
2#include <iostream>
3#include <queue>
4#include <vector>
5
6int main() {
7 size_t n;
8 std::cin >> n;
9 std::vector<std::pair<int, int>> c(n);
10 for (size_t i = 0; i < n; i++) {
11 int a, b;
12 std::cin >> a >> b;
13 c[i] = {a, b};
14 }
15 std::sort(c.begin(), c.end());
16 std::priority_queue<int, std::vector<int>, std::greater<int>> q;
17 size_t m{0};
18 for (auto d : c) {
19 while (!q.empty() && q.top() < d.first) q.pop();
20 q.push(d.second);
21 m = std::max(m, q.size());
22 }
23 std::cout << m << "\n";
24}
diff --git a/02_sorting_and_searching/room_allocation_1164.cpp b/02_sorting_and_searching/room_allocation_1164.cpp
new file mode 100644
index 0000000..f79cc96
--- /dev/null
+++ b/02_sorting_and_searching/room_allocation_1164.cpp
@@ -0,0 +1,38 @@
1#include <algorithm>
2#include <iostream>
3#include <queue>
4#include <tuple>
5#include <vector>
6
7int main() {
8 int n;
9 std::vector<std::tuple<int, int, int>> v;
10 std::cin >> n;
11 for (int i = 0; i < n; i++) {
12 int a, b;
13 std::cin >> a >> b;
14 v.push_back({a, b, i});
15 }
16 std::sort(v.begin(), v.end());
17
18 using P = std::pair<int, int>;
19 std::priority_queue<P, std::vector<P>, std::greater<>> r;
20 std::vector<int> al(n);
21 for (auto [a, d, i] : v) {
22 int ind = r.size()+1;
23 if (!r.empty()) {
24 auto [x, j] = r.top();
25 if (a > x) {
26 r.pop();
27 ind = j;
28 }
29 }
30 r.push({d, ind});
31 al[i] = ind;
32 }
33
34 std::cout << r.size() << "\n";
35 for (auto i : al)
36 std::cout << i << " ";
37 std::cout << "\n";
38}
diff --git a/02_sorting_and_searching/stick_lengths_1074.cpp b/02_sorting_and_searching/stick_lengths_1074.cpp
new file mode 100644
index 0000000..e41a7d8
--- /dev/null
+++ b/02_sorting_and_searching/stick_lengths_1074.cpp
@@ -0,0 +1,16 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int main() {
6 long long n;
7 std::cin >> n;
8 std::vector<long long> a(n);
9 for (long long i = 0; i < n; i++)
10 std::cin >> a[i];
11 std::sort(a.begin(), a.end());
12 long long s{0}, t{a[n/2]};
13 for (auto x : a)
14 s += std::abs(x-t);
15 std::cout << s << "\n";
16}
diff --git a/02_sorting_and_searching/sum_of_two_values_1640.cpp b/02_sorting_and_searching/sum_of_two_values_1640.cpp
new file mode 100644
index 0000000..75b0f54
--- /dev/null
+++ b/02_sorting_and_searching/sum_of_two_values_1640.cpp
@@ -0,0 +1,27 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int main() {
6 int n, x;
7 std::cin >> n >> x;
8 std::vector<std::pair<int, int>> a(n);
9 for (int i = 0; i < n; i++) {
10 std::cin >> a[i].first;
11 a[i].second = i;
12 }
13 std::sort(a.begin(), a.end());
14
15 int i{0}, j{n-1};
16 while (i < j) {
17 auto [ai, ii] = a[i];
18 auto [aj, ij] = a[j];
19 if (ai + aj < x) i++;
20 else if (ai + aj > x) j--;
21 else {
22 std::cout << ii+1 << " " << ij+1 << "\n";
23 return 0;
24 }
25 }
26 std::cout << "IMPOSSIBLE\n";
27}
diff --git a/02_sorting_and_searching/towers_1063.cpp b/02_sorting_and_searching/towers_1063.cpp
new file mode 100644
index 0000000..b0656f3
--- /dev/null
+++ b/02_sorting_and_searching/towers_1063.cpp
@@ -0,0 +1,17 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int main() {
6 size_t n;
7 std::cin >> n;
8 std::vector<int> v;
9 for (size_t i = 0; i < n; i++) {
10 int x;
11 std::cin >> x;
12 auto it = std::upper_bound(v.begin(), v.end(), x);
13 if (it == v.end()) v.push_back(x);
14 else *it = x;
15 }
16 std::cout << v.size() << "\n";
17}

Generated with cgit - Back to sebastiano.tronto.net