# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
746984 | nguyentunglam | Cake 3 (JOI19_cake3) | C++17 | 1 ms | 340 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<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
pair<int, int> a[N];
int v[N], c[N];
pair<long long, long long> calc(pair<long long, long long> A, pair<long long, long long> B) {
if (A.first == B.first) return A.second < B.second ? A : B;
return A.first > B.first ? A : B;
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int n, m; cin >> n >> m;
vector<ii> lst;
for(int i = 1; i <= n; i++) {
int x, y; cin >> x >> y;
lst.emplace_back(x, y);
}
sort(lst.begin(), lst.end(), [] (const ii &x, const ii &y) {
return x.se < y.se;
});
for(int i = 1; i <= n; i++) tie(v[i], c[i]) = lst[i - 1];
long long l = -1e12, r = 1e12, ans = 0;
while (l <= r) {
long long tax = l + r >> 1;
pair<long long, long long> f, ret;
f = ret = make_pair(-1e18, -1e18);
for(int i = 1; i <= n; i++) {
ret = calc(ret, make_pair(f.first + v[i] - 2 * c[i] - tax, f.second + 1));
pair<long long, long long> g = make_pair(f.first + v[i] - tax, f.second + 1);
f = calc(f, g);
f = calc(f, make_pair(v[i] - tax + 2 * c[i], 1LL));
}
if (ret.second <= m) {
ans = ret.first + m * tax;
r = tax - 1;
}
else l = tax + 1;
}
cout << ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |