結果

問題 No.1341 真ん中を入れ替えて門松列
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-01-15 22:35:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,459 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 125,900 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-05-05 00:37:48
合計ジャッジ時間 5,586 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
9,472 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx")

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


namespace MCF {

using Capa = int;
using Cost = Int;
constexpr int MAX_N = 1'000'000;
constexpr int MAX_M = 1'000'000;
constexpr int QUE_SIZE = 1 << (32 - __builtin_clz(MAX_N));
constexpr int BELLMAN_FORD_NUM_ITERS = 10;
constexpr int LOG_SCALING = 2;

int n, m, ptr[MAX_N], cur[MAX_N], next[MAX_M << 1], zu[MAX_M << 1];
bool on[MAX_N];
int que[QUE_SIZE], qb, qe;
Capa capa[MAX_M << 1], ex[MAX_N];
Cost cost[MAX_M << 1], pot[MAX_N], pot0[MAX_N];

void init(int n_) {
  n = n_; m = 0; memset(ptr, ~0, n * sizeof(int)); memset(ex, 0, n * sizeof(Capa));
}
void ae(int u, int v, Capa c, Cost d) {
  d *= (n + 1);
  next[m] = ptr[u]; ptr[u] = m; zu[m] = v; capa[m] = c; cost[m] = d; ++m;
  next[m] = ptr[v]; ptr[v] = m; zu[m] = u; capa[m] = 0; cost[m] = -d; ++m;
}

bool bellmanFord(Cost eps) {
  memcpy(pot0, pot, n * sizeof(Cost));
  for (int iter = 0; iter < BELLMAN_FORD_NUM_ITERS; ++iter) {
    bool upd = false;
    for (int i = 0; i < m; ++i) {
      if (capa[i] > 0) {
        const int u = zu[i ^ 1], v = zu[i];
        if (pot0[v] > pot0[u] + cost[i] + eps) {
          pot0[v] = pot0[u] + cost[i] + eps;
          upd = true;
        }
      }
    }
    if (!upd) {
      memcpy(pot, pot0, n * sizeof(Cost));
      return true;
    }
  }
  return false;
}

Cost solve() {
  Cost minCost = 0;
  for (int i = 0; i < m; i += 2) if (minCost > cost[i]) minCost = cost[i];
  Cost eps = 1;
  for (; eps < -minCost; eps <<= LOG_SCALING) {}
  memset(pot, 0, n * sizeof(Cost));
  for (; eps >>= LOG_SCALING; ) {
    if (bellmanFord(eps)) continue;
    for (int i = 0; i < m; i += 2) {
      const int u = zu[i ^ 1], v = zu[i];
      const Cost d = cost[i] + pot[u] - pot[v];
      if (capa[i] > 0 && d < 0) {
        Capa &c = capa[i]; ex[u] -= c; ex[v] += c; capa[i ^ 1] += c; c = 0;
      } else if (capa[i ^ 1] > 0 && -d < 0) {
        Capa &c = capa[i ^ 1]; ex[v] -= c; ex[u] += c; capa[i] += c; c = 0;
      }
    }
    memcpy(cur, ptr, n * sizeof(int));
    qb = qe = 0;
    for (int u = 0; u < n; ++u) if (ex[u] > 0) {
      que[qe] = u; ++qe &= QUE_SIZE - 1;
    }
    for (; qb != qe; ) {
      const int u = que[qb]; ++qb &= QUE_SIZE - 1;
      for (int &i = cur[u]; ~i; i = next[i]) {
        if (capa[i] > 0) {
          const int v = zu[i];
          if (cost[i] + pot[u] - pot[v] < 0) {
            const Capa c = min(ex[u], capa[i]);
            if (ex[v] <= 0 && ex[v] + c > 0) {
              que[qe] = v; ++qe &= QUE_SIZE - 1;
            }
            ex[u] -= c; ex[v] += c; capa[i] -= c; capa[i ^ 1] += c;
            if (ex[u] == 0) break;
          }
        }
      }
      if (ex[u] > 0) {
        bool relabeled = false;
        for (int i = ptr[u]; ~i; i = next[i]) {
          if (capa[i] > 0) {
            const Cost p = pot[zu[i]] - cost[i] - eps;
            if (!relabeled || pot[u] < p) {
              relabeled = true; pot[u] = p;
            }
          }
        }
        cur[u] = ptr[u]; que[qe] = u; ++qe &= QUE_SIZE - 1;
      }
    }
  }
  Cost totalCost = 0;
  for (int i = 0; i < m; i += 2) totalCost += cost[i] * capa[i ^ 1];
  return totalCost / (n + 1);
}

}  // namespace MCF


