some dp problems

This commit is contained in:
2024-06-14 19:49:10 +02:00
parent 66d059f230
commit b67bf7bbea
21 changed files with 124 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
// Apartments
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n, m;
ll k; cin >> n >> m >> k;
vector<ll> as(n);
for (auto &e: as) cin >> e;
vector<ll> bs(m);
for (auto &e: bs) cin >> e;
std::sort(as.begin(), as.end(), greater<ll>());
std::sort(bs.begin(), bs.end(), greater<ll>());
int count = 0;
while (as.size()) {
int val = as.back();
as.pop_back();
while (bs.size()) {
int val2 = bs.back();
if (val2 > val + k) break;
bs.pop_back();
if (val2 + k >= val && val2 - k <= val) {
count++;
break;
}
}
}
cout << count << endl;
return 0;
}

View File

@@ -0,0 +1,19 @@
// Collecting Numbers
#include<bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
vector<int> in(n);
for (auto &e: in) cin >> e;
int count = 1;
map<int, int> pos;
for (int i{}; i < n; i++) pos[in[i]] = i;
for (int i = 1; i < n; i++) {
if (pos[i] > pos[i+1]) count ++;
}
cout << count << endl;
return 0;
}

View File

@@ -0,0 +1,39 @@
// Collecting Numbers II
#include<bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
int m; cin >> m;
vector<int> in(n);
for (auto &e: in) cin >> e;
int count = 1;
unordered_map<int, int> pos;
for (int i{}; i < n; i++) pos[in[i]] = i;
for (int i = 1; i < n; i++) {
if (pos[i] > pos[i+1]) count ++;
}
// cout << count << endl;
for (int i{}; i < m; i++) {
int a, b; cin >> a >> b;
if (in[a - 1] > 1 && pos[in[a - 1] - 1] > a - 1 && pos[in[a - 1] - 1] <= b - 1) count--;
if (in[a - 1] > 1 && pos[in[a - 1] - 1] < a - 1 && pos[in[a - 1] - 1] >= b - 1) count++;
if (in[a - 1] < n && pos[in[a - 1] + 1] > a - 1 && pos[in[a - 1] + 1] <= b - 1) count++; //
if (in[a - 1] < n && pos[in[a - 1] + 1] < a - 1 && pos[in[a - 1] + 1] >= b - 1) count--;
if (in[b - 1] > 1 && pos[in[b - 1] - 1] > a - 1 && pos[in[b - 1] - 1] < b - 1) count++;
if (in[b - 1] > 1 && pos[in[b - 1] - 1] < a - 1 && pos[in[b - 1] - 1] > b - 1) count--;
if (in[b - 1] < n && pos[in[b - 1] + 1] > a - 1 && pos[in[b - 1] + 1] < b - 1) count--;
if (in[b - 1] < n && pos[in[b - 1] + 1] < a - 1 && pos[in[b - 1] + 1] > b - 1) count++;
pos[in[a - 1]] = b - 1;
pos[in[b - 1]] = a - 1;
swap(in[a-1], in[b-1]);
cout << count << endl;
}
return 0;
}

View File

@@ -0,0 +1,23 @@
// Concert Tickets
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, m; cin >> n >> m;
multiset<ll> price;
for (int i{}; i < n; i++) {ll a; cin >> a; price.insert(a);}
for (int i{}; i < m; i++) {
ll v; cin >> v;
auto it = price.upper_bound(v);
if (it == price.begin()) {
cout << -1 << endl;
} else {
cout << *(--it) << endl;
price.erase(it);
}
}
return 0;
}

View File

@@ -0,0 +1,19 @@
// Distinct Numbers
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int count = 1;
int n; cin >> n;
vector<ll> i(n);
for (auto &e: i) cin >> e;
std::sort(i.begin(), i.end());
for (int j = 1; j < i.size(); j++) {
if (i[j] != i[j-1]) count ++;
}
cout << count << endl;
return 0;
}

View File

@@ -0,0 +1,29 @@
// Ferris Wheel
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n; cin >> n;
ll x; cin >> x;
vector<ll> ps(n);
for (auto &e: ps) cin >> e;
ll count = 0;
ll min = 0;
ll max = n-1;
std::sort(ps.begin(), ps.end());
while (min <= max) {
if (ps[min] + ps[max] <= x) {
max--;
min++;
count++;
} else {
max --;
count++;
}
}
cout << count << endl;
return 0;
}

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,27 @@
// Missing Coin Sum
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
vector<ll> coins(n);
for (auto &e: coins) cin >> e;
std::sort(coins.begin(), coins.end());
ll sum = 0;
for (int i{}; i < n; i++) {
if (coins[i] - 1 > sum) {
break;
} else {
sum += coins[i];
}
}
cout << sum + 1 << 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,28 @@
//
// Created by darian on 07.06.24.
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
vector<ll> in(n);
for (auto &e : in) cin >> e;
int lo = 0;
int hi = 0;
int max_count = 1;
set<ll> current = {in[0]};
while (hi < n - 1) {
hi++;
while (current.count(in[hi])) {
current.erase(in[lo]);
lo++;
}
current.insert(in[hi]);
max_count = max(max_count, (int)current.size());
}
cout << max_count << endl;
}

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,24 @@
// Stick Lengths
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
vector<ll> in(n);
for (auto &e: in) cin >> e;
std::sort(in.begin(), in.end());
ll target = in[n/2];
ll target2 = in[n/2 + 1];
ll count = 0, count2 = 0;
for (auto e: in) {
count += abs(target - e);
count2 += abs(target2 - e);
}
cout << min(count, count2) << 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;
}

