# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
305317 | Fdg | Stations (IOI20_stations) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <memory.h>
using namespace std;
vector<pair<int, int>> dp;
void upd(pair<int, int> &r, pair<int, int> a) {
if (r.first == -1 || r.first < a.first || (r.first == a.first && r.second > a.second)) r = a;
}
int find_maximum_unique(int x, int y, vector<int> a, vector<int> b) {
int n = a.size();
vector<pair<int, int>> v;
for (int i = 0; i < n; ++i)
v.push_back({b[i], a[i]});
sort(v.begin(), v.end());
dp.clear();
dp.resize(x + 1, {-1, -1});
dp[0] = {0, 0};
for (int t = 0; t < n; ++t) {
for (int i = x; i >= 0; --i) {
if (dp[i].first == -1) continue;
// Take A.
if (i + v[t].second <= x) {
upd(dp[i + v[t].second], {dp[i].first + 1, dp[i].second});
}
// Take B.
if (dp[i].second + v[t].first <= y) {
upd(dp[i], {dp[i].first + 1, dp[i].second + v[t].first});
}
}
}
int ans = 0;
for (int i = 0; i <= x; ++i)
if (dp[i].first != -1 && dp[i].second <= y)
ans = max(ans, dp[i].first);
return ans;
}
int find_next_station(int s, int t, vector<int> st) {
sort(st.begin(), st.end());
if (s < st[0]) {
if (t < s) return st.back();
for (int x : st)
if (t <= x) return x;
return st.back();
} else {
if (t > s) return st[0];
for (int i = st.size() - 1; i >= 0; --i)
if (st[i] <= t) return st[i];
return st[0];
}
}