QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661803#7683. Hard Brackets ProblemjiangtaoTL 27ms3884kbC++201.2kb2024-10-20 18:16:162024-10-20 18:16:16

Judging History

你现在查看的是最新测评结果

  • [2024-10-20 18:16:16]
  • 评测
  • 测评结果:TL
  • 用时:27ms
  • 内存:3884kb
  • [2024-10-20 18:16:16]
  • 提交

answer

// Problem: G. Hard Brackets Problem
// Contest: Codeforces - 2023 China Collegiate Programming Contest (CCPC) Guilin Onsite (The 2nd Universal Cup. Stage 8: Guilin)
// URL: https://codeforces.com/gym/104768/problem/G
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
// Begin:2024-10-20 17:06:45
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5, M = 1e9 + 7, mod = 998244353;
#define YES {cout << "Yes" << endl; return ;}
#define NO {cout << "No" << endl; return ;}
char st[N];
int top;
void solve(){
	top = 0;
    string s; cin >> s;
    int n = s.size();
    string ans = "";
    for (int i = 0; i < n; i++){
    	char c = s[i];
    	if (c == '(') {
    		st[++top] = c;
    		ans = ans + '(';
    	}
    	else {
    		if (top == 0) {
    			ans = ans + ')';
    		}else if (top != n - i){
    			ans = ans + ')';
    			top--;
    		}else{
    			top--;
    		}
    	}
    }
    if (top) cout << "impossible\n";
    else cout << ans << "\n";
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int ___ = 1;
    cin >> ___;
    while (___--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3552kb

input:

3
((()))
(
)))()

output:

(((
impossible
)))(

result:

ok 3 cases (3 test cases)

Test #2:

score: 0
Accepted
time: 21ms
memory: 3600kb

input:

100000
())(()()()
()())()))
)()()()()
())()(())(
((())()))
)(())()))(
)()))())))
)))))(((()
()))((()((
()((()()))
(()())())
())(()))()
(())(())))
))))(()(()
()))(())((
()((())())
()))()))()
()((())())
()()))((()
()))(()))
(()(())))
())((()))
())()((())
(()(())))
()(((()())
))))())))(
((((()()()
(()(...

output:

impossible
()())()))
)()()()(
impossible
((())()))
impossible
)()))())))
impossible
impossible
()((()(
(()())())
())(()))(
(())(())))
impossible
impossible
()((())(
()))()))(
()((())(
impossible
()))(()))
(()(())))
())(((
impossible
(()(())))
impossible
impossible
impossible
impossible
impossible
()...

result:

ok 100000 cases (100000 test cases)

Test #3:

score: 0
Accepted
time: 17ms
memory: 3588kb

input:

200000
(()))
()))(
)()))
(())
)((((
)(())
)(())
()))(
((()(
()(()
))())
)((((
()())
(())
()(()
(()()
)(())
()))
(()()
()())
(())
()()(
(()((
)())
(()()
(())
))(((
(())
())()
())()
(()))
(((((
)())
(()))
()(()
)()()
(()))
()())
(()))
)(())
(())(
))))(
())((
))(((
))))(
()()
()())
(())
(())
(())
)(())...

output:

(()))
impossible
)()))
((
impossible
)((
)((
impossible
impossible
impossible
))())
impossible
()())
((
impossible
impossible
)((
()))
impossible
()())
((
impossible
impossible
)())
impossible
((
impossible
((
())(
())(
(()))
impossible
)())
(()))
impossible
)()(
(()))
()())
(()))
)((
impossible
imp...

result:

ok 200000 cases (200000 test cases)

Test #4:

score: 0
Accepted
time: 27ms
memory: 3592kb

input:

250000
()()
(())
())
)(((
(()(
(())
)()
(())
(())
))()
)(()
)())
)())
(())
)())
()))
())
(()(
))))
()((
()()
)())
())
())
())
(())
))()
(())
(()(
)))(
()))
())
)))(
)(()
()))
()()
(())
((()
(())
))((
))((
(())
()))
(()(
(())
((()
)(()
(())
((((
())
))()
())
()()
)()(
)()
(())
))()
()))
())
)()
)())
...

output:

()(
((
())
impossible
impossible
((
)(
((
((
))(
impossible
)())
)())
((
)())
()))
())
impossible
))))
impossible
()(
)())
())
())
())
((
))(
((
impossible
impossible
()))
())
impossible
impossible
()))
()(
((
impossible
((
impossible
impossible
((
()))
impossible
((
impossible
impossible
((
impossi...

result:

ok 250000 cases (250000 test cases)

Test #5:

score: 0
Accepted
time: 23ms
memory: 3588kb

input:

50000
)))()((((()(((())(()
)(())()())((())))()(
(((((()))()(((()((()
)()(()())(()())(()()
()(())()()()((())))
)((())))((((())(((((
((())(()()))()(()())
()))())(()))(()())((
))(((())(())()))())
((((()())()))()(((((
((())()))))(((()))))
()()(())((()(()))))
()())())))()()(()))
()))(())()())()(())
()()(...

output:

impossible
impossible
impossible
impossible
()(())()()()((())))
impossible
((())(()()))()(()(
impossible
))(((())(())()))())
impossible
((())()))))(((()))))
()()(())((()(()))))
()())())))()()(()))
()))(())()())()((
impossible
)))))(())()())(((
impossible
)(((()())()(())(
impossible
impossible
imposs...

result:

ok 50000 cases (50000 test cases)

Test #6:

score: 0
Accepted
time: 20ms
memory: 3648kb

input:

83333
((()((())(()
(())(()()))
()(()(())))
((((())))))
)())(((())))
()()())()())
()()((())))
(()(()(((())
()((()()()()
(()()()())))
(()()()(((()
)))))()()())
)((()()())))
))(((()))))
()())(()))()
)()))))()())
)(()((())))(
(()())(()))
())()((())))
(()()()(()))
)())()()(())
((())()))(()
(()(()(((())
)...

output:

impossible
(())(()()))
()(()(())))
((((())))))
)())((((
()()())()())
()()((())))
impossible
impossible
(()()()())))
impossible
)))))()()())
)((()()())))
))(((()))))
()())(()))(
)()))))()())
impossible
(()())(()))
())()((())))
(()()()((
)())()()((
impossible
impossible
impossible
impossible
impossibl...

result:

ok 83333 cases (83333 test cases)

Test #7:

score: 0
Accepted
time: 26ms
memory: 3884kb

input:

250000
)))
)(((
()()
((()
((()
())
())(
))((
)())
()()
()()
())
((()
())
()()
(())
(())
()()
()))
())
)())
()()
)()
())
)()(
(())
)()
)(((
))()
(())
)()
)())
)()
)()
))((
()()
(())
()((
))((
)(((
()))
)())
)())
)())
)())
)(((
())
)))
(())
(())
())(
())
(())
(())
()()
())(
()()
())
))()
(())
(())
)()...