View File

@@ -0,0 +1,26 @@
//
// Created by darian on 07.06.24.
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
vector<ll> towers;
for (int i{}; i < n; i++) {
ll a; cin >> a;
auto it = std::lower_bound(towers.begin(), towers.end(), a+1);
if (it == towers.begin()) {
if (towers.size()) towers[0] = a;
else towers.insert(towers.begin(), a);
} else if (it == towers.end()) {
towers.push_back(a);
} else {
towers[it - towers.begin()] = a;
}
}
cout << towers.size() << endl;
}

View File

@@ -0,0 +1,123 @@
//
// Created by darian on 07.06.24.
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//MISSING distances[p.second - p.first]--; !!!!!!
//int main() {
// ll x; cin >> x;
// int n; cin >> n;
// map<ll, int> distances;
// distances[x] = 1;
// set<pair<ll, ll>> lines = {{0, x}};
//
// for (int i{}; i < n; i++) {
// ll t; cin >> t;
// auto it = lines.lower_bound({t, t});
// --it;
// auto p = *it;
// lines.erase(p);
// lines.insert({p.first, t});
// lines.insert({t, p.second});
// distances[p.second - p.first]--;
// if (distances[p.second - p.first] == 0) distances.erase(p.second - p.first);
// distances[t - p.first]++;
// distances[p.second - t]++;
// cout << (*distances.rbegin()).first << endl;
// }
//}
//
//slightly inspirated from usaco
int main() {
ll x; cin >> x;
int n; cin >> n;
set<ll> lights{0, x};
map<ll, int> dist{{x, 1}};
for (int i{}; i < n; i++) {
ll a; cin >> a;
auto it = lights.upper_bound(a);
auto it2 = it;
it2--;
dist[a - *it2]++;
dist[*it - a]++;
dist[*it - *it2]--;
if (!dist[*it - *it2]) {
dist.erase(*it - *it2);
}
lights.insert(a);
cout << (*dist.rbegin()).first << endl;
}
}
//faster solution from usaco
//#include <algorithm>
//#include <iostream>
//#include <vector>
//
//using std::cout;
//using std::endl;
//using std::vector;
//
//int main() {
// int street_len;
// int light_num;
// std::cin >> street_len >> light_num;
// vector<int> lights(light_num);
// for (int &l : lights) { std::cin >> l; }
//
// vector<std::pair<int, int>> sorted_lights(light_num);
// for (int l = 0; l < light_num; l++) { sorted_lights[l] = {lights[l], l}; }
// std::sort(sorted_lights.begin(), sorted_lights.end());
//
// // Given the light position, this array stores its position in
// // sorted_lights.
// vector<int> new_pos(light_num);
// for (int l = 0; l < light_num; l++) {
// new_pos[sorted_lights[l].second] = l;
// }
//
// struct Light {
// int prev, next;
// int pos;
// };
// vector<Light> light_ll(light_num + 2);
// // First, we set our "lights" on the edges of the street.
// light_ll[0] = {-1, 1, 0};
// light_ll[light_num + 1] = {light_num, -1, street_len};
// for (int l = 0; l < light_num; l++) {
// light_ll[l + 1] = {l, l + 2, sorted_lights[l].first};
// }
//
// // Find the longest passage once all the streetlights are added
// vector<int> gaps(light_num);
// int max_gap = 0;
// for (int l = 0; l <= light_num; l++) {
// max_gap = std::max(max_gap, light_ll[l + 1].pos - light_ll[l].pos);
// }
// gaps.back() = max_gap;
//
// // Remove the streetlights in reverse order like as we did in the above
// // solution.
// for (int l = light_num - 1; l > 0; l--) {
// Light to_del = light_ll[new_pos[l] + 1];
// Light &left = light_ll[to_del.prev];
// Light &right = light_ll[to_del.next];
// // Re-assign the references to the next & previous node
// left.next = to_del.next;
// right.prev = to_del.prev;
//
// max_gap = std::max(max_gap, right.pos - left.pos);
// gaps[l - 1] = max_gap;
// }
//
// for (int i = 0; i < gaps.size() - 1; i++) { cout << gaps[i] << ' '; }
// cout << gaps.back() << endl;
//}