Submission #72756850


Source Code Expand

// #include "rcpl/my_template.hpp"
// #include "rcpl/dp/inversion_number.hpp"
// using namespace std;
// 
// void solve() {
//     // TODO: Implement
//     INT(N, L);
//     vector<i64> C(N + 1);
//     vector P(N + 1, vector<int>(L));
//     const int inv_max = 100;
//     REP(i, N) {
//         in(C[i + 1], P[i + 1]);
//         REP(j, L) P[i + 1][j]--;
//     }
//     REP(j, L) P[0][j] = j;
//     show(C);
//     show(P);
//     if (L == 1) {
//         print(SUM(C));
//         return;
//     }
//     vector<i64> dp(N + 1, -INF<i64>);
//     dp[0] = 0;
//     auto f = [&](vector<int>& a, vector<int>& b) -> int {
//         vector<int> ainv(L);
//         REP(i, L) ainv[a[i]] = i;
//         vector<int> c(L);
//         REP(i, L) c[i] = ainv[b[i]];
//         return inversion_number<int>(c);
//     };
//     REP(i, N) {
//         // calc i + 1
//         show(i);
//         REP(k, 1, inv_max + 1) {
//             if (i + k > N) continue;
//             // calc inv number P[i] and P[i + k]
//             int cost = f(P[i], P[i + k]);
//             if (cost <= k) {
//                 chmax(dp[i + k], dp[i] + C[i + k]);
//             }
//         }
//     }
//     show(dp);
//     i64 ans = MAX(dp);
//     print(ans);
//     return;
// }
// 
// int main() {
//     int T = 1;
//     // INT(T);
//     REP(T) solve();
//     return 0;
// }
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#ifdef RUTHEN_LOCAL
#include <rcpl/debug.hpp>
#else
#define show(x) true
#endif

// type definition
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using f32 = float;
using f64 = double;
using f128 = long double;
template <class T> using pque = std::priority_queue<T>;
template <class T> using pqueg = std::priority_queue<T, std::vector<T>, std::greater<T>>;
// overload
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define overload2(_1, _2, name, ...) name
// for loop
#define REP1(a) for (long long _ = 0; _ < (a); _++)
#define REP2(i, a) for (long long i = 0; i < (a); i++)
#define REP3(i, a, b) for (long long i = (a); i < (b); i++)
#define REP4(i, a, b, c) for (long long i = (a); i < (b); i += (c))
#define REP(...) overload4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define RREP1(a) for (long long _ = (a) - 1; _ >= 0; _--)
#define RREP2(i, a) for (long long i = (a) - 1; i >= 0; i--)
#define RREP3(i, a, b) for (long long i = (b) - 1; i >= (a); i--)
#define RREP(...) overload3(__VA_ARGS__, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define FORE1(x, a) for (auto&& x : a)
#define FORE2(x, y, a) for (auto&& [x, y] : a)
#define FORE3(x, y, z, a) for (auto&& [x, y, z] : a)
#define FORE(...) overload4(__VA_ARGS__, FORE3, FORE2, FORE1)(__VA_ARGS__)
#define FORSUB(t, s) for (long long t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
// function
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(a) std::sort((a).begin(), (a).end())
#define RSORT(a) std::sort((a).rbegin(), (a).rend())
#define REV(a) std::reverse((a).begin(), (a).end())
#define UNIQUE(a)                      \
    std::sort((a).begin(), (a).end()); \
    (a).erase(std::unique((a).begin(), (a).end()), (a).end())
