結果
問題 | No.22 括弧の対応 |
ユーザー | 佐藤竜也 |
提出日時 | 2021-06-25 13:03:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,461 bytes |
コンパイル時間 | 1,867 ms |
コンパイル使用メモリ | 168,512 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-25 07:40:22 |
合計ジャッジ時間 | 2,646 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, s, n) for (int i = (s); i < (int)(n); i++) #define rep2(i, n, s) for (int i = (n - 1); i >= (s); i--) template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } ll p(ll a, ll m, ll mod = 0){ if (!mod) { if (m == 0) return 1; if (m % 2 == 0) return p(a * a, m / 2); return p(a * a, m / 2) * a; } else { if (m == 0) return 1; if (m % 2 == 0) return p(1L * a * a % mod, m / 2, mod); return p(1L * a * a % mod, m / 2, mod) * a % mod; } } ll INF = 1000000000000000000; // rep(i, 0, 4) => 上下左右, rep(i, 0, 8) => 斜めも含む vector<int> dy = {-1, 0, 1, 0, -1, -1, 1, 1}; vector<int> dx = {0, 1, 0, -1, -1, 1, 1, -1}; int main() { int N, K; string S; cin >> N >> K >> S; int c = 0; if (S[K - 1] == '(') { rep(i, K, N) { if (S[i] == '(') c++; else c--; if (c < 0) { cout << i + 1 << endl; return 0; } } } else { rep2(i, K - 1, 0) { if (S[i] == ')') c++; else c--; if (c < 0) { cout << i + 1 << endl; return 0; } } } }