P6246 [IOI 2000] 邮局
题意
给定 \(n\) 个点,第 \(i\) 个点的坐标是 \(a_i\)。你现在要把其中恰好 \(m\) 个点打上标记,定义总权值为每个点到距离它最近的点的距离之和,求最小总权值。
一次加强:\(n\le 3000,m\le 300\)
二次加强:\(n\le 5\times10^5\)
分析
统计距离它最近的点太难做了,考虑到要求最小值的最小值,我们可以直接让一个点随便指定它到其左边还是右边最近,设 \(f_i\) 为考虑前 \(i\) 个点的贡献,转移式子显然可以写成 \(f_i=\min_{j<i}f_j+w(j,i)\),其中 \(w(l,r)=\min_{i=l+1}^r a_i\times(i-l)-(s_i-s_l)+(s_r-s_i)-a_i\times(r-i)\),其中 \(s_i\) 是 \(a_i\) 的前缀和。
通过你喜欢的方法可以得到 \(w(j,i)\) 满足四边形不等式,所以 \(f_i\) 不仅满足决策单调性,答案关于 \(m\) 还是凸的。根据决策单调性 \(P(l,r-1)\le P(l,r)\le P(l+1,r)\),暴力找最优决策点就是 \(O(n^2)\),通过一次加强。
由于答案是凸的,套一个 wqs 扔掉 \(m\) 的限制,二分斜率 \(K\),由于答案凸包的斜率全是非正整数,所以可以二分非负整数,DP 的时候由减去斜率改为加上斜率。决策单调性仍然存在,二分栈可以做到 \(O(n\log n\log V)\),不知道能不能过。
把转移式展开,\(f_i=\min_{k<j\le i}f_k+a_j\times(j-k)-(s_j-s_k)+(s_i-s_j)-a_j\times(i-j)+K\),发现 \(j\) 前半部分和后半部分相对独立,考虑设 \(g_i=\min_{j<i}f_j+a_i\times(i-j)-(s_i-s_j)\),此时 \(f_i=\min_{j\le i}g_j+(s_i-s_j)-a_j\times(i-j)+K\),此时两个式子都满足斜率优化的形式,且横坐标和斜率都是单调的,可以线性处理,所以最终复杂度 \(O(n\log V)\)。
附转化后的斜优式:
小细节:由于三点贡献的存在,最终二分出来的 \(m'\) 可能不是想要的 \(m\),但此时算答案仍然是 \(f_n-m\times K\)。
小细节 2:插入时横坐标相同的数要处理!!!!
点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<array>
#include<tuple>
#include<ctime>
#include<random>
#include<cassert>
#include<chrono>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0)
#define OTIE cout.tie(0)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
int qwqx=0,qwqf=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
if(qwqx<0){qwqx=-qwqx;putchar('-');}
int qwqy=0;static char qwqz[40];
while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
while(qwqy--){putchar(qwqz[qwqy]);}if(ch)putchar(ch);
}
bool Mbg;
const int mod=998244353;
template<typename T1,typename T2>inline void adder(T1 &x,T2 y){x+=y,x=x>=mod?x-mod:x;}
template<typename T1,typename T2>inline void suber(T1 &x,T2 y){x-=y,x=x<0?x+mod:x;}
const int maxn=5e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
const __int128 e=1;
int n,m;
i64 a[maxn],s[maxn];
i64 f[maxn],g[maxn];
int cntf[maxn],cntg[maxn];
struct point{
i64 x,y;
int c;
point(){x=y=c=0;}
point(i64 _x,i64 _y,int _c=0){x=_x,y=_y,c=_c;}
friend point operator-(point x,point y){
return point(x.x-y.x,x.y-y.y,0);
}
friend bool operator<(point x,point y){
if(x.x<0)x.x=-x.x,x.y=-x.y;
if(y.x<0)y.x=-y.x,y.y=-y.y;
return e*x.y*y.x<e*x.x*y.y;
}
friend bool operator>(point x,point y){
if(x.x<0)x.x=-x.x,x.y=-x.y;
if(y.x<0)y.x=-y.x,y.y=-y.y;
return e*x.y*y.x>e*x.x*y.y;
}
};
point sta_f[maxn],sta_g[maxn];
int tp_f,tp_g;
bool ck(i64 K){
sta_f[tp_f=1]=point(0,0,0),tp_g=0;
f[0]=0,cntf[0]=0;
int pf=1,pg=1;
rep(i,1,n){
while(pf<tp_f&&point(1,a[i])>sta_f[pf+1]-sta_f[pf])++pf;
g[i]=sta_f[pf].y-a[i]*sta_f[pf].x+a[i]*i-s[i],cntg[i]=sta_f[pf].c;
point nw(a[i],g[i]+a[i]*i+K-s[i],cntg[i]);
if(nw.x==sta_g[tp_g].x){
if(nw.y>=sta_g[tp_g].y)goto fff;
--tp_g;
}
while(pg<tp_g&&nw-sta_g[tp_g]<sta_g[tp_g]-sta_g[tp_g-1])--tp_g;
sta_g[++tp_g]=nw;
fff:
while(pg<tp_g&&point(1,i)>sta_g[pg+1]-sta_g[pg])++pg;
f[i]=sta_g[pg].y-sta_g[pg].x*i+s[i],cntf[i]=sta_g[pg].c+1;
nw=point(i,f[i]+s[i],cntf[i]);
while(pf<tp_f&&nw-sta_f[tp_f]<sta_f[tp_f]-sta_f[tp_f-1])--tp_f;
sta_f[++tp_f]=nw;
}
return cntf[n]<=m;
}
inline void solve_the_problem(){
n=rd(),m=rd();
rep(i,1,n)a[i]=rd(),s[i]=s[i-1]+a[i];
int l=0,r=1e9,res=-1;
while(l<=r){
int mid=(l+r)>>1;
if(ck(mid))res=mid,r=mid-1;
else l=mid+1;
}
ck(res);
write(f[n]-res*m);
}
bool Med;
signed main(){
// freopen("in.in","r",stdin);
fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;
while(_--)solve_the_problem();
}
/*
5 2
1 1 4 5 14
output:7
*/

浙公网安备 33010602011771号