description
有(n)个人要坐(k)辆车。如果第(i)个人和第(j)个人同坐一辆车,就会产生(w_{i,j})的代价。
求最小化代价。(nle4000)
sol
凸优化+决策单调性优化
这么一讲其实这题就已经做完了,复杂度(O(nlog nlog w))
code
(bzoj)上需要卡常。上网蒯个读入优化模板就行了。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi(){
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 4005;
struct node{int j,l,r;}q[N];
int n,k,s[N][N],f[N],g[N],hd,tl;
int cal(int j,int i){
return f[j]+(s[i][i]-s[i][j]-s[j][i]+s[j][j]>>1);
}
bool better(int i,int j,int k){
int si=cal(i,k),sj=cal(j,k);
return si<sj||(si==sj&&g[i]<g[j]);
}
int binary(int i,int j){
int l=q[tl].l,r=n,res=0;
while (l<=r){
int mid=l+r>>1;
if (better(i,j,mid)) res=mid,r=mid-1;
else l=mid+1;
}
return res;
}
void solve(int c){
q[hd=tl=1]=(node){0,0,n};
for (int i=1;i<=n;++i){
++q[hd].l;if (q[hd].l>q[hd].r) ++hd;
f[i]=cal(q[hd].j,i)+c;g[i]=g[q[hd].j]+1;
if (hd>tl||better(i,q[tl].j,n)){
while (hd<=tl&&better(i,q[tl].j,q[tl].l)) --tl;
if (hd>tl) q[++tl]=(node){i,i,n};
else{
int x=binary(i,q[tl].j);
q[tl].r=x-1;q[++tl]=(node){i,x,n};
}
}
}
}
int main(){
n=gi();k=gi();
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+gi();
int l=0,r=s[n][n],res=0;
while (l<=r){
int mid=l+r>>1;solve(mid);
if (g[n]<=k) res=mid,r=mid-1;else l=mid+1;
}
solve(res);printf("%d
",f[n]-k*res);return 0;
}