output:

)))
impossible
()(
impossible
impossible
())
impossible
impossible
)())
()(
()(
())
impossible
())
()(
((
((
()(
()))
())
)())
()(
)(
())
impossible
((
)(
impossible
))(
((
)(
)())
)(
)(
impossible
()(
((
impossible
impossible
impossible
()))
)())
)())
)())
)())
impossible
())
)))
((
((
impossible
(...

result:

ok 250000 cases (250000 test cases)

Test #8:

score: 0
Accepted
time: 23ms
memory: 3660kb

input:

50000
(())((()()(())(((()(
()()()((((()())())))
(())(()()()((())))()
(())(()))(()(()()))
()((((()((())))))))
)))())))()()())()))(
()(()))(((())()(((((
()()())())(((()())))
(())(())))(()((())))
()(()(())()()()()(((
()()()((()(()()))))
)(())))()(())()())()
)((((((()())())((())
)()())(()((()(()))))
(()...

output:

impossible
()()()((((()())(
(())(()()()((())))(
(())(()))(()(()(
()((((()((())))))))
impossible
impossible
()()())())(((()(
(())(())))(()(((
impossible
()()()((()(()()))))
)(())))()(())()())(
impossible
)()())(()((()((
(())(()()())()())())
)())()()()()((())(
(())()((())((()))))
))()))(()()))())))(
)...

result:

ok 50000 cases (50000 test cases)

Test #9:

score: 0
Accepted
time: 20ms
memory: 3688kb

input:

83333
((()(())))()
()))()()))()
()))()))())(
())(())))())
)()))))(((((
)(()(()())((
(()))(())())
)()()()(()()
((()())((())
)())()()(()(
()(())()())
)(()(())())
()(())(()))
))()(()(())(
()((()())))
)()(((())))
)())()())))
(((()()())))
()())()())()
()())(())()
)()((()())((
(()()((((())
(())))()()))
((...

output:

((()(())))(
()))()()))(
impossible
())(())))())
impossible
impossible
(()))(())())
impossible
impossible
impossible
()(())()())
)(()(())(
()(())(()))
impossible
()((()())))
)()((((
)())()())))
(((()()(
()())()())(
()())(())(
impossible
impossible
(())))()()))
((((())))))
impossible
())()(()()))
()()...

result:

ok 83333 cases (83333 test cases)

Test #10:

score: 0
Accepted
time: 22ms
memory: 3540kb

input:

250000
)())
()))
())(
(())
()()
())
))((
())
)()
())
()((
()))
(())
)(((
())
(())
))()
)())
(()(
)()
((((
()))
)))(
(()(
()()
))))
)())
()()
((()
)(((
()()
(())
)(()
()()
)()
)))(
())
())(
())
)))(
))()
())
)())
))((
())
()))
((()
)()(
)()
()))
())
)())
(())
(()(
()()
()))
()))
)))(
()))
)()
())
())...

output:

)())
()))
impossible
((
()(
())
impossible
())
)(
())
impossible
()))
((
impossible
())
((
))(
)())
impossible
)(
impossible
()))
impossible
impossible
()(
))))
)())
()(
impossible
impossible
()(
((
impossible
()(
)(
impossible
())
impossible
())
impossible
))(
())
)())
impossible
())
()))
impossibl...

result:

ok 250000 cases (250000 test cases)

Test #11:

score: 0
Accepted
time: 23ms
memory: 3564kb

input:

50000
(())(()(()(())))())
())()()))(())()()())
())()())((()(()))))
()(()()))(()((())))
()))))()))((()((()))
()(())())())(()))())
((()((()((())))())))
())()))()()()())))))
(((()()))))))()(())
))(()((())((((()((((
()((()()(((()()()())
(()())(()(()))())))
()()))))(()(()()()))
())()())(((()(()))))
()(((...

output:

(())(()(()(())))())
())()()))(())()()())
())()())((()(()))))
()(()()))(()(((
impossible
()(())())())(()))())
((()((()((())))(
())()))()()()())))))
(((()()))))))()((
impossible
impossible
(()())(()(()))())))
()()))))(()(()()(
())()())(((()((
()((()(()))()(())))
impossible
()))())(())((((
())(((()))()...

result:

ok 50000 cases (50000 test cases)

Test #12:

score: 0
Accepted
time: 20ms
memory: 3584kb

input:

83333
())()))))(()
(()((()(()))
((((((()())(
())(((((()))
((())((())))
)())())))))(
()(((())((()
()(((()))))
(()))((()))
)(())())((((
)()()()(())
)(()))(())))
)(((()())))
)(())(()()))
(())))(()))(
(((((()))))(
))((()()())(
(((()))())()
(((()()((())
())))))(((((
()((((()))))
)()(()))(())
(()))(()()))...

output:

impossible
impossible
impossible
impossible
((())(((
impossible
impossible
()(((()))))
(()))(((
impossible
)()()()((
)(()))(())))
)(((()(
)(())(()()))
impossible
impossible
impossible
(((()))())(
impossible
impossible
()(((((
)()(()))((
(()))(()()))
)())))()()))
impossible
(())(()()(
()(()(((
imposs...

result:

ok 83333 cases (83333 test cases)

Test #13:

score: 0
Accepted
time: 23ms
memory: 3640kb

input:

250000
(())
((((
(())
())
()()
(())
(())
())
))()
(())
))()
(())
()))
))))
(())
()))
()))
())
)()(
)))
)())
(())
(())
))()
(())
())
()))
))()
))((
)()
()()
)(((
(())
()()
)()
)(()
())
)())
((()
))()
)(((
()))
())
())
)())
()))
)())
(())
))()
))))
)())
())
))()
)()(
))))
((((
())
(())
)()
)())
())
()...

