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 <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
const int mxN = 2e4;
pii seg[4 * mxN]; // {workers, cost}
vector<int> freq[mxN + 1];
void build(int l = 1, int r = mxN, int idx = 1) {
if (l == r) {
seg[idx] = {0, 0};
return;
}
int mid = (l + r) >> 1;
build(l, mid, (idx << 1));
build(mid + 1, r, (idx << 1) | 1);
seg[idx] = {0, 0};
}
void update(int pos, int l = 1, int r = mxN, int idx = 1) {
if (l == r) {
seg[idx].ff++;
seg[idx].ss += pos;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(pos, l, mid, (idx << 1));
else update(pos, mid + 1, r, (idx << 1) | 1);
seg[idx].ff = seg[(idx << 1)].ff + seg[(idx << 1) | 1].ff;
seg[idx].ss = seg[(idx << 1)].ss + seg[(idx << 1) | 1].ss;
}
pii query(int tar, int l = 1, int r = mxN, int idx = 1) {
if (l == r) {
int res = min(seg[idx].ff, tar / l);
return {res, res * l};
}
int mid = (l + r) >> 1;
pii res = {0, 0};
if (seg[(idx << 1)].ss <= tar) {
res = query(tar - seg[(idx << 1)].ss, mid + 1, r, (idx << 1) | 1);
res.ff += seg[(idx << 1)].ff;
res.ss += seg[(idx << 1)].ss;
} else {
res = query(tar, l, mid, (idx << 1));
}
return res;
}
bool cmp(pair<pii, int> &lhs, pair<pii, int> &rhs) {
// lhs.ff.ff / lhs.ff.ss < rhs.ff.ff / rhs.ff.ss (smaller cost per qualification)
return lhs.ff.ff * rhs.ff.ss < rhs.ff.ff * lhs.ff.ss;
}
void solve() {
int n, w;
cin >> n >> w;
vector<pair<pii, int>> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].ff.ff >> v[i].ff.ss;
v[i].ss = i + 1;
}
sort(v.begin(), v.end(), cmp);
// 1st run: find answer
build();
pair<pii, int> best = {{0, 0}, -1}; // {{workers, cost}, bound}
for (int i = 0; i < n; i++) {
update(v[i].ff.ss);
int allow = w * v[i].ff.ss / v[i].ff.ff;
pii res = query(allow);
// cerr << res.ff << ' ' << res.ss << ' ' << allow << endl;
if (res > best.ff) {
best = {res, i};
}
}
// 2nd run: find construction
for (int i = 0; i <= best.ss; i++) {
freq[v[i].ff.ss].pb(v[i].ss);
}
vector<int> ans;
for (int i = 1; i <= mxN; i++) {
if (!best.ff.ff) break;
for (int &x : freq[i]) {
ans.pb(x);
best.ff.ff--;
if (!best.ff.ff) break;
}
}
sort(ans.begin(), ans.end());
cout << ans.size() << endl;
for (int &x : ans) cout << x << endl;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |