模板题。
每个决策点都有一个作用区间,后来的决策点可能会比先前的优。于是对于每个决策点二分到它会比谁在什么时候更优,得到新的决策点集合与区间。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
typedef long double ll;
using namespace std; const int N=;
const ll MAX=1e18;
int T,n,l,p,top;
ll sm[N],f[N];
char ch[N][];
struct P{ int l,r,p; }q[N]; ll ksm(ll x){
if (x<) x=-x;
ll res=; rep(i,,p) res*=x; return res;
} ll cal(int j,int i){ return f[j]+ksm(sm[i]-sm[j]+(i-j-)-l); } int find(P a,int b){
int l=a.l,r=a.r;
while (l<=r){
int mid=(l+r)>>;
if (cal(a.p,mid)<cal(b,mid)) l=mid+; else r=mid-;
}
return l;
} void DP(){
int st=,ed=; q[]=(P){,n,};
rep(i,,n){
if (st<=ed && i>q[st].r) st++;
f[i]=cal(q[st].p,i);
if (st>ed || cal(i,n)<=cal(q[ed].p,n)){
while (st<=ed && cal(i,q[ed].l)<=cal(q[ed].p,q[ed].l)) ed--;
if (st>ed) q[++ed]=(P){i,n,i};
else{
int t=find(q[ed],i);
q[ed].r=t-; q[++ed]=(P){t,n,i};
}
}
}
} int main(){
freopen("bzoj1563.in","r",stdin);
freopen("bzoj1563.out","w",stdout);
for (scanf("%d",&T); T--; ){
scanf("%d%d%d",&n,&l,&p);
rep(i,,n) scanf("%s",ch[i]);
rep(i,,n) sm[i]=sm[i-]+strlen(ch[i]);
DP();
if (f[n]>MAX) printf("Too hard to arrange\n");
else printf("%lld\n",(long long)(f[n]));
puts("--------------------");
}
return ;
}