CH0601: Genius ACM — Solution
Approach
This problem ate up my whole morning. This morning, after finding out my NOIp 2018 results, I couldn't steady my mind enough to go to class, so I skipped class and came to the computer lab to work on problems. And after poring over the book and the solutions for ages, I pieced together an approach.
We first store the whole sequence in the array \(pre[]\), then, starting from the endpoint \(L=1\), we enumerate the right endpoint. We use binary lifting to speed this up. Write a function \(check(num)\) to test whether the checksum of the interval meets the standard. The checksum is computed by sorting, then taking the largest and the smallest, the second largest and the second smallest, ... and computing with them. For the sorting we can use the idea of merge sort: since our interval lengths are powers of \(2\) and are enumerated from small to large, each time we only need to sort the right half of the interval and then do a merge. See the code for the details.
Code
// CH0601.cpp
#include <cstdio>
#include <iostream>
#include <algorithm>
const int maxn = 5000020;
using namespace std;
int pre[maxn], seq[maxn], tmp[maxn], n, m, k;
long long T;
long long doubleTimes(long long x)
{
return x * x;
}
bool checkHash(int l, int r, int nr)
{
if (l == r)
seq[l] = pre[l];
copy(pre + r + 1, pre + nr + 1, seq + r + 1);
long long sum = 0;
sort(seq + r + 1, seq + nr + 1);
merge(seq + l, seq + r + 1, seq + r + 1, seq + nr + 1, tmp + l);
int p1 = l, p2 = nr, pr = 0;
while (p1 < p2 && pr < m && sum <= T)
sum += doubleTimes(tmp[p1++] - tmp[p2--]), pr++;
if (sum <= T)
copy(tmp + l, tmp + nr + 1, seq + l);
return (sum <= T);
}
int main()
{
scanf("%d", &k);
while (k--)
{
scanf("%d%d%lld", &n, &m, &T);
for (int i = 1; i <= n; i++)
scanf("%d", &pre[i]);
int segs = 0;
int p = 1, R;
for (int L = 1; L <= n; L = R + 1)
{
p = 1, R = L;
for (int i = 1; i <= n; i++)
seq[i] = pre[i];
while (p != 0 && R <= n)
if (checkHash(L, R, min(n, R + p)))
R += p, p <<= 1;
else
p >>= 1;
segs++;
}
printf("%d\n", segs);
}
return 0;
}