Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

恕我冒昧我用除法判斜率过了

Posted by Hoblovski at 2014-06-09 08:31:51 on Problem 1180
In Reply To:pascal用除法判斜率的小心精度问题,poj的精度是在爆表 Posted by:wukewen at 2014-03-21 09:53:05
SF最大只能达到10^6级别,ST也一样
您只要用的不是单精度浮点数
我觉得1e-7都可以轻松过
顺便extended有效数字位数20位[左右]
qv[i]: q[i]和q[i+1]的斜率

program poj1180;

const maxn=10017;

var st,sf,f,q:array[0..maxn] of longint;
    qv:array[0..maxn] of extended;
    n,s,frt,rer:longint;

function slope(k1,k2:longint):extended;
begin
exit( ((f[k2]-f[k1])+(int64(st[k1])*int64(sf[k1])-int64(st[k2])*int64(sf[k2]))+int64(s)*(int64(sf[k2])-int64(sf[k1])))
        /
      (sf[k1]-sf[k2])
);
end;

procedure init;
var i,j,k:longint;
begin
readln(n); readln(s); for i:=1 to n do begin
        readln(st[i],sf[i]); inc(st[i],st[i-1]); inc(sf[i],sf[i-1]);
end; for i:=0 to n do sf[i]:=sf[n]-sf[i];
end;

function dp:longint;
var i,j:longint;
begin
frt:=1; rer:=1; q[1]:=0; f[0]:=0;
for i:=1 to n do begin
        while (frt<rer)and(st[i]>=qv[frt]) do inc(frt); j:=q[frt];
        f[i]:=f[j]+(st[i]-st[j]+s)*sf[j];
        while (frt<rer)and(qv[rer-1]>=slope(q[rer],i)) do dec(rer);
        inc(rer); q[rer]:=i; qv[rer-1]:=slope(q[rer-1],i);
end; exit(f[n]);
end;

begin
init;
writeln(dp);
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator