結果

問題 No.1341 真ん中を入れ替えて門松列
ユーザー 👑 hitonanodehitonanode
提出日時 2021-01-15 23:12:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 3,103 bytes
コンパイル時間 2,164 ms
コンパイル使用メモリ 217,144 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-05 01:05:34
合計ジャッジ時間 3,087 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 29 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 32 ms
6,940 KB
testcase_11 AC 31 ms
6,944 KB
testcase_12 AC 30 ms
6,944 KB
testcase_13 AC 33 ms
6,944 KB
testcase_14 AC 32 ms
6,940 KB
testcase_15 AC 34 ms
6,940 KB
testcase_16 AC 34 ms
6,940 KB
testcase_17 AC 34 ms
6,940 KB
testcase_18 AC 30 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl
#else
#define dbg(x) (x)
#endif


void bad()
{
    puts("NO");
    exit(0);
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N;
    lint M;
    cin >> N >> M;
    vector<lint> B(N);
    vector<pint> AC;
    REP(i, N) {
        int a, c;
        cin >> a >> B[i] >> c;
        if (a > c) swap(a, c);
        AC.emplace_back(a, c);
    }

    sort(AC.begin(), AC.end(), [](const auto &l, const auto &r) { return l.second < r.second; });

    sort(B.begin(), B.end());

    const auto zaatsu = sort_unique(B);

    vector<int> sh(N);
    REP(i, N) sh[i] = lower_bound(zaatsu.begin(), zaatsu.end(), AC[i].first) - zaatsu.begin();

    lint ret = -1;

    vector<int> adds;
    vector<int> cnt(zaatsu.size() + 1);
    vector<int> used(N);

    lint bupsum = accumulate(B.begin(), B.end(), 0LL);

    auto try_to_solve = [&]() {
        lint tmp = 0;
        for (auto i : adds) tmp += AC[i].second;
        vector<int> cs;
        REP(i, N) if (!used[i]) cs.emplace_back(AC[i].second);
        REP(i, cs.size()) if (B[i + adds.size()] <= cs[i]) return;
        tmp += bupsum;
        chmax(ret, tmp);
    };

    REP(i, N) {
        try_to_solve();

        auto t = lower_bound(zaatsu.begin(), zaatsu.end(), B[i]) - zaatsu.begin();
        REP(j, t + 1) cnt[j]++;

        int h = 0;
        while (cnt[h] > 0) h++;

        int amax = -1;
        REP(i, N) if (!used[i] and sh[i] >= h) {
            if (amax < 0 or AC[amax].second < AC[i].second) { amax = i; }
        }
        if (amax < 0) break;
        used[amax] = 1;
        REP(j, sh[amax]) cnt[j]--;
        bupsum -= B[i];
        adds.emplace_back(amax);
    }

    try_to_solve();

    if (ret < 0) bad();
    puts("YES");
    if (ret < M) bad();
    puts("KADOMATSU!");
}
0