#define LEN(a) (int)((a).size())
#define MIN(a) *std::min_element((a).begin(), (a).end())
#define MAX(a) *std::max_element((a).begin(), (a).end())
#define SUM1(a) std::accumulate((a).begin(), (a).end(), 0LL)
#define SUM2(a, x) std::accumulate((a).begin(), (a).end(), (x))
#define SUM(...) overload2(__VA_ARGS__, SUM2, SUM1)(__VA_ARGS__)
#define LB(a, x) std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x)))
#define UB(a, x) std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x)))
template <class T, class U> inline bool chmin(T& a, const U& b) { return (a > T(b) ? a = b, 1 : 0); }
template <class T, class U> inline bool chmax(T& a, const U& b) { return (a < T(b) ? a = b, 1 : 0); }
template <class T, class S> inline T floor(const T x, const S y) {
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
template <class T, class S> inline T ceil(const T x, const S y) {
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}
template <class T, class S> std::pair<T, T> inline divmod(const T x, const S y) {
    T q = floor(x, y);
    return {q, x - q * y};
}
// 10 ^ n
constexpr long long TEN(int n) { return (n == 0) ? 1 : 10LL * TEN(n - 1); }
// 1 + 2 + ... + n
#define TRI1(n) ((n) * ((n) + 1LL) / 2)
// l + (l + 1) + ... + r
#define TRI2(l, r) (((l) + (r)) * ((r) - (l) + 1LL) / 2)
#define TRI(...) overload2(__VA_ARGS__, TRI2, TRI1)(__VA_ARGS__)
// bit operation
// bit[i] (= 0 or 1)
#define IBIT(bit, i) (((bit) >> (i)) & 1)
// (0, 1, 2, 3, 4) -> (0, 1, 3, 7, 15)
#define MASK(n) ((1LL << (n)) - 1)
#define POW2(n) (1LL << (n))

#if __cplusplus >= 202002L
#include <bit>
#endif

// countr_zero
// (000, 001, 010, 011, 100) -> (32, 0, 1, 0, 2)
#if __cplusplus >= 202002L
using std::countr_zero;
#else
int countr_zero(unsigned int x) { return x == 0 ? 32 : __builtin_ctz(x); }
int countr_zero(unsigned long long int x) { return x == 0 ? 64 : __builtin_ctzll(x); }
#endif
int countr_zero(int x) { return countr_zero((unsigned int)(x)); }
int countr_zero(long long int x) { return countr_zero((unsigned long long int)(x)); }

// lowbit
// (000, 001, 010, 011, 100) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(unsigned int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(long long int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(unsigned long long int x) { return (x == 0 ? -1 : countr_zero(x)); }

#if __cplusplus >= 202002L
#include <bit>
#endif

// popcount
// (000, 001, 010, 011, 100) -> (0, 1, 1, 2, 1)
#if __cplusplus >= 202002L
using std::popcount;
#else
int popcount(unsigned int x) { return __builtin_popcount(x); }
int popcount(unsigned long long int x) { return __builtin_popcountll(x); }
#endif
int popcount(int x) { return popcount((unsigned int)(x)); }
int popcount(long long int x) { return popcount((unsigned long long int)(x)); }

#if __cplusplus >= 202002L
#include <bit>
#endif

// countl_zero
// (000, 001, 010, 011, 100) -> (32, 31, 30, 30, 29)
#if __cplusplus >= 202002L
using std::countl_zero;
#else
int countl_zero(unsigned int x) { return x == 0 ? 32 : __builtin_clz(x); }
int countl_zero(unsigned long long int x) { return x == 0 ? 64 : __builtin_clzll(x); }
#endif
int countl_zero(int x) { return countl_zero((unsigned int)(x)); }
int countl_zero(long long int x) { return countl_zero((unsigned long long int)(x)); }

// topbit
// (000, 001, 010, 011, 100) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return 31 - countl_zero(x); }
int topbit(unsigned int x) { return 31 - countl_zero(x); }
int topbit(long long int x) { return 63 - countl_zero(x); }
int topbit(unsigned long long int x) { return 63 - countl_zero(x); }
// binary search (integer)
template <class T, class F> T bin_search(T ok, T ng, F& f) {
    // assert(f(ok) and !f(ng));
    while ((ok > ng ? ok - ng : ng - ok) > 1) {
        T md = (ng + ok) >> 1;
        (f(md) ? ok : ng) = md;
    }
    return ok;
}
// binary search (real number)
template <class T, class F> T bin_search_real(T ok, T ng, F& f, const int iter = 100) {
    // assert(f(ok) and !f(ng));
    for (int _ = 0; _ < iter; _++) {
        T md = (ng + ok) / 2;
        (f(md) ? ok : ng) = md;
    }
    return ok;
}
// floor(sqrt(x))
template <class T> constexpr T sqrt_floor(T x) { return T(sqrtl(x)); }
// check if [l1, r1) and [l2, r2) intersect
template <class T> constexpr bool intersect(const T l1, const T r1, const T l2, const T r2) { return std::max(l1, l2) < std::min(r1, r2); }
// check if [a.first, a.second) and [b.first, b.second) intersect
template <class T> constexpr bool intersect(const std::pair<T, T>& a, const std::pair<T, T>& b) { return intersect(a.first, a.second, b.first, b.second); }
// rotate matrix counterclockwise by pi / 2
template <class T> void rot(std::vector<std::vector<T>>& a) {
    if ((int)(a.size()) == 0) return;
    if ((int)(a[0].size()) == 0) return;
    int n = (int)(a.size()), m = (int)(a[0].size());
    std::vector res(m, std::vector<T>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            res[m - 1 - j][i] = a[i][j];
        }
    }
    a.swap(res);
}
// const value
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// infinity
template <class T> constexpr T INF = 0;
template <> constexpr int INF<int> = 1'000'000'000;                 // 1e9
template <> constexpr i64 INF<i64> = i64(INF<int>) * INF<int> * 2;  // 2e18
template <> constexpr u32 INF<u32> = INF<int>;                      // 1e9
template <> constexpr u64 INF<u64> = INF<i64>;                      // 2e18
template <> constexpr f32 INF<f32> = INF<i64>;                      // 2e18
template <> constexpr f64 INF<f64> = INF<i64>;                      // 2e18
template <> constexpr f128 INF<f128> = INF<i64>;                    // 2e18
// I/O
// input
template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (auto&& i : v) is >> i;
    return is;
}
template <class... T> void in(T&... a) { (std::cin >> ... >> a); }
void scan() {}
template <class Head, class... Tail> void scan(Head& head, Tail&... tail) {
    in(head);
    scan(tail...);
}
// input macro
#define INT(...)     \
    int __VA_ARGS__; \
    scan(__VA_ARGS__)
