結果

問題 No.684 Prefix Parenthesis
ユーザー te-shte-sh
提出日時 2018-05-16 14:05:28
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 789 bytes
コンパイル時間 569 ms
コンパイル使用メモリ 97,148 KB
実行使用メモリ 6,660 KB
最終ジャッジ日時 2023-09-03 20:11:55
合計ジャッジ時間 1,762 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 7 ms
4,376 KB
testcase_05 AC 8 ms
5,884 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 11 ms
5,580 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 7 ms
5,852 KB
testcase_14 AC 6 ms
5,588 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 7 ms
5,888 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 12 ms
5,816 KB
testcase_22 AC 7 ms
5,100 KB
testcase_23 AC 6 ms
5,028 KB
testcase_24 AC 7 ms
5,112 KB
testcase_25 AC 7 ms
5,132 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  long n; readV(n);
  string s; readV(s);

  auto a = new long[](n);
  foreach (i; 0..n) a[i] = s[i] == '(' ? 1 : -1;
  foreach (i; 1..n) a[i] = a[i-1] + a[i];

  struct AM { long a, m; }
  auto am = new AM[](n), m = 0L;
  foreach (i; 0..n) {
    m = min(m, a[i]);
    am[i] = AM(a[i], m);
  }

  am.sort!((a, b) => a.m == b.m ? a.a > b.a : a.m > b.m);
  auto ami = am[0];
  foreach (i; 1..n) {
    ami.m = min(ami.m, ami.a + am[i].m);
    ami.a += am[i].a;
  }

  writeln(n*(n+1)/2 - ami.a + ami.m*2);
}
0