constexpr Int BIG = 1'000'000'000'000'000LL;


int N;
Int M;
vector<Int> A, B, C;

int main() {
  for (; ~scanf("%d%lld", &N, &M); ) {
    A.resize(N);
    B.resize(N);
    C.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%lld%lld%lld", &A[i], &B[i], &C[i]);
    }
    
    for (int i = 0; i < N; ++i) {
      if (A[i] > C[i]) {
        swap(A[i], C[i]);
      }
    }
    
    vector<Int> bs = B;
    sort(bs.begin(), bs.end());
    bs.erase(unique(bs.begin(), bs.end()), bs.end());
    const int bsLen = bs.size();
// cerr<<"bs = ";pv(bs.begin(),bs.end());
    
    bool bad = false;
    for (const Int b : bs) {
      bool found = false;
      for (int i = 0; i < N; ++i) {
        if (b < A[i] || C[i] < b) {
          found = true;
          break;
        }
      }
      if (!found) {
        bad = true;
        break;
      }
    }
    if (bad) {
      puts("NO");
      continue;
    }
    
    MCF::init(2 + N + N + bsLen + bsLen);
    for (int i = 0; i < N; ++i) {
      MCF::ae(0, 2 + i, 1, 0);
    }
    for (int i = 0; i < N; ++i) {
      // b < A[i]
      {
        const int j = lower_bound(bs.begin(), bs.end(), A[i]) - bs.begin() - 1;
// cerr<<i<<": <= "<<j<<endl;
        if (j >= 0) {
          MCF::ae(2 + i, 2 + N + N + j, 1, -C[i]);
        }
      }
      // C[i] < b
      {
        const int j = upper_bound(bs.begin(), bs.end(), C[i]) - bs.begin();
// cerr<<i<<": >= "<<j<<endl;
        if (j < bsLen) {
          MCF::ae(2 + i, 2 + N + N + bsLen + j, 1, 0);
        }
      }
    }
    for (int j = 1; j < bsLen; ++j) {
      MCF::ae(2 + N + N + j, 2 + N + N + (j - 1), N, 0);
    }
    for (int j = 0; j < bsLen - 1; ++j) {
      MCF::ae(2 + N + N + bsLen + j, 2 + N + N + bsLen + (j + 1), N, 0);
    }
    for (int i = 0; i < N; ++i) {
      const int j = lower_bound(bs.begin(), bs.end(), B[i]) - bs.begin();
      MCF::ae(2 + N + N + j, 2 + N + i, 1, 0);
      MCF::ae(2 + N + N + bsLen + j, 2 + N + i, 1, -B[i]);
    }
    for (int i = 0; i < N; ++i) {
      MCF::ae(2 + N + i, 1, 1, 0);
    }
    MCF::ae(1, 0, N, -BIG);
    const Int res = MCF::solve();
    const Int score = -(res - N * (-BIG));
// cerr<<"score = "<<score<<endl;
    if (score >= 0) {
      puts("YES");
      puts((score >= M) ? "KADOMATSU!" : "NO");
    } else {
      puts("NO");
    }
  }
  return 0;
}
0