結果

問題 No.2591 安上がりな括弧列
コンテスト
ユーザー suisen
提出日時 2023-12-19 22:01:20
言語 C++17(gcc12)
(gcc 12.4.0 + boost 1.90.0)
コンパイル:
g++-12 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,664 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 615 ms
コンパイル使用メモリ 88,908 KB
最終ジャッジ日時 2026-07-03 06:59:50
合計ジャッジ時間 1,297 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:14:5: error: lambda-expression in template-argument only available with ‘-std=c++20’ or ‘-std=gnu++20’
   14 |     [](int x, int y) { return std::min(x, y); },
      |     ^
main.cpp:15:5: error: lambda-expression in template-argument only available with ‘-std=c++20’ or ‘-std=gnu++20’
   15 |     []{ return 1 << 30; },
      |     ^
main.cpp:17:5: error: lambda-expression in template-argument only available with ‘-std=c++20’ or ‘-std=gnu++20’
   17 |     [](int f, int x) { return f + x; },
      |     ^
main.cpp:18:5: error: lambda-expression in template-argument only available with ‘-std=c++20’ or ‘-std=gnu++20’
   18 |     [](int f, int g) { return f + g; },
      |     ^
main.cpp:19:5: error: lambda-expression in template-argument only available with ‘-std=c++20’ or ‘-std=gnu++20’
   19 |     []{ return 0; }
      |     ^
main.cpp:20:1: error: template argument 2 is invalid
   20 | >;
      | ^
main.cpp:20:1: error: template argument 3 is invalid
main.cpp:20:1: error: template argument 5 is invalid
main.cpp:20:1: error: template argument 6 is invalid
main.cpp:20:1: error: template argument 7 is invalid
main.cpp: In function ‘int main()’:
main.cpp:47:10: error: ‘std::ranges’ has not been declared
   47 |     std::ranges::sort(p, {}, [&cost](int i) { return cost[i]; });
      |          ^~~~~~
main.cpp:54:5: error: ‘range_add_range_min’ was not declared in this scope
   54 |     range_add_range_min seg(init);
      |     ^~~~~~~~~~~~~~~~~~~
main.cpp:66:13: error: ‘seg’ was not declared in this scope
   66 |         if (seg.prod(r + 1, i + 1) >= 2) {
      |             ^~~

ソースコード

diff #
raw source code

#include <algorithm>
#include <cstdint>
#include <cstddef>
#include <functional>
#include <iostream>
#include <numeric>
#include <ranges>
#include <vector>

#include <atcoder/lazysegtree>

using range_add_range_min = atcoder::lazy_segtree<
    int,
    [](int x, int y) { return std::min(x, y); },
    []{ return 1 << 30; },
    int,
    [](int f, int x) { return f + x; },
    [](int f, int g) { return f + g; },
    []{ return 0; }
>;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::string s;
    std::cin >> n >> s;

    std::vector<long long> a(2 * n);
    for (auto &e : a) std::cin >> e;

    std::vector<long long> cost(2 * n);

    long long ans = 0;
    for (int i = 0; i < 2 * n; ++i) {
        if (s[i] == '(') {
            ans += a[i];
            cost[i] = -a[i];
        } else {
            cost[i] = a[i];
        }
    }

    std::vector<int> p(2 * n);
    std::iota(p.begin(), p.end(), 0);
    std::ranges::sort(p, {}, [&cost](int i) { return cost[i]; });

    std::vector<int8_t> used(2 * n, false);

    std::vector<int> init(2 * n + 1);
    for (int i = 0; i < n; ++i) init[i + 1] = init[i] + 1;
    for (int i = n; i < 2 * n; ++i) init[i + 1] = init[i] - 1;
    range_add_range_min seg(init);

    int r = n - 1;
    for (int i : p) {
        used[i] = true;
        if (i <= r) {
            ans += cost[i];
            continue;
        }
        while (r >= 0 and used[r]) --r;
        if (r < 0) break;

        if (seg.prod(r + 1, i + 1) >= 2) {
            ans += cost[i];
            seg.apply(r + 1, i + 1, -2);
            --r;
        }
    }
    std::cout << ans << std::endl;
}
0