[JOISC 2017 Day 2] Arranging Tickets — Solution
Main Idea
Holy crap, this one is seriously hard. I spent a whole afternoon staring blankly at pty's solution.
First, consider turning this cycle into a chain. Among the \(m\) intervals, those satisfying \(a_i > b_i\) can first be flipped directly into \([1, a_i) \cup [b_i, n]\). We now need to minimize the number of times covered.
Let's consider binary search on the answer, and then how do we go about it?
First, there's a conclusion: if two intervals have no intersection, then they won't both be flipped. After flipping, the answer will definitely get larger; this is obvious.
So conversely, the intervals that are flipped together must share a common part. We can pick a common point \(t\) and do a greedy: we scan from \(1\) toward \(t - 1\), and each time we reach the left endpoint of a segment, we throw its right endpoint and \(c\) into a max-heap. During the scan, if the point's own coverage count already exceeds \(ans\), we pick the interval at the top of the heap and flip it.
However, there's a problem: this flipping is not free of aftereffects. We can only guarantee that the left endpoint is to the left of \(t\), but later flips can make the current coverage count exceed the answer again. We need to know the total number of flips in advance, so that we can work out what the final state of the point actually is if we flip. Clearly, a flip is needed if the following condition holds:
\[ now - x + cnt - x > ans \]
Here, \(x\) is the number of segments currently flipped, \(now\) is the number of times covered so far, and \(cnt\) is the remaining number of times.
Next come a few properties:
Property 1: Let the intersection of the flipped intervals be \([l, r)\), and find \(t \in [l, r)\) such that \(b_t\) (the coverage count after flipping) is maximized. Then \(b_t = \max(b_i)\) or \(b_t = \max(b_i) - 1\).
Proof 1: By contradiction. If the maximum is not inside \([l, r)\), consider un-flipping the flipped interval with the leftmost right endpoint and the one with the rightmost left endpoint. The maximum outside will not increase, every position is still valid, and \(b_t\) gets larger, so \(b_t\) must be the maximum. So the number of flips falls into two cases, \(cnt = ans - a_t, cnt = ans - a_t + 1\).
Property 2: \(t\) satisfies \(a_t = \max(a_i)\).
Proof 2: Again by contradiction. If it doesn't hold, there exists some \(a_i > a_t, i \not \in [l, r)\), so at least one flipped interval doesn't contain \(i\), which means \(b_i - a_i > b_t - a_t\). But we also have \(b_i > b_t + 1\), and so it's bogus.
Then for this \(t\), you just take the leftmost and rightmost points and check them.
Code
// LOJ2393.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 200;
typedef long long ll;
typedef pair<int, ll> pil;
int n, m;
ll ai[MAX_N], bi[MAX_N];
vector<pil> tags[MAX_N];
struct segment
{
int l, r, c;
} segs[MAX_N];
bool judge(ll threshold, int pos, ll rev_cnt)
{
if (threshold < rev_cnt)
return false;
priority_queue<pil> pq;
for (int i = 1; i <= n; i++)
bi[i] = 0, tags[i].clear();
for (int i = 1; i <= m; i++)
if (segs[i].l <= pos && pos < segs[i].r)
tags[segs[i].l].push_back(make_pair(segs[i].r, segs[i].c));
ll curt = 0;
for (int i = 1; i <= pos; i++)
{
for (pil v : tags[i])
pq.push(v);
while (ai[i] - curt + rev_cnt > threshold)
{
if (pq.empty())
return false;
pil u = pq.top();
pq.pop();
ll crev = min(u.second, (ai[i] - curt + rev_cnt - threshold + 1) >> 1);
curt += crev, rev_cnt -= crev, bi[u.first] += (crev << 1);
if (u.second != crev)
pq.push(make_pair(u.first, u.second - crev));
}
}
bi[pos + 1] -= curt;
for (int i = pos + 1; i <= n; i++)
{
bi[i] += bi[i - 1];
if (bi[i] + ai[i] > threshold)
return false;
}
return true;
}
bool check(ll threshold, int pos)
{
return judge(threshold, pos, ai[pos] - threshold) || judge(threshold, pos, ai[pos] - threshold + 1);
}
int main()
{
// freopen("02-06.in", "r", stdin);
scanf("%d%d", &n, &m);
ll l = 0, r = 0, res = 0;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &segs[i].l, &segs[i].r, &segs[i].c);
if (segs[i].l > segs[i].r)
swap(segs[i].l, segs[i].r);
ai[segs[i].l] += segs[i].c, ai[segs[i].r] -= segs[i].c;
r += segs[i].c;
}
for (int i = 1; i <= n; i++)
ai[i] += ai[i - 1];
int lpt = 0, rpt = 0;
for (int i = 1; i <= n; i++)
{
if (ai[i] > ai[lpt] || lpt == 0)
lpt = i;
if (ai[i] >= ai[rpt])
rpt = i;
}
while (l <= r)
{
ll mid = (l + r) >> 1;
if (check(mid, lpt) || check(mid, rpt))
r = mid - 1, res = mid;
else
l = mid + 1;
}
printf("%lld\n", res);
return 0;
}
orz