another 4 sorting and searching problems

This commit is contained in:
2024-06-04 11:04:33 +02:00
parent 4e39870547
commit 7fd24571df
5 changed files with 106 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,19 @@
// Maximum Subarray Sum
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
ll max_sum = INT32_MIN;
ll current_sum = INT32_MIN;
for (int i{}; i < n; i++) {
ll a; cin >> a;
current_sum = max(current_sum + a, a);
max_sum = max(current_sum, max_sum);
}
cout << max_sum << endl;
return 0;
}

View File

@@ -0,0 +1,28 @@
// Movie Festival
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
vector<pair<ll, ll>> movies(n);
for (auto &p: movies) {
ll a, b; cin >> a >> b;
p = {b, a};
}
int count = 0;
int current_time = 0;
std::sort(movies.begin(), movies.end());
for (auto &[end, start]: movies) {
if (start >= current_time) {
current_time = end;
count++;
}
}
cout << count << endl;
return 0;
}

View File

@@ -0,0 +1,25 @@
// Restaurant Customers
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
map<ll, int> m;
for (int i{}; i < n; i++) {
ll a, b;
cin >> a >> b;
m[a]++;
m[b]--;
}
int max_count = 0;
int count = 0;
for (auto &[t, c]: m) {
count += c;
max_count = max(max_count, count);
}
cout << max_count << endl;
return 0;
}

View File

@@ -0,0 +1,33 @@
// Sum of Two Values
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
map<ll, vector<int>> input;
int n; cin >> n;
ll x; cin >> x;
vector<ll> in;
for (int i{}; i < n; i++) {
ll a; cin >> a;
input[a].push_back(i);
in.push_back(a);
}
for (int i{}; i < n; i++) {
ll target = x - in[i];
bool flag = target == in[i];
if (input[target].empty() || (input[target].size() == 1 && flag)) continue;
else {
if (flag) {
cout << i + 1 << " " << input[target][1] + 1<< endl;
} else {
cout << i + 1 << " " << input[target][0] + 1<< endl;
}
return 0;
}
}
cout << "IMPOSSIBLE" << endl;
return 0;
}