题解
1.如果限定在 \(x\) 天内买完,那么限定在 \(x+1\) 天内也能买完,二分浮现。
2.如果要买 \(type\ 1\) ,那么一定是在这 \(x\) 天内 \(a\) 价格最低的那天一次买齐,且优先买价格低的 \(type\ 1\)
3.优先买 单价x汇率 最低的那个
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 114514;
ll n, m, k, s;
ll a[2 * N], b[2 * N];
struct unit {
ll id, val;
bool operator < (const unit &other) const {
return val < other.val;
}
};
vector<unit> dollar, pounds;
struct node {
ll id, day;
} ans[2 * N];
struct {
ll day, val;
} prea[2 * N], preb[2 * N];
bool check(ll x) {
ll it1 = 0, it2 = 0;
ll sum = 0;
for (ll i = 1; i <= k; i++) {
if (it1 < dollar.size() && it2 < pounds.size()) {
if (dollar[it1].val * prea[x].val < pounds[it2].val * preb[x].val) {
sum += dollar[it1].val * prea[x].val;
it1++;
} else {
sum += pounds[it2].val * preb[x].val;
it2++;
}
} else if (it1 < dollar.size()) {
sum += dollar[it1].val * prea[x].val;
it1++;
} else {
sum += pounds[it2].val * preb[x].val;
it2++;
}
}
return sum <= s;
}
void build(ll x) {
ll it1 = 0, it2 = 0;
for (ll i = 1; i <= k; i++) {
if (it1 < dollar.size() && it2 < pounds.size()) {
if (dollar[it1].val * prea[x].val < pounds[it2].val * preb[x].val) {
ans[i].id = dollar[it1].id;
ans[i].day = prea[x].day;
it1++;
} else {
ans[i].id = pounds[it2].id;
ans[i].day = preb[x].day;
it2++;
}
} else if (it1 < dollar.size()) {
ans[i].id = dollar[it1].id;
ans[i].day = prea[x].day;
it1++;
} else {
ans[i].id = pounds[it2].id;
ans[i].day = preb[x].day;
it2++;
}
}
}
void solve() {
cin >> n >> m >> k >> s;
for (ll i = 1; i <= n; i++) cin >> a[i];
for (ll i = 1; i <= n; i++) cin >> b[i];
for (ll i = 1; i <= m; i++) {
ll type, val;
cin >> type >> val;
if (type == 1) dollar.push_back({i, val});
else pounds.push_back({i, val});
}
sort(dollar.begin(), dollar.end());
sort(pounds.begin(), pounds.end());
prea[0].val = 2e9;
prea[0].day = -1;
preb[0].val = 2e9;
preb[0].day = -1;
for (ll i = 1; i <= n; i++) {
prea[i].val = prea[i - 1].val;
prea[i].day = prea[i - 1].day;
preb[i].val = preb[i - 1].val;
preb[i].day = preb[i - 1].day;
if (a[i] < prea[i - 1].val) {
prea[i].val = a[i];
prea[i].day = i;
}
if (b[i] < preb[i - 1].val) {
preb[i].val = b[i];
preb[i].day = i;
}
}
ll l = 0, r = n + 1;
while (l + 1 < r) {
ll mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
if (r == n + 1) cout << "-1\n";
else {
cout << r << '\n';
build(r);
for (ll i = 1; i <= k; i++) cout << ans[i].id << ' ' << ans[i].day << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
while (t--) solve();
return 0;
}
标签:val,pounds,dollars,dollar,day,Gadgets,type,ll
From: https://www.cnblogs.com/pure4knowledge/p/18306122