結果
問題 |
No.3306 Life is Easy?
|
ユーザー |
|
提出日時 | 2025-10-05 15:35:29 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 273 ms / 2,000 ms |
コード長 | 13,024 bytes |
コンパイル時間 | 2,962 ms |
コンパイル使用メモリ | 298,428 KB |
実行使用メモリ | 7,716 KB |
最終ジャッジ日時 | 2025-10-05 15:35:36 |
合計ジャッジ時間 | 5,598 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 35 |
ソースコード
// Problem: No.3306 Life is Easy? // Contest: yukicoder // URL: https://yukicoder.me/problems/no/3306 // Memory Limit: 512 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #line 2 "/root/AtCoder/Halc-Library/Template/Template.hpp" #include <bits/stdc++.h> using namespace std; #line 8 "/root/AtCoder/Halc-Library/Template/InOut.hpp" inline void scan() {} inline void scan(int32_t &a) { std::cin >> a; } inline void scan(uint32_t &a) { std::cin >> a; } inline void scan(int64_t &a) { std::cin >> a; } inline void scan(uint64_t &a) { std::cin >> a; } inline void scan(char &a) { std::cin >> a; } inline void scan(float &a) { std::cin >> a; } inline void scan(double &a) { std::cin >> a; } inline void scan(long double &a) { std::cin >> a; } inline void scan(std::vector<bool> &vec) { for (int32_t i = 0; i < vec.size(); i++) { int a; scan(a); vec[i] = a; } } inline void scan(std::string &a) { std::cin >> a; } template <class T> inline void scan(std::vector<T> &vec); template <class T, size_t size> inline void scan(std::array<T, size> &vec); template <class T, class L> inline void scan(std::pair<T, L> &p); template <class T, size_t size> inline void scan(T (&vec)[size]); template <class T> inline void scan(std::vector<T> &vec) { for (auto &i : vec) scan(i); } template <class T> inline void scan(std::deque<T> &vec) { for (auto &i : vec) scan(i); } template <class T, size_t size> inline void scan(std::array<T, size> &vec) { for (auto &i : vec) scan(i); } template <class T, class L> inline void scan(std::pair<T, L> &p) { scan(p.first); scan(p.second); } template <class T, size_t size> inline void scan(T (&vec)[size]) { for (auto &i : vec) scan(i); } template <class T> inline void scan(T &a) { std::cin >> a; } inline void in() {} template <class Head, class... Tail> inline void in(Head &head, Tail &...tail) { scan(head); in(tail...); } inline void print() { std::cout << ' '; } inline void print(const bool &a) { std::cout << a; } inline void print(const int32_t &a) { std::cout << a; } inline void print(const uint32_t &a) { std::cout << a; } inline void print(const int64_t &a) { std::cout << a; } inline void print(const uint64_t &a) { std::cout << a; } inline void print(const char &a) { std::cout << a; } inline void print(const char a[]) { std::cout << a; } inline void print(const float &a) { std::cout << a; } inline void print(const double &a) { std::cout << a; } inline void print(const long double &a) { std::cout << a; } inline void print(const std::string &a) { for (auto &&i : a) print(i); } template <class T> inline void print(const std::vector<T> &vec); template <class T, size_t size> inline void print(const std::array<T, size> &vec); template <class T, class L> inline void print(const std::pair<T, L> &p); template <class T, size_t size> inline void print(const T (&vec)[size]); template <class T> inline void print(const std::vector<T> &vec) { if (vec.empty()) return; print(vec[0]); for (auto i = vec.begin(); ++i != vec.end();) { std::cout << ' '; print(*i); } } template <class T> inline void print(const std::deque<T> &vec) { if (vec.empty()) return; print(vec[0]); for (auto i = vec.begin(); ++i != vec.end();) { std::cout << ' '; print(*i); } } template <class T, size_t size> inline void print(const std::array<T, size> &vec) { print(vec[0]); for (auto i = vec.begin(); ++i != vec.end();) { std::cout << ' '; print(*i); } } template <class T, class L> inline void print(const std::pair<T, L> &p) { print(p.first); std::cout << ' '; print(p.second); } template <class T, size_t size> inline void print(const T (&vec)[size]) { print(vec[0]); for (auto i = vec; ++i != end(vec);) { std::cout << ' '; print(*i); } } template <class T> inline void print(const T &a) { std::cout << a; } inline void out() { std::cout << '\n'; } template <class T> inline void out(const T &t) { print(t); std::cout << '\n'; } template <class Head, class... Tail> inline void out(const Head &head, const Tail &...tail) { print(head); std::cout << ' '; out(tail...); } inline void Yes(bool i = true) { out(i ? "Yes" : "No"); } inline void No(bool i = true) { out(i ? "No" : "Yes"); } inline void Takahashi(bool i = true) { out(i ? "Takahashi" : "Aoki"); } inline void Aoki(bool i = true) { out(i ? "Aoki" : "Takahashi"); } inline void Alice(bool i = true) { out(i ? "Alice" : "Bob"); } inline void Bob(bool i = true) { out(i ? "Bob" : "Alice"); } inline void First(bool i = true) { out(i ? "First" : "Second"); } inline void Second(bool i = true) { out(i ? "Second" : "First"); } inline void Possible(bool i = true) { out(i ? "Possible" : "Impossible"); } inline void Impossible(bool i = true) { out(i ? "Impossible" : "Possible"); } inline void fls() { std::flush(std::cout); } struct IOsetup { IOsetup() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(16); } } iosetup; #line 9 "/root/AtCoder/Halc-Library/Template/Util.hpp" using ll = int64_t; using ld = long double; using ull = uint64_t; using uint = uint32_t; using pll = std::pair<ll, ll>; using pii = std::pair<int32_t, int32_t>; using vl = std::vector<ll>; using vvl = std::vector<std::vector<ll>>; using pdd = std::pair<ld, ld>; using tuplis = std::array<ll, 3>; template <class T> using pq = std::priority_queue<T, std::vector<T>, std::greater<T>>; constexpr ll LINF = (1LL << 62) - (1LL << 31); constexpr int32_t INF = INT_MAX >> 1; constexpr ll MINF = 1LL << 40; constexpr ld DINF = std::numeric_limits<ld>::infinity(); constexpr int32_t MODD = 1000000007; constexpr int32_t MOD = 998244353; constexpr ld EPS = 1e-9; constexpr ld PI = 3.1415926535897932; const ll four[] = {0, 1, 0, -1, 0}; const ll eight[] = {0, 1, 1, 0, -1, -1, 1, -1, 0}; template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } else return false; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } else return false; } template <class T> ll sum(const T &a) { return accumulate(std::begin(a), std::end(a), 0LL); } template <class T> ld dsum(const T &a) { return accumulate(std::begin(a), std::end(a), 0.0L); } template <class T> auto min(const T &a) { return *min_element(std::begin(a), std::end(a)); } template <class T> auto max(const T &a) { return *max_element(std::begin(a), std::end(a)); } #line 1 "/root/AtCoder/Halc-Library/Template/Macro.hpp" #define _overload3(_1, _2, _3, name, ...) name #define _overload4(_1, _2, _3, _4, name, ...) name #define _rep1(i, n) for (int64_t i = 0; i < (n); i++) #define _rep2(i, a, b) for (int64_t i = (a); i < (b); i++) #define _rep3(i, a, b, c) for (int64_t i = (a); i < (b); i += (c)) #define rep(...) _overload4(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__) #define _rrep1(i, n) for (int64_t i = (n) - 1; i >= 0; i--) #define _rrep2(i, a, b) for (int64_t i = (b) - 1; i >= (a); i--) #define rrep(...) _overload3(__VA_ARGS__, _rrep2, _rrep1)(__VA_ARGS__) #define each(i, ...) for (auto&& i : __VA_ARGS__) #define all(i) std::begin(i), std::end(i) #define rall(i) std::rbegin(i), std::rend(i) #define len(x) ((int64_t)(x).size()) #define fi first #define se second #define uniq(x) x.erase(unique(all(x)), std::end(x)) #define vec(type, name, ...) vector<type> name(__VA_ARGS__); #define vv(type, name, h, ...) std::vector<std::vector<type>> name(h, std::vector<type>(__VA_ARGS__)); #define INT(...) int32_t __VA_ARGS__; in(__VA_ARGS__) #define LL(...) int64_t __VA_ARGS__; in(__VA_ARGS__) #define ULL(...) uint64_t __VA_ARGS__; in(__VA_ARGS__) #define STR(...) std::string __VA_ARGS__; in(__VA_ARGS__) #define CHR(...) char __VA_ARGS__; in(__VA_ARGS__) #define LD(...) long double __VA_ARGS__; in(__VA_ARGS__) #define VEC(type, name, size) std::vector<type> name(size); in(name) #define VV(type, name, h, w) std::vector<std::vector<type>> name(h, std::vector<type>(w)); in(name) #line 2 "main.cpp" #line 1 "math/matrix/matrix.hpp" template <class T> struct Matrix { vector<vector<T> > A; Matrix() {} Matrix(size_t n, size_t m) : A(n, vector<T>(m, 0)) {} Matrix(size_t n) : A(n, vector<T>(n, 0)) {}; size_t size() const { if (A.empty()) return 0; assert(A.size() == A[0].size()); return A.size(); } size_t height() const { return (A.size()); } size_t width() const { return (A[0].size()); } inline const vector<T>& operator[](int k) const { return (A.at(k)); } inline vector<T>& operator[](int k) { return (A.at(k)); } static Matrix I(size_t n) { Matrix mat(n); for (int i = 0; i < n; i++) mat[i][i] = 1; return (mat); } Matrix& operator+=(const Matrix& B) { size_t n = height(), m = width(); assert(n == B.height() && m == B.width()); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) (*this)[i][j] += B[i][j]; return (*this); } Matrix& operator-=(const Matrix& B) { size_t n = height(), m = width(); assert(n == B.height() && m == B.width()); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) (*this)[i][j] -= B[i][j]; return (*this); } Matrix& operator*=(const Matrix& B) { size_t n = height(), m = B.width(), p = width(); assert(p == B.height()); vector<vector<T> > C(n, vector<T>(m, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) for (int k = 0; k < p; k++) C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]); A.swap(C); return (*this); } Matrix& operator^=(long long k) { Matrix B = Matrix::I(height()); while (k > 0) { if (k & 1) B *= *this; *this *= *this; k >>= 1LL; } A.swap(B.A); return (*this); } Matrix operator+(const Matrix& B) const { return (Matrix(*this) += B); } Matrix operator-(const Matrix& B) const { return (Matrix(*this) -= B); } Matrix operator*(const Matrix& B) const { return (Matrix(*this) *= B); } Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); } friend ostream& operator<<(ostream& os, Matrix& p) { size_t n = p.height(), m = p.width(); for (int i = 0; i < n; i++) { os << "["; for (int j = 0; j < m; j++) { os << p[i][j] << (j + 1 == m ? "]\n" : ","); } } return (os); } T determinant() { Matrix B(*this); assert(width() == height()); T ret = 1; for (int i = 0; i < width(); i++) { int idx = -1; for (int j = i; j < width(); j++) { if (B[j][i] != 0) idx = j; } if (idx == -1) return (0); if (i != idx) { ret *= -1; swap(B[i], B[idx]); } ret *= B[i][i]; T vv = B[i][i]; for (int j = 0; j < width(); j++) { B[i][j] /= vv; } for (int j = i + 1; j < width(); j++) { T a = B[j][i]; for (int k = 0; k < width(); k++) { B[j][k] -= B[i][k] * a; } } } return (ret); } }; #line 2 "graph/flow/hungarian.hpp" /** * @brief Hungarian(二部グラフの最小重み最大マッチング) * */ template <typename T> pair<T, vector<int> > hungarian(Matrix<T>& A) { const T infty = numeric_limits<T>::max(); const int N = (int)A.height(); const int M = (int)A.width(); vector<int> P(M), way(M); vector<T> U(N, 0), V(M, 0), minV; vector<bool> used; for (int i = 1; i < N; i++) { P[0] = i; minV.assign(M, infty); used.assign(M, false); int j0 = 0; while (P[j0] != 0) { int i0 = P[j0], j1 = 0; used[j0] = true; T delta = infty; for (int j = 1; j < M; j++) { if (used[j]) continue; T curr = A[i0][j] - U[i0] - V[j]; if (curr < minV[j]) minV[j] = curr, way[j] = j0; if (minV[j] < delta) delta = minV[j], j1 = j; } for (int j = 0; j < M; j++) { if (used[j]) U[P[j]] += delta, V[j] -= delta; else minV[j] -= delta; } j0 = j1; } do { P[j0] = P[way[j0]]; j0 = way[j0]; } while (j0 != 0); } return {-V[0], P}; } void solve() { LL(N,M); VV(ll,A,N,M); ll day=N/2; if(M==1){ ll ans=0; rep(i,day){ ans+=A[N-i-1][0]-A[i][0]; } out(ans); return; } if(M==2){ vl s1,s2; ll ans=0; rep(i,day){ ans+=A[N-i-1][0]-A[i][0]; s1.push_back(A[N-i-1][1]-A[N-i-1][0]); s2.push_back(A[i][0]-A[i][1]); } sort(all(s1)); sort(all(s2)); ll fin=ans; rrep(i,day){ ans+=s1[i]+s2[i]; chmax(fin,ans); } out(fin); return; } Matrix<ll> mat(day+1,day+1); rep(i,day){ rep(j,day){ ll mx=0; rep(k,M){ chmax(mx,A[N-j-1][k]-A[i][k]); } mat[i+1][j+1]=-mx; } } out(-hungarian(mat).fi); } int main() { solve(); }