#define I64(...)     \
    i64 __VA_ARGS__; \
    scan(__VA_ARGS__)
#define U32(...)     \
    u32 __VA_ARGS__; \
    scan(__VA_ARGS__)
#define U64(...)     \
    u64 __VA_ARGS__; \
    scan(__VA_ARGS__)
#define F32(...)     \
    f32 __VA_ARGS__; \
    scan(__VA_ARGS__)
#define F64(...)     \
    f64 __VA_ARGS__; \
    scan(__VA_ARGS__)
#define F128(...)     \
    f128 __VA_ARGS__; \
    scan(__VA_ARGS__)
#define STR(...)             \
    std::string __VA_ARGS__; \
    scan(__VA_ARGS__)
#define CHR(...)      \
    char __VA_ARGS__; \
    scan(__VA_ARGS__)
#define VEC(type, name, size)     \
    std::vector<type> name(size); \
    scan(name)
#define VEC2(type, name1, name2, size)          \
    std::vector<type> name1(size), name2(size); \
    for (int i = 0; i < size; i++) scan(name1[i], name2[i])
#define VEC3(type, name1, name2, name3, size)                \
    std::vector<type> name1(size), name2(size), name3(size); \
    for (int i = 0; i < size; i++) scan(name1[i], name2[i], name3[i])
#define VEC4(type, name1, name2, name3, name4, size)                      \
    std::vector<type> name1(size), name2(size), name3(size), name4(size); \
    for (int i = 0; i < size; i++) scan(name1[i], name2[i], name3[i], name4[i])
