Codeforces-solutions/Educational Codeforces Round 3/D. Gadgets for dollars and pounds.cpp

201 lines
3.9 KiB
C++

/* Problem URL: https://codeforces.com/contest/609/problem/D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
ll s;
cin >> n >> m >> k >> s;
vl cost[2] = {vl(n), vl(n)};
cin >> cost[0];
cin >> cost[1];
nrep(i, 1, n) {
rmin(cost[0][i], cost[0][i - 1]);
rmin(cost[1][i], cost[1][i - 1]);
}
V<pair<ll, int>> gad[2];
rep(i, m) {
int t;
ll c;
cin >> t >> c;
gad[t - 1].emplace_back(c, i);
}
sortv(gad[0]);
sortv(gad[1]);
ll sum1 = 0;
ll sum2 = 0;
int i = 0;
int j = 0;
for (; i < min((int)gad[0].size(), k); i++) {
sum1 += gad[0][i].first;
}
for (; j < k - i; j++) {
sum2 += gad[1][j].first;
}
int oo = INT32_MAX >> 1;
int ansi = -1;
int ansj = -1;
int ans = oo;
while (i >= 0 && j <= gad[1].size()) {
int low = 0;
int high = n - 1;
int act = -1;
while (low <= high) {
int mid = (low + high) >> 1;
ll now = sum1 * cost[0][mid] + sum2 * cost[1][mid];
if (now <= s) {
act = mid;
high = mid - 1;
continue;
}
low = mid + 1;
}
if (act == -1) {
if (j == gad[1].size()) {
break;
}
i--;
sum1 -= gad[0][i].first;
sum2 += gad[1][j].first;
j++;
continue;
}
if (act < ans) {
ans = act;
ansi = i;
ansj = j;
}
if (j == gad[1].size()) {
break;
}
i--;
sum1 -= gad[0][i].first;
sum2 += gad[1][j].first;
j++;
}
if (ans == oo) {
cout << "-1\n";
return 0;
}
int choice1 = 0;
int choice2 = 0;
nrep(i, 1, ans + 1) {
if (cost[0][i] < cost[0][i - 1]) {
choice1 = i;
}
if (cost[1][i] < cost[1][i - 1]) {
choice2 = i;
}
}
cout << ans + 1 << '\n';
rep(k, ansi) {
cout << gad[0][k].second + 1 << ' ' << choice1 + 1 << '\n';
}
rep(k, ansj) {
cout << gad[1][k].second + 1 << ' ' << choice2 + 1 << '\n';
}
}