output:

((
impossible
((
())
()(
((
((
())
))(
((
))(
((
()))
))))
((
()))
()))
())
impossible
)))
)())
((
((
))(
((
())
()))
))(
impossible
)(
()(
impossible
((
()(
)(
impossible
())
)())
impossible
))(
impossible
()))
())
())
)())
()))
)())
((
))(
))))
)())
())
))(
impossible
))))
impossible
())
((
)(
)()...

result:

ok 250000 cases (250000 test cases)

Test #14:

score: 0
Accepted
time: 23ms
memory: 3592kb

input:

50000
))(())())())()(()(()
)((())((()()))(())))
()(()(()))()(()()))
((()())()())(()(()))
)()()(())())()(())()
(()(()(((())))()()))
((()))()()(()(()((()
()()()()(())(((())))
)))))((((()()()()(((
((())((()()(())))))
(((((()()()(()))))))
())())))())())()(())
((())((((((())))))))
((())()())())()()())
()...

output:

impossible
)((())((()()))(())))
()(()(()))()(()()))
((()())()())(()((
)()()(())())()(())(
(()(()(((())))()(
impossible
()()()()(())((((
impossible
((())((()()(())))))
(((((()()()((
())())))())())()((
((())(((((((
((())()())())()()())
()((((()((((
()())(((()))((()))))
impossible
))))))(())()((((
()()...

result:

ok 50000 cases (50000 test cases)

Test #15:

score: 0
Accepted
time: 20ms
memory: 3644kb

input:

83333
)))((())(()(
()((()))())
(()))()(()((
))((()()(()(
(()))()()))
)))()()(()((
)(()))()()((
())((()))))
(())))()())
)(()()))(()(
()(()()))()
()))())())((
((()(()())))
(((())()()()
()(())(()())
))())((()))
())((()()(()
))(()((())))
((((()()))))
(()((())())(
)()())(())()
))(()())()))
(()(())(()()
(...

output:

impossible
()((()))())
impossible
impossible
(()))()()))
impossible
impossible
())((()))))
(())))()())
impossible
()(()()))(
impossible
((()(()(
impossible
()(())(()(
))())(((
impossible
))(()(((
((((()(
impossible
)()())(())(
))(()())()))
impossible
())(())()(
impossible
impossible
))()(())())
()((...

result:

ok 83333 cases (83333 test cases)

Test #16:

score: -100
Time Limit Exceeded

input:

1
(()()((((())))(((((((())(()())())()(()((()(((()(()))))())((())((())((()))()())(()))()))((())()())))()())()(()((())(((()()(((())(((()))(((()())((()(((()())()())(()(()(())()()())())(((((((()((((()))()())())((((()(()())()())))((()((((()(((((()((((())()(()(()((((()()(((())())())))(())))))))))((((()(()...

output:


result: