시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 128 MB 51 36 34 73.913%

문제

천방지축 베시(소, 1세)는 외양간을 탈출해서 풀로 뒤덮인 산등성이에 숨었다. 농부인 존씨는 베시를 다시 잡기위해 온 풀숲을 샅샅히 뒤졌지만 찾지 못하였다. 안타깝게도 그는 베시를 찾는데 어려워하고 있다. 존에게 그 풀밭은 N개의 소괄호로 이루어진 문자열처럼 보였기 때문이다. 예를 들면, 아래와 같다.

)((()())())

존은 베시의 뒷다리가 왼쪽 소괄호 두 개가 붙어있는 것 (( 과 똑같이 생긴 것을 알고있다. 또한 베시의 앞다리는 오른쪽 소괄호 두 개가 붙어있는 것 )) 과 똑같이 생겼다. 베시의 위치는 뒷다리의 위치가 x이고 앞다리의 위치가 y라고 할때 x < y가 되는 쌍으로 표현될 수 있다.

이때, 베시가 서있는 위치가 될 수 있는 서로 다른 순서쌍들의 개수를 구하여 존을 도와보자.

입력

첫 째줄에 N개의 소괄호로 이루어진 문자열이 주어진다. (1 ≤ N ≤ 50,000)

출력

첫 째줄에 베시가 서있을 수 있는 위치의 개수를 출력한다. (즉,  가 나타나는 곳의 인덱스 x와 가 나타나는 곳의 인덱스 y에서 x<y가 되는 서로 다른 순서쌍들의 개수를 출력한다.)

예제 입력 1

)((()())())

예제 출력 1

4

힌트

예제에서 베시가 있을 수 있는 곳은 아래의 네가지이다.

  1. )()(())
  2. )()(())
  3. )()())(
  4. )()())(
W3sicHJvYmxlbV9pZCI6IjU4NzQiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWMxOGNcdWI5N2MgXHVjYzNlXHVjNTQ0XHViNzdjIiwiZGVzY3JpcHRpb24iOiI8cD5cdWNjOWNcdWJjMjlcdWM5YzBcdWNkOTUmbmJzcDtcdWJjYTBcdWMyZGMoXHVjMThjLCAxXHVjMTM4KVx1YjI5NCBcdWM2NzhcdWM1OTFcdWFjMDRcdWM3NDQgXHVkMGM4XHVjZDljXHVkNTc0XHVjM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HQ7IHkgYXQgd2hpY2ggdGhlcmUgaXMgdGhlIHBhdHRlcm4gKCggYXQgaW5kZXggeCBhbmQgdGhlIHBhdHRlcm4gKSkgYXQgaW5kZXggeS48XC9saT48XC91bD4iLCJoaW50IjoiPGg0Pk91dHB1dCBEZXRhaWxzPFwvaDQ+PHA+VGhlcmUgYXJlIDQgcG9zc2libGUgbG9jYXRpb25zIGZvciBCZXNzaWUsIGluZGljYXRlZCBiZWxvdzo8XC9wPjxwPjEuICkoKCgpKCkpKCkpIF5eICAgXl48XC9wPjxwPjIuICkoKCgpKCkpKCkpIF5eICBeXjxcL3A+PHA+My4gKSgoKCkoKSkoKSkgXl4gICAgIF5ePFwvcD48cD40LiApKCgoKSgpKSgpKSBeXiAgICAgIF5ePFwvcD4iLCJvcmlnaW5hbCI6IjEiLCJwcm9ibGVtX2xhbmdfY29kZSI6Ilx1YzYwMVx1YzViNCJ9XQ==

출처

Olympiad > USA Computing Olympiad > 2012-2013 Season > USACO November 2012 Contest > Bronze 1번