/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 2.527 MiB
#2 Accepted 2ms 2.477 MiB
#3 Accepted 9ms 3.387 MiB
#4 Accepted 8ms 3.254 MiB
#5 Accepted 11ms 3.379 MiB
#6 Accepted 11ms 3.324 MiB
#7 Accepted 8ms 3.27 MiB
#8 Accepted 8ms 3.277 MiB
#9 Accepted 8ms 5.32 MiB
#10 Accepted 476ms 42.398 MiB
#11 Accepted 466ms 44.406 MiB
#12 Accepted 469ms 42.402 MiB
#13 Accepted 457ms 42.398 MiB
#14 Accepted 462ms 44.41 MiB
#15 Accepted 469ms 42.402 MiB
#16 Accepted 462ms 42.398 MiB
#17 Accepted 458ms 42.422 MiB
#18 Accepted 475ms 42.406 MiB
#19 Accepted 461ms 42.391 MiB
#20 Accepted 503ms 44.398 MiB
#21 Accepted 464ms 44.527 MiB
#22 Accepted 467ms 44.402 MiB
#23 Accepted 471ms 44.402 MiB
#24 Accepted 459ms 42.395 MiB
#25 Accepted 473ms 42.41 MiB
#26 Accepted 458ms 42.398 MiB
#27 Accepted 464ms 42.391 MiB

Code

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <cmath>
#include <random>
#include <tuple>
#include <numeric>
#include <map>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;

template <typename T, typename = void>
struct has_val_function : std::false_type {};

template <typename T>
struct has_val_function<T, std::void_t<decltype(std::declval<std::ostream&>() << std::declval<T>().val())>> : std::true_type { };

template<typename t>
typename std::enable_if_t<has_val_function<t>::value, std::ostream&>
operator << (ostream& o, const t &c)
{
    o << c.val() << " ";
    return o;
}

template<typename t, typename b>
ostream& operator << (ostream& o, const pair<t, b> &c)
{

    o << "(" << c.first <<"," << c.second << ") ";
    return o;
}


template<typename t, typename b, typename x>
ostream& operator << (ostream& o, const tuple<t, b, x> &c)
{

    o << "(" << std::get<0>(c) <<"," << std::get<1>(c) << "," << std::get<2>(c) << ") ";
    return o;
}

template<typename t>
typename std::enable_if_t<std::is_void<std::void_t<decltype(std::begin(std::declval<t>())), decltype(std::end(std::declval<t>()))>>::value, std::ostream&>
        operator << (ostream& o, const t &c)
{
    for(size_t i = 0; i < c.size(); i++)
    {
        o << c[i] << " ";
    }
    return o;
}
const int N = 2e5 + 5;

const string yes = "YES";
const string no = "NO";

int a[N][6];
int n, k;

int f[N][1 << 5];

pair<int, int> closed[1 << 5];


int main()
{
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j < k; j++)
        {
            cin >> a[i][j];
        }
        for(int j = 0; j < (1 << k); j++)
        {
            for(int x = 0; x < k; x++)
            {
                if (j >> x & 1)
                    f[i][j] += a[i][x];
                else f[i][j] -= a[i][x];
            }
        }
    }
    vector<vector<pair<int, int>>> open(1 << k);
    vector<int> bg(1 << k, 0);
    vector<int> del(1 + n, 0);


    for(int j = 0; j < (1 << k); j++)
    {
        closed[j].first = 1;
        closed[j].second = 1;
    }
    for(int i = 2; i <= n; i++)
    {
        for(int j = 0; j < (1 << k); j++)
        {
            open[j].push_back({f[i][j], i});
        }
    }
    vector<int> ed(1 << k, n - 2);

    del[1] = 1;

    for(int j = 0; j < (1 << k); j++)
    {
        sort(open[j].begin(), open[j].end());
    }

    auto getmin = [&](int ind)
    {
        while(del[open[ind][bg[ind]].second])
        {
            bg[ind]++;
        };
        return open[ind][bg[ind]];
    };

    auto getmax = [&](int ind)
    {
        while(del[open[ind][ed[ind]].second])
        {
            ed[ind]--;
        };
        return open[ind][ed[ind]];
    };

    ll ans = 0;
    for(int i = 1; i < n; i++)
    {
        int best = -1;
        int id = 0;
        for(int j = 0; j < (1 << k); j++)
        {
            pair<int, int> minv = { f[closed[j].first][j], closed[j].first};
            pair<int, int> maxv = { f[closed[j].second][j], closed[j].second};

            auto cminv = getmin(j);
            auto cmaxv = getmax(j);


            if (abs(minv.first - cmaxv.first) >= best)
            {
                best = abs(minv.first - cmaxv.first);
                id = cmaxv.second;
            }

            if (abs(minv.first - cminv.first) >= best)
            {
                best = abs(minv.first - cminv.first);
                id = cminv.second;
            }

            if (abs(maxv.first - cminv.first) >= best)
            {
                best = abs(maxv.first - cminv.first);
                id = cminv.second;
            }

            if (abs(maxv.first - cmaxv.first) >= best)
            {
                best = abs(maxv.first - cmaxv.first);
                id = cmaxv.second;
            }
        }
        ans += best;
        for(int j = 0; j < (1 << k); j++)
        {
            if (f[closed[j].first][j] > f[id][j])
            {
                closed[j].first = id;
            }
            if (f[closed[j].second][j] < f[id][j])
            {
                closed[j].second = id;
            }
        }
        del[id] = 1;
    }
    cout << ans << endl;
}

Information

Submit By
Type
Submission
Problem
P1098 KuZ the Maximum spanning tree
Contest
LU IUJPC : Sylhet Division 2024 Replay Contest
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 11:56:52
Judged At
2024-12-10 11:56:52
Judged By
Score
100
Total Time
503ms
Peak Memory
44.527 MiB