結果

問題 No.3144 Parentheses Modification and Rotation (01 Ver.)
ユーザー t98slider
提出日時 2025-05-16 22:18:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 644 bytes
コンパイル時間 3,494 ms
コンパイル使用メモリ 253,672 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-16 22:18:39
合計ジャッジ時間 5,003 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int op(int lhs, int rhs){return min(lhs, rhs);}
constexpr int e(){return 1 << 30;}

int main(){
int n, r, m;
string s;
cin >> n >> s >> r >> m;
if(n % 2 == 1){
cout << "-1\n";
return 0;
}
s += s;
vector<int> sv(2 * n + 1);
for(int i = 0; i < 2 * n; i++){
sv[i + 1] = sv[i] + (s[i] == '(' ? 1 : -1);
}
atcoder::segtree<int, op, e> seg(sv);
int ans = 1 << 30;
for(int i = 0; i < n; i++){
	int mn = min(0, seg.prod(i, i + n) - sv[i]);
	int tot = (-mn + 1) / 2;
	int d = (sv[i + n] + 2 * tot) - sv[i];
	ans = min(ans, d / 2 + tot);
}
cout << ans << '\n';
}
0