有N个玩具,大小分别为A[i];另外有N-1个盒子,大小分别为B[i]。现要再买一个盒子,把所有玩具装到盒子里,要求每个玩具都装一个盒子,并且玩具大小不超过盒子大小。问买的盒子至少为多大?如果无法满足,输出-1。
2<=N<=2E5, 1<=A[i],B[i]<=1E9
分析:将玩具按从大到小排序再依次处理,每次用不小于玩具大小的最小盒子来装。
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int N;
std::cin >> N;
std::vector<int> a(N);
for (int i = 0; i < N; i++) {
std::cin >> a[i];
}
std::sort(a.rbegin(), a.rend());
std::multiset<int> st;
for (int i = 1; i < N; i++) {
int x;
std::cin >> x;
st.insert(x);
}
std::vector<int> remain;
for (int i = 0; i < N; i++) {
auto it = st.lower_bound(a[i]);
if (it != st.end()) {
st.erase(it);
} else {
remain.push_back(a[i]);
}
}
if (remain.size() > 1) {
std::cout << -1 << "\n";
} else {
std::cout << remain[0] << "\n";
}
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}
标签:Box,std,盒子,int,玩具,st,abc376C,remain,Another
From: https://www.cnblogs.com/chenfy27/p/18487203