洛谷 P4998 信号站 题解

首先,尝试枚举建立信号站的位置是 \(1e16\) 的复杂度, 现在就要 \(O(1)\) 求出每个信号站的不合理值。
设当前枚举的信号站的位置为 \(i\), 在 \(i\) 左边的信号站有 \(cnt_i\) 个.
对于位于信号站左边的信号站。
\[\Sigma_{j=1}^{cnt_i }|i - pos_j|
\]
\[i*cnt_i - \Sigma_{j=1}^{cnt_i} pos_j
\]
对于右边的信号站同理, 使用二分查找 \(cnt_i\)
复杂度 \(O(1e6*log_2N)\)
但是有可能存在拜访位置为负数的情况,那么让每个位置向右偏离 \(+10^6\)
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
//
// By txp2024 www.luogu.com.cn TXP2023 www.github.com
//
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#pragma once
#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <numeric>
#include <ctype.h>
#include <cstdarg>
#include <climits>
#include <time.h>
#include <iostream>
#include <stdint.h>
#define _FREAD true
#define MAX_INF 1e18
#define MAX_NUM_SIZE 35
#define MAXN (size_t)(1e6+5)
typedef long long int ll;
typedef unsigned long long int ull;
template<typename _T>
inline _T fpow(_T a, _T n, _T mod) {
_T base = a, ret = 1;
while (n) {
if (n & 1) {
ret = ret * base;
ret %= mod;
}
base = base * base;
base %= mod;
n >>= 1;
}
return ret % mod;
}
//快读函数声明
template< typename Type >
inline Type fread(Type* p = nullptr);
//快速输出函数
template<typename Type>
inline void fwrite(Type x);
ll pos[MAXN], sum[MAXN], val[(ll)2e6 + 5];
ll n, k, ans;
int main() {
#ifdef _FREOPEN
freopen("input.txt", "r", stdin);
#endif // _FREOPEN
#ifdef _RUN_TIME
clock_t start = clock();
#endif // _RUN_TIME
fread(&n), fread(&k);
for (size_t i = 1; i <= n; i++) {
pos[i] = fread<ll>() + 1e6;
}
std::sort(pos + 1, pos + 1 + n);
for (size_t i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + pos[i];
}
for (size_t i = 0; i <= 2e6; i++) {
ll temp = (std::lower_bound(pos + 1, pos + 1 + n, i) - pos - 1);
if (temp != n + 1 && temp != 0) {
val[i] += i * temp - sum[temp];
}
temp = (std::upper_bound(pos + 1, pos + 1 + n, i) - pos);
if (temp != n + 1) {
val[i] += sum[n] - sum[temp - 1] - i * (n - temp + 1);
}
}
std::sort(val, val + (ll)2e6);
for (size_t i = 0; i < k; i++) {
ans += val[i];
}
printf("%lld\n", ans);
#ifdef _RUN_TIME
printf("The running duration is not less than %ld ms\n", clock() - start);
#endif // _RUN_TIME
return 0;
}
template< typename Type >
inline Type fread(Type* p) {
#if _FREAD
Type ret = 0, sgn = 0, ch = getchar();
while (!isdigit(ch)) {
sgn |= ch == '-', ch = getchar();
}
while (isdigit(ch)) ret = ret * 10 + ch - '0', ch = getchar();
if (p != nullptr) {
*p = Type(sgn ? -ret : ret);
}
return sgn ? -ret : ret;
#else
if (p == nullptr) {
Type temp;
p = &temp;
}
scanf("%lld", p);
return *p;
#endif // _FREAD
}
template<typename Type>
inline void fwrite(Type x) {
int sta[MAX_NUM_SIZE];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) {
putchar(sta[--top] + '0');
} // 48 是 '0'
return;
}
/**
* ,----------------, ,---------,
* ,-----------------------, ," ,"|
* ," ,"| ," ," |
* +-----------------------+ | ," ," |
* | .-----------------. | | +---------+ |
* | | | | | | -==----'| |
* | | | | | | | |
* | | C:\>rp++ | | | |`---= | |
* | | | | | |==== ooo | ;
* | | | | | |(((( [33]| ,"
* | `-----------------' | / |(((( | ,"
* +-----------------------+/ | |,"
* /_)______________(_/ +---------+
* _______________________________
* / oooooooooooooooo .o. oooo /, /-----------
* / ==ooooooooooooooo==.o. ooo= // /\--{)B ,"
* /_==__==========__==_ooo__ooo=_/' /___________,"
*
*/

浙公网安备 33010602011771号