結果
| 問題 | No.684 Prefix Parenthesis | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2025-10-24 00:40:16 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,065 bytes | 
| コンパイル時間 | 924 ms | 
| コンパイル使用メモリ | 74,708 KB | 
| 実行使用メモリ | 7,724 KB | 
| 最終ジャッジ日時 | 2025-10-24 00:40:19 | 
| 合計ジャッジ時間 | 2,653 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | WA * 31 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |     freopen("parentheses.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:17:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |     freopen("parentheses.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:19:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
            
            ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, l, r, ans;
string s;
struct Node {
    int L, R;
};
vector<Node> nodes;
int main() {
    freopen("parentheses.in", "r", stdin);
    freopen("parentheses.out", "w", stdout);
    scanf("%d", &n);
    cin >> s;
    for (int i = 0, match = 0; i < s.size(); ++i) {
        if (s[i] == '(') {
            ++l;
        } else {
            if (l > 0) {
                ++match;
                --l;
            } else {
                ++r;
            }
        }
        ans += match * 2;
        nodes.push_back({ l, r });
    }
    sort(nodes.begin(), nodes.end(), [](Node n1, Node n2) {
        bool n1_type = n1.R <= n1.L, n2_type = n2.R <= n2.L;
        if (n1_type != n2_type) return n1_type;
        if (n1.R <= n1.L) return n1.R < n2.R;
        return n1.L > n2.L;
    });
    l = 0LL;
    for (auto node : nodes) {
        int match = min(l, node.R);
        l -= match;
        l += node.L;
        ans += match * 2;
    }
    printf("%d\n", ans);
    return 0;
}
            
            
            
        