Submission #1698740


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;

  int dist1 = 0, dist2 = 0;
  int ans = 0;
  queue<int> que;
  int cnt = 0;
  for(int i=0;i<s.size();i++) {
    if(s[i] == '(')
      cnt++;
    else {
      que.push(i);
      cnt--;
    }
    if(cnt < 0) {
      dist1 = que.front();
      que.pop();
      s[dist1] = '(';
      cnt += 2;
      ans++;
    }
  }
  vector<int> v(s.size(), 0);
  cnt = 0;
  for(int i=0;i<s.size();i++) {
    if(s[i] == '(')
      cnt++;
    else
      cnt--;
    v[i] = cnt;
  }
  if(v[s.size()-1] > 0) {
    int opn = v[s.size()-1] / 2;
    int idx = s.size() - 1;
    while(idx > 0) {
      if(v[idx-1] >= 2 * opn)
        idx--;
      else
        break;
    }
    cout << ans + idx + opn << endl;
  } else {
    cout << ans + dist1 << endl;
  }


}

Submission Info

Submission Time
Task B - 天下一魔力発電
User hkr
Language C++14 (GCC 5.4.1)
Score 400
Code Size 941 Byte
Status AC
Exec Time 5 ms
Memory 768 KB

Judge Result

Set Name All
Score / Max Score 400 / 400
Status
AC × 32
Set Name Test Cases
All 01_sample_01, 01_sample_02, 01_sample_03, 02_manual_01, 02_manual_02, 02_manual_03, 10_random_00, 10_random_01, 10_random_02, 10_random_03, 10_random_04, 10_random_05, 10_random_06, 10_random_07, 10_random_08, 10_random_09, 10_random_10, 10_random_11, 10_random_12, 10_random_13, 10_random_14, 10_random_15, 10_random_16, 10_random_17, 10_random_18, 10_random_19, 20_max_00, 20_max_01, 20_max_02, 20_max_03, 20_max_04, 20_max_05
Case Name Status Exec Time Memory
01_sample_01 AC 5 ms 768 KB
01_sample_02 AC 1 ms 256 KB
01_sample_03 AC 1 ms 256 KB
02_manual_01 AC 1 ms 256 KB
02_manual_02 AC 1 ms 256 KB
02_manual_03 AC 1 ms 256 KB
10_random_00 AC 1 ms 256 KB
10_random_01 AC 1 ms 256 KB
10_random_02 AC 1 ms 256 KB
10_random_03 AC 1 ms 256 KB
10_random_04 AC 1 ms 256 KB
10_random_05 AC 1 ms 256 KB
10_random_06 AC 1 ms 256 KB
10_random_07 AC 1 ms 256 KB
10_random_08 AC 1 ms 256 KB
10_random_09 AC 1 ms 256 KB
10_random_10 AC 1 ms 256 KB
10_random_11 AC 1 ms 256 KB
10_random_12 AC 1 ms 256 KB
10_random_13 AC 1 ms 256 KB
10_random_14 AC 1 ms 256 KB
10_random_15 AC 1 ms 256 KB
10_random_16 AC 1 ms 256 KB
10_random_17 AC 1 ms 256 KB
10_random_18 AC 1 ms 256 KB
10_random_19 AC 1 ms 256 KB
20_max_00 AC 1 ms 256 KB
20_max_01 AC 1 ms 256 KB
20_max_02 AC 1 ms 256 KB
20_max_03 AC 1 ms 256 KB
20_max_04 AC 1 ms 256 KB
20_max_05 AC 1 ms 256 KB