結果

問題 No.684 Prefix Parenthesis
ユーザー 37zigen37zigen
提出日時 2018-05-13 01:19:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 1,010 bytes
コンパイル時間 2,302 ms
コンパイル使用メモリ 75,580 KB
実行使用メモリ 63,076 KB
最終ジャッジ日時 2023-09-10 18:38:07
合計ジャッジ時間 10,278 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,664 KB
testcase_01 AC 125 ms
56,228 KB
testcase_02 AC 128 ms
56,048 KB
testcase_03 AC 218 ms
58,416 KB
testcase_04 AC 244 ms
58,972 KB
testcase_05 AC 275 ms
61,904 KB
testcase_06 AC 212 ms
59,024 KB
testcase_07 AC 249 ms
61,264 KB
testcase_08 AC 156 ms
55,864 KB
testcase_09 AC 153 ms
56,080 KB
testcase_10 AC 287 ms
61,232 KB
testcase_11 AC 213 ms
61,152 KB
testcase_12 AC 155 ms
56,076 KB
testcase_13 AC 257 ms
61,212 KB
testcase_14 AC 260 ms
61,688 KB
testcase_15 AC 210 ms
58,624 KB
testcase_16 AC 231 ms
60,764 KB
testcase_17 AC 194 ms
58,512 KB
testcase_18 AC 131 ms
55,960 KB
testcase_19 AC 256 ms
59,444 KB
testcase_20 AC 266 ms
61,552 KB
testcase_21 AC 282 ms
61,336 KB
testcase_22 AC 219 ms
61,704 KB
testcase_23 AC 235 ms
63,076 KB
testcase_24 AC 236 ms
60,948 KB
testcase_25 AC 268 ms
62,356 KB
testcase_26 AC 126 ms
55,908 KB
testcase_27 AC 126 ms
56,080 KB
testcase_28 AC 126 ms
55,748 KB
testcase_29 AC 260 ms
61,652 KB
testcase_30 AC 257 ms
61,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

	void run() {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		String str = sc.next();
		long ans = N * (N + 1) / 2;
		long sum = 0;
		long min = 0;
		long[][] a = new long[(int) N][2];
		for (int i = 0; i < N; ++i) {
			sum += (str.charAt(i) == '(' ? 1 : -1);
			ans -= sum;
			min = Math.min(min, sum);
			a[i][0] = sum;
			a[i][1] = min;
		}
		Arrays.sort(a, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return -Long.compare(Math.min(o1[1], o1[0] + o2[1]), Math.min(o2[1], o2[0] + o1[1]));
			}
		});
		sum = 0;
		min = 0;
		for (int i = 0; i < N; ++i) {
			min = Math.min(min, a[i][1] + sum);
			sum += a[i][0];
		}
		System.out.println(ans + 2 * min);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0