another 4 sorting and searching problems
This commit is contained in:
19
CSES - CSES Problem Set/Maximum_Subarray_Sum.cpp
Normal file
19
CSES - CSES Problem Set/Maximum_Subarray_Sum.cpp
Normal 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;
|
||||
}
|
||||
28
CSES - CSES Problem Set/Movie_Festival.cpp
Normal file
28
CSES - CSES Problem Set/Movie_Festival.cpp
Normal 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;
|
||||
}
|
||||
25
CSES - CSES Problem Set/Restaurant_Customers.cpp
Normal file
25
CSES - CSES Problem Set/Restaurant_Customers.cpp
Normal 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;
|
||||
}
|
||||
33
CSES - CSES Problem Set/Sum_of_Two_Values.cpp
Normal file
33
CSES - CSES Problem Set/Sum_of_Two_Values.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user