您的位置:首页 > 其它

【NOI2009】 诗人小G

2012-05-16 09:02 113 查看
解法4:单调性优化

program poet;
type
ty=record
l,r,x:longint;
end;

var
i,j,q:longint;
t:array[0..100000] of string;
n,l,p,max,top:int64;
f,s:array[0..100000] of extended;
_f:array[0..100000] of int64;
g:array[0..100000] of longint;
inn:array[0..100000] of ty;
//==================
function quick(x,y:longint):extended;
var
t,k:extended;
begin
t:=1; k:=x;
while y>0 do
begin
if y and 1=1 then t:=t*k;
k:=k*k;
y:=y >> 1;
end;
exit(t);
end;
//==================
function _quick(x,y:longint):int64;
var
t,k:int64;
begin
t:=1; k:=x;
while y>0 do
begin
if y and 1=1 then
begin
if t/max*k>1 then exit(max);
t:=t*k;
end;
if k/max*k>1 then k:=max else k:=k*k;
y:=y >> 1;
end;
exit(t);
end;
//==================
procedure huisu(x:longint);
var
i:longint;
begin
if g[x]>0 then huisu(g[x]);
for i:=g[x]+1 to x-1 do write(t[i],' ');
writeln(t[x]);
end;
//==================
function getf(i,j:longint):extended;
begin
exit(f[j]+quick(trunc(abs(s[i]-s[j]+i-j-1-l)),p));
end;
//==================
function _getf(i,j:longint):int64;
begin
if _f[j]>=max then exit(max);
exit(_f[j]+_quick(trunc(abs(s[i]-s[j]+i-j-1-l)),p));
end;
//==================
procedure change(x:longint);
var
sl,sr,m,t:longint;
begin
while (inn[top].l>x)and(getf(inn[top].l,inn[top].x)>getf(inn[top].l,x)) do
begin
inn[top-1].r:=inn[top].r;
dec(top);
end;
t:=0; sl:=inn[top].l; sr:=inn[top].r;
if sl<x+1 then sl:=x+1;
while sl<=sr do
begin
m:=(sl+sr) >> 1;
if getf(m,inn[top].x)>getf(m,x) then
begin
t:=m; sr:=m-1;
end else sl:=m+1;
end;
if t=0 then exit;
inn[top].r:=t-1;
inc(top);
inn[top].l:=t;
inn[top].x:=x;
inn[top].r:=n;
end;
//==================
begin
assign(input,'poet.in'); reset(input);
assign(output,'poet.out'); rewrite(output);
max:=1;
for i:=1 to 18 do max:=max*10;
inc(max);
read(q);
while q>0 do
begin
dec(q);
readln(n,l,p);
s[0]:=0; f[0]:=0;
for i:=1 to n do
begin
readln(t[i]);
s[i]:=s[i-1]+length(t[i]);
end;
top:=1;
inn[top].x:=0; inn[top].l:=1; inn[top].r:=n;
j:=1;
for i:=1 to n do
begin
while i>inn[j].r do inc(j);
f[i]:=getf(i,inn[j].x);
g[i]:=inn[j].x;
change(i);
end;
for i:=1 to n do _f[i]:=_getf(i,g[i]);
if _f
>=max then writeln('Too hard to arrange') else
begin
writeln(_f
);
huisu(n);
end;
writeln('--------------------');
end;
close(input); close(output);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: