20160225模拟

T1

const
    maxn=200005;
var
    x,y,z,next:array[-2*maxn..2*maxn]of longint;
    seg:array[0..4*maxn,1..4]of longint;
    i,j,k:longint;
    n,m,t1,t2,ans:longint;
function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b); end;

procedure build(a,l,r:longint);
var mid:longint;
begin
    seg[a,1]:=l; seg[a,2]:=r; seg[a,3]:=0; seg[a,4]:=l;
    if l=r then exit;
    mid:=(l+r)>>1;
    build(a<<1,l,mid); build(a<<1+1,mid+1,r);
end;

procedure update(a,b,c:longint);
var mid:longint;
begin
    if seg[a,1]=seg[a,2] then begin seg[a,3]:=c; exit; end;
    mid:=(seg[a,1]+seg[a,2])>>1;
    if b<=mid then update(a<<1,b,c) else update(a<<1+1,b,c);
    if seg[a<<1,3]>seg[a<<1+1,3]
    then begin seg[a,3]:=seg[a<<1,3]; seg[a,4]:=seg[a<<1,4]; end else 
    if seg[a<<1,3]<seg[a<<1+1,3]
    then begin seg[a,3]:=seg[a<<1+1,3]; seg[a,4]:=seg[a<<1+1,4]; end
    else begin seg[a,3]:=seg[a<<1+1,3]; seg[a,4]:=seg[a<<1,4]; end;
end;

begin   
    readln(n,m);
    for i:=1 to n do
        y[i]:=-i;
    for i:=1 to n do
        begin
            read(x[i]);
            next[y[x[i]]]:=i;
            y[x[i]]:=i;
        end;
    for i:=-n to n do
        if next[i]=0 then next[i]:=n+1;
    build(1,1,n);
    for i:=1 to m do
        begin
            y[i]:=1;
            update(1,i,next[-i]);
            z[i]:=next[-i];
        end;
    ans:=0;
    for i:=1 to n do
        begin
            if y[x[i]]=1 then begin update(1,x[i],next[z[x[i]]]); z[x[i]]:=next[z[x[i]]]; continue; end;    
            inc(ans);
            t1:=seg[1,3]; t2:=seg[1,4]; 
            if t1>next[i] 
            then begin update(1,t2,0); update(1,x[i],next[i]); z[x[i]]:=next[i]; y[t2]:=0; y[x[i]]:=1; end;
        end;
    writeln(ans);   
end.

T2

uses math;
var
    x,p,q:array[0..20050]of longint;
    i,j,k:longint;
    n,l,r,mid,ans,s:longint;
begin
    assign(input,'sbtwo.in'); assign(output,'sbtwo.out'); reset(input); rewrite(output);
    readln(n);
    for i:=1 to n do
        readln(x[i]);
    s:=x[n];
    for i:=1 to n-1 do
        s:=max(s,x[i]+x[i+1]);
    l:=s; r:=2*s; 
    while l<r do
        begin
            mid:=(l+r)>>1;
            p[1]:=x[1]; q[1]:=x[1]; 
            for i:=2 to n do
                begin
                    p[i]:=max(0,x[i]+x[i-1]+x[1]-q[i-1]-mid);
                    q[i]:=min(x[i],x[1]-p[i-1]);
                end;            
            if p[n]=0
            then r:=mid
            else l:=mid+1;
        end;
    writeln(l);
    close(input); close(output);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值