#define VV(type, name, h, w)                       \
    std::vector name((h), std::vector<type>((w))); \
    scan(name)
// output
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    auto n = v.size();
    for (size_t i = 0; i < n; i++) {
        if (i) os << ' ';
        os << v[i];
    }
    return os;
}
template <class... T> void out(const T&... a) { (std::cout << ... << a); }
void print() { out('\n'); }
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {
    out(head);
    if (sizeof...(Tail)) out(' ');
    print(tail...);
}
// for interactive problems
void printi() { std::cout << std::endl; }
template <class Head, class... Tail> void printi(Head&& head, Tail&&... tail) {
    out(head);
    if (sizeof...(Tail)) out(' ');
    printi(tail...);
}
// bool output
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void NO(bool t = 1) { YES(!t); }
void No(bool t = 1) { Yes(!t); }
void no(bool t = 1) { yes(!t); }
void POSSIBLE(bool t = 1) { print(t ? "POSSIBLE" : "IMPOSSIBLE"); }
void Possible(bool t = 1) { print(t ? "Possible" : "Impossible"); }
void possible(bool t = 1) { print(t ? "possible" : "impossible"); }
void IMPOSSIBLE(bool t = 1) { POSSIBLE(!t); }
void Impossible(bool t = 1) { Possible(!t); }
void impossible(bool t = 1) { possible(!t); }
void FIRST(bool t = 1) { print(t ? "FIRST" : "SECOND"); }
void First(bool t = 1) { print(t ? "First" : "Second"); }
void first(bool t = 1) { print(t ? "first" : "second"); }
void SECOND(bool t = 1) { FIRST(!t); }
void Second(bool t = 1) { First(!t); }
void second(bool t = 1) { first(!t); }
// I/O speed up
struct SetUpIO {
    SetUpIO() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        std::cout << std::fixed << std::setprecision(20);
    }
} set_up_io;

template <class T> struct FenwickTree {
    int n;
    std::vector<T> seg;
    FenwickTree() : n(0) {}
    FenwickTree(int n) : n(n), seg(n + 1, 0) {}
    FenwickTree(std::vector<T>& arr) {
        n = int(arr.size());
        seg.resize(n + 1);
        for (int i = 0; i < n; i++) add(i, arr[i]);
    }
    // A[i] += x
    void add(int i, const T& x) {
        assert(0 <= i and i < n);
        i++;  // 1-indexed
        while (i <= n) {
            seg[i] += x;
            i += i & -i;
        }
    }
    // A[0] + ... + A[i - 1]
    T sum(int i) const {
        assert(0 <= i and i <= n);
        T s = T(0);
        while (i > 0) {
            s += seg[i];
            i -= i & -i;
        }
        return s;
    }
    // A[a] + ... + A[b - 1]
    T sum(int a, int b) const {
        assert(0 <= a and a <= b and b <= n);
        return sum(b) - sum(a);
    }
    // return A[i]
    T get(int i) const { return sum(i, i + 1); }
    // A[i] = x
    void set(int i, const T x) { add(i, x - get(i)); }

    std::vector<T> make_vector() {
        std::vector<T> vec(n);
        for (int i = 0; i < n; i++) vec[i] = get(i);
        return vec;
    }
};

template <class T> long long inversion_number(std::vector<T>& a) {
    auto z = a;
    std::sort(z.begin(), z.end());
    z.erase(std::unique(z.begin(), z.end()), z.end());
    const int n = (int)(z.size());
    FenwickTree<int> fen(n);
    long long ret = 0;
    for (auto& ai : a) {
        int i = lower_bound(z.begin(), z.end(), ai) - z.begin();
        ret += fen.sum(i + 1, n);
        fen.add(i, 1);
    }
    return ret;
}
using namespace std;

