QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#217817 | #5108. Prehistoric Programs | drldrls | WA | 16ms | 6060kb | C++17 | 4.5kb | 2023-10-17 14:40:59 | 2023-10-17 14:40:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
bool compare_neg_score(const vector<int>& a, const vector<int>& b){
if(a[2] > b[2]) return true;
else if (a[2] == b[2] && a[1] > b[1]) return true;
return false;
}
bool ncompare_neg_score(const vector<int>& a, const vector<int>& b){
if(a[2] > b[2]) return true;
else if (a[2] == b[2] && a[1] < b[1]) return true;
return false;
}
int main(){
// To get input n
int count{0};
cin >> count;
// Process of solving this problem:
// 1. First, in order to make a properly nested structure, the counts of '(' and ')' has to be the same.
// Let the total '(' - ')' be total_score, and each string also has its score.
// 2. In the final sequence, we will always have all strings with positive scores in the front,
// and those with negative scores in the back, so there wont be strings like "))((" happens.
// 3. But in positive strings, there could be a substring of negative scores. E.g. ")))((((((".
// Despite it has positive score, it cannot be paired with some positive strings like "(" in the front.
// Thus, we need to calculate another score "neg_score" to indicate the substring (from the start of the string) with smallest negative score.
// 4. If all positive strings has neg_score < 0, then it's "impossible", since it must starts with ')'.
// 5. Sort all positive strings by their neg_score (decreasing).
// This can guaruntee we find the possible answer without choose something like "))((" as the first string and return "impossible".
// Check each time if the total_score - new neg_score is >= 0. If not, return "impossible".
// 6. After checking all the positive strings, check the negative strings for the sequence.
// Reverse all negative string and check like positive strings. It's like checking the string from behind.
// Reverse means flip all ( & ) and reverse the order.
// When they meet in the middle, we can check if total_score = 0.
// 7. If it's a valid nested structure (total_score = 0), print out the sequence. Else print "impossible".
// *strings with score = 0 are stored to positive strings.
vector<vector<int>> pos_strings(0); // save strings with positive score
vector<vector<int>> neg_strings(0); // save strings with negative score
bool wrong = false; // save whether it's impossible
// Getting input strings
for(int i = 0; i < count; i++){
string s;
int score{0}, neg_score{0}, temp_neg{0}, pos_score{0}, temp_pos{0};
cin >> s;
// calculate score & neg_score. Fkip the negative strings
score = 2 * std::count(s.begin(), s.end(), '(') - s.length();
if(score <= 0){
for(char c: s){
if(c == '(') c = ')';
else c = '(';
}
reverse(s.begin(), s.end());
}
for(char c: s){
if(c == '('){
temp_neg++;
score++;
}else{
temp_neg--;
score--;
}
neg_score = temp_neg < neg_score ? temp_neg : neg_score;
}
// save the strings to corresponding vectors
vector<int> v = {i + 1, score, neg_score};
if(score > 0) pos_strings.emplace_back(v);
else neg_strings.emplace_back(v);
}
// sort positive strings by neg_score (function defined above)
sort(pos_strings.begin(), pos_strings.end(), compare_neg_score);
sort(neg_strings.begin(), neg_strings.end(), ncompare_neg_score);
// check each round that if the score went below 0. If so, print "impossible" and return
int total_pos_score{0}; // save the current score
for(auto s: pos_strings){
total_pos_score += s[1];
if(total_pos_score + s[2] < 0){
cout << "impossible";
return 0;
}
}
// check negatve strings
int total_neg_score{0}; // save the current score
for(auto s: neg_strings){
total_neg_score -= s[1];
if(total_neg_score + s[2] < 0){
cout << "impossible";
return 0;
}
}
if(total_pos_score != total_neg_score) cout << "impossible";
else{
// print results after passing all the checks, print neg strings in reverse order
for(auto s: pos_strings) cout << s[0] << " ";
for(auto it = neg_strings.rbegin(); it != neg_strings.rend(); it++) cout << (*it)[0] << " ";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 16ms
memory: 6060kb
input:
50000 ( ( ))))()))()(()))()()()))()(((((()(((()))()(((()))((()(())))))(()( ) ( ) ((( ( ( ( ( ( () ( ) ( )((())()(( )))))( ( ) )) )() ( ) ) )()( ( ( () ( ) ( )()((((())()))())( ( ( )( ( ( (()())()) ) ) ( ( ( )((())))((())))))))))((((()()))()))))))))((()())())) ) )() ) ) ) ) ) ())(())))))()(()((()(())...
output:
41248 4238 13809 5338 27609 2458 48374 389 6754 48749 42979 18533 6986 14096 31692 5803 169 3405 32583 23456 38930 9225 10427 34539 26695 26677 43508 41740 11398 35132 21764 46194 30771 25061 39743 2253 24373 46429 31402 34110 5699 23312 35039 33777 45491 6381 41639 23551 39389 9790 28794 7365 6701 ...
result:
ok good plan
Test #2:
score: 0
Accepted
time: 2ms
memory: 3916kb
input:
1000 ( ))(())) ((((())())))((())(()))( )( ) ))) ))((()(((((((())()(())()())))(()(())()())))))))((()((()())()())(())))()((()()) )((()()()(())(()))()(())()))(()))))())))))))))))))()))(()()(())(()))())()()))))(())()()()((())(()))(())))))))(()()())()))()())))()()))))))( )))((( ( )))()()()))) ( (((())(((...
output:
36 13 66 386 966 585 286 257 127 83 39 595 476 329 907 598 214 814 62 981 427 131 662 384 707 807 793 511 271 638 869 449 80 379 767 632 387 474 746 422 20 327 239 502 975 171 168 989 553 715 535 345 473 31 296 919 915 88 334 324 177 640 604 258 945 575 563 667 738 566 731 725 208 929 17 33 60 686 9...
result:
ok good plan
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3548kb
input:
2 () ()
output:
impossible
result:
wrong answer you didn't find a solution but jury did