[BZOJ4236] JOIOJI

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=4236

题目大意

给定一个只由 JOI 三个字符组成的字符串,询问满足三种字符数相同的最长字串长度

题解

暴力是求前缀和然后 O(N2) 查询
我们求完前缀和后对 JOI 三个前缀和之间差分以下
这样我们发现要求的其实就是 JOI 相对大小不变
所以我们把它们排个序取个最大值即可

const
    maxn=200005;
var
    x,y:array[0..maxn,1..3]of longint;
    i,j,k:longint;
    n,ans,a:longint;
    cha:char;
function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b); end;

procedure sort(l,r:longint);
var i,j,a,b,c,d,e:longint;
begin
    i:=l; j:=r; a:=x[(l+r)div 2,1]; b:=x[(l+r)div 2,2]; c:=x[(l+r)div 2,3];
    repeat
        while (x[i,1]<a)or((x[i,1]=a)and(x[i,2]<b))or((x[i,1]=a)and(x[i,2]=b)and(x[i,3]<c)) do inc(i);
        while (x[j,1]>a)or((x[j,1]=a)and(x[j,2]>b))or((x[j,1]=a)and(x[j,2]=b)and(x[j,3]>c)) do dec(j);
        if not(i>j) then
        begin
            e:=1; d:=x[i,e]; x[i,e]:=x[j,e]; x[j,e]:=d;
            e:=2; d:=x[i,e]; x[i,e]:=x[j,e]; x[j,e]:=d;
            e:=3; d:=x[i,e]; x[i,e]:=x[j,e]; x[j,e]:=d;
            inc(i); dec(j);
        end;
    until i>j;
    if l<j then sort(l,j);
    if i<r then sort(i,r);
end;

begin
    readln(n);
    for i:=1 to n do
        begin
            read(cha);
            y[i,1]:=y[i-1,1]; y[i,2]:=y[i-1,2]; y[i,3]:=y[i-1,3];
            case cha of
            'J':inc(y[i,1]);
            'O':inc(y[i,2]);
            'I':inc(y[i,3]);
            end;
        end;
    readln;
    for i:=1 to n do
        begin
            x[i,1]:=y[i,2]-y[i,1]; x[i,2]:=y[i,3]-y[i,2]; x[i,3]:=i;
        end;
    x[n+1,1]:=0; x[n+1,2]:=0; x[n+1,3]:=0;
    sort(1,n+1);
    ans:=0; a:=1; x[n+2,1]:=maxlongint; x[n+2,2]:=maxlongint; x[n+2,3]:=maxlongint;
    for i:=2 to n+2 do
        begin
            if (x[i,1]<>x[a,1])or(x[i,2]<>x[a,2])
            then begin ans:=max(ans,x[i-1,3]-x[a,3]); a:=i; end;
        end;
    writeln(ans);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值