結果

問題 No.672 最長AB列
ユーザー ikdikd
提出日時 2018-04-13 22:47:48
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 965 bytes
コンパイル時間 592 ms
コンパイル使用メモリ 78,096 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 19:26:33
合計ジャッジ時間 1,583 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 22 ms
4,376 KB
testcase_10 AC 11 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 38 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 24 ms
4,376 KB
testcase_18 AC 14 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  auto s=readln.chomp.to!(char[]);

  int n=s.length.to!(int);
  bool can(int len){
    if(len>n) return false;
    int na=0, nb=0;
    foreach(i; 0..n){
      if(i<len){
        (s[i]=='A'?na:nb)+=1;
      }else{
        (s[i-len]=='A'?na:nb)-=1;
        (s[i]=='A'?na:nb)+=1;
      }
      if(na==nb && na+nb>=len) return true;
    }
    return false;
  }

  int ok=-1, ng=n+1;
  while(ng-ok>1){
    auto m=(ng+ok)/2;
    if(m&1){
      auto res_l=can(m-1), res_r=can(m+1);
      if(res_l==true && res_r==false){
        ok=m-1; break;
      }else if(res_l==true && res_r==true){
        ok=m;
      }else if(res_l==false && res_r==false){
        ng=m;
      }
    }else{
      (can(m)?ok:ng)=m;
    }
  }
  writeln(ok);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
0