诗人小G (恶心的四边形不等式证明)
前言:
没有前言(快累死了,不想写)。
solution:
设$ f_i $ 为第 $ i $ 句时最小的不协调度。
令 $ w_{i,j}=(s_i+i)-(s_j+j)-(L+1)$。
这里直接写证明了。
视 \(x=w_{i,j}\)。
视 $ c=a_{i+1}+1 $。
忽略 $ w $ 第一维。
\(\because x_{j+1} \le x_j\)
$\therefore $ 问题转为证明函数 \(f(x)=\left|x\right|^P-\left|x+c\right|^P\) 单调递减。
\(1:P\) 为奇数且 $ x \in \left(-c , 0\right]$。
\(\because P\) 为奇数,\(\therefore P-1\) 为偶数。\(\therefore x^{P-1}\ge0 \,\&\,\left(x+c\right)^{P-1}\ge0\)。
\(\therefore f'(x) \le 0\)。
$\therefore $ 原函数单调递减。
\(2:P\) 为奇数且 $ x\in\left(-\infty,-c\right]$。
\(\because P\) 为奇数,\(\therefore P-1\) 为偶数。\(\therefore x^{P-1}\ge0 \,\&\,\left(x+c\right)^{P-1}\ge0\)。
\(\because x < 0 \quad \therefore x^{P-1} \ge (x+c)^{P-1}\)。
$\therefore $ 原函数单调递减。
\(3:P\) 为奇数且 $ x\in\left(0,\infty\right)$。
\(\because P\) 为奇数,\(\therefore P-1\) 为偶数。\(\therefore x^{P-1}\ge0 \,\&\,\left(x+c\right)^{P-1}\ge0\)。
\(\because c > 0 \quad \therefore x^{P-1} < \left(x+c\right)^{P-1}\)
\(\therefore f'(x) < 0\)。
$\therefore $ 原函数单调递减。
\(4:P\) 为偶数且 $ x \in \left(-\infty,0\right)$。
\(\because x < 0 \quad \therefore P \times x^{P-1} < 0 \quad -P \times (x+c)^{P-1}>0\)
\(\because x<x+c \quad \therefore x^{P-1}<(x+c)^{P-1}\)
\(\therefore f'(x)<0\)
$\therefore $ 原函数单调递减。
\(5: P\) 为偶数且$ x \in \left[0,\infty\right)$。
\(\because x \ge 0 ,c>0\quad \therefore x^{P-1} < (x+c)^{P-1}\)
$\therefore f'(x) < 0 $
$\therefore $ 原函数单调递减。
综上,原函数在 $ P>0,c>0 $ 的条件下单调递减。
所以原dp式满足四边形不等式。
Code:
#include<deque>
#include<math.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define TP template<typename T>
#define TP_ template<typename T,typename ... T_>
TP void read(T &x)
{
x=0;int f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
x*=f;
}
TP_ void read(T &x,T_&...y){read(x);read(y...);}
TP void write(T x){if(x<0){putchar('-'),x=-x;}if(x>9)write(x/10);putchar(48+x%10);}
TP void writeln(const T x){write(x);puts("");}
TP void writesp(const T x){write(x);putchar(' ');}
TP_ void writeln(const T x,T_ ...y){writesp(x);writeln(y...);}
typedef long long LL;
typedef long double LD;
constexpr int N=1e5+5;
char a[N][51];
LD f[N],s[N];
LL n,L,P;
struct node
{
int j,l,r;
}q[N],ans[N],tmp[N];
LD val(int i,int j)
{
LD x=fabs(s[i]-s[j]-L);
LD ans=1;
int b=P;
for(;b;b>>=1)
{
if(b&1)
ans=ans*x;
x*=x;
}
return ans;
}
LD calc(int x,int y){return f[y]+val(x,y);}
int bound(int x,int y)
{
int l=x,r=n+1;
while(l<r)
{
int mid=(l+r)>>1;
if(calc(mid,x)>=calc(mid,y))r=mid;
else l=mid+1;
}
return l;
}
int main()
{
int T;read(T);
while(T--)
{
read(n,L,P);++L;
memset(s,0,sizeof(s));
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
{
scanf("%s",a[i]+1);
s[i]=s[i-1]+strlen(a[i]+1)+1;
}
int l=1,r=1;q[l]={0,1,n};
for(int i=1;i<=n;i++)
{
while(l<=r&&q[l].r<i)l++;
f[i]=calc(i,q[l].j);
ans[i]=q[l];
while(l<=r&&calc(q[r].l,i)<=calc(q[r].l,q[r].j))r--;
int pos=-1;pos=bound(q[r].j,i);
if(pos<=n)
{
q[r].r=pos-1;
q[++r]={i,pos,n};
}
}
if(f[n]>1e18)
puts("Too hard to arrange");
else
{
printf("%.0Lf\n",f[n]);
int len=0;
for(int i=n;i;i=ans[i].j)
tmp[++len]=ans[i];
tmp[0]={n,0,0};
for(int i=len;i>=1;i--)
{
for(int j=tmp[i].j+1;j<tmp[i-1].j;j++)
printf("%s ",a[j]+1);
printf("%s\n",a[tmp[i-1].j]+1);
}
}
puts("--------------------");
}
return 0;
}

浙公网安备 33010602011771号