void solve() {
    // TODO: Implement
    INT(N, L);
    vector<i64> C(N + 1);
    vector P(N + 1, vector<int>(L));
    const int inv_max = 100;
    REP(i, N) {
        in(C[i + 1], P[i + 1]);
        REP(j, L) P[i + 1][j]--;
    }
    REP(j, L) P[0][j] = j;
    show(C);
    show(P);
    if (L == 1) {
        print(SUM(C));
        return;
    }
    vector<i64> dp(N + 1, -INF<i64>);
    dp[0] = 0;
    auto f = [&](vector<int>& a, vector<int>& b) -> int {
        vector<int> ainv(L);
        REP(i, L) ainv[a[i]] = i;
        vector<int> c(L);
        REP(i, L) c[i] = ainv[b[i]];
        return inversion_number<int>(c);
    };
    REP(i, N) {
        // calc i + 1
        show(i);
        REP(k, 1, inv_max + 1) {
            if (i + k > N) continue;
            // calc inv number P[i] and P[i + k]
            int cost = f(P[i], P[i + k]);
            if (cost <= k) {
                chmax(dp[i + k], dp[i] + C[i + k]);
            }
        }
    }
    show(dp);
    i64 ans = MAX(dp);
    print(ans);
    return;
}

int main() {
    int T = 1;
    // INT(T);
    REP(T) solve();
    return 0;
}

Submission Info

Submission Time
Task A - Swapping Game
User ruthen71
Language C++23 (GCC 15.2.0)
Score 600
Code Size 16460 Byte
Status AC
Exec Time 849 ms
Memory 6076 KiB

Compile Error

./Main.cpp: In function 'void solve()':
./Main.cpp:453:12: warning: statement has no effect [-Wunused-value]
  453 |     show(C);
      |            ^
./Main.cpp:454:12: warning: statement has no effect [-Wunused-value]
  454 |     show(P);
      |            ^
./Main.cpp:470:16: warning: statement has no effect [-Wunused-value]
  470 |         show(i);
      |                ^
./Main.cpp:480:13: warning: statement has no effect [-Wunused-value]
  480 |     show(dp);
      |             ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 41
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt, example3.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, example0.txt, example1.txt, example2.txt, example3.txt
Case Name Status Exec Time Memory
000.txt AC 1 ms 3612 KiB
001.txt AC 4 ms 5152 KiB
002.txt AC 204 ms 5504 KiB
003.txt AC 251 ms 5428 KiB
004.txt AC 300 ms 5444 KiB
005.txt AC 495 ms 5408 KiB
006.txt AC 736 ms 5940 KiB
007.txt AC 848 ms 5968 KiB
008.txt AC 355 ms 5408 KiB
009.txt AC 645 ms 5940 KiB
010.txt AC 848 ms 5932 KiB
011.txt AC 849 ms 6076 KiB
012.txt AC 735 ms 5940 KiB
013.txt AC 848 ms 6020 KiB
014.txt AC 829 ms 5804 KiB
015.txt AC 610 ms 5940 KiB
016.txt AC 722 ms 6076 KiB
017.txt AC 804 ms 5884 KiB
018.txt AC 840 ms 6008 KiB
019.txt AC 847 ms 6076 KiB
020.txt AC 734 ms 5968 KiB
021.txt AC 719 ms 5884 KiB
022.txt AC 804 ms 5936 KiB
023.txt AC 802 ms 5952 KiB
024.txt AC 837 ms 5952 KiB
025.txt AC 719 ms 5940 KiB
026.txt AC 777 ms 5952 KiB
027.txt AC 828 ms 6020 KiB
028.txt AC 844 ms 5828 KiB
029.txt AC 848 ms 5884 KiB
030.txt AC 723 ms 6020 KiB
031.txt AC 825 ms 5952 KiB
032.txt AC 841 ms 6008 KiB
033.txt AC 847 ms 5884 KiB
034.txt AC 371 ms 5876 KiB
035.txt AC 370 ms 5956 KiB
036.txt AC 375 ms 5940 KiB
example0.txt AC 1 ms 3480 KiB
example1.txt AC 1 ms 3444 KiB
example2.txt AC 1 ms 3588 KiB
example3.txt AC 1 ms 3452 KiB