QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#639918#1762. Low Effort Leagueoxford01#AC ✓48ms5944kbC++204.2kb2024-10-13 23:53:112024-10-13 23:53:12

Judging History

你现在查看的是最新测评结果

  • [2024-10-13 23:53:12]
  • 评测
  • 测评结果:AC
  • 用时:48ms
  • 内存:5944kb
  • [2024-10-13 23:53:11]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (auto i = a; i < (b); ++i)
#define repr(i, a, b) for (auto i = (a) - 1; i >= (b); --i)
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pii>;

template<class T>
inline bool cmax(T &a, const T& b) {
    return a < b ? a = b, 1 : 0;
}

template<class T>
inline bool cmin(T &a, const T &b) {
    return b < a ? a = b, 1 : 0;
}

const int inf = 0x3f3f3f3f;
const ll linf = 1e18;
const double dinf = numeric_limits<double>::infinity();

struct Line {
    mutable ll k, m, p;
    bool operator<(const Line &o) const {
        return k < o.k;
    }
    bool operator<(ll x) const {
        return p < x;
    }
};
struct LineContainer : multiset<Line, less<> >{
    static const ll inf = LLONG_MAX;
    ll div(ll a, ll b) {
        return a / b - ((a ^ b) < 0 && a % b);
    }
    bool isect(iterator x, iterator y) {
        if (y == end())
            return x->p = inf, 0;
        if (x->k == y->k)
            x->p = x->m > y->m ? inf : -inf;
        else
            x->p = div(y->m - x->m, x->k - y->k);
        return x->p >= y->p;
    }
    void add(ll k, ll m) {
        auto z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z))
            z = erase(z);
        if (x != begin() && isect(--x, y))
            isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }
    ll query(ll x) {
        assert(!empty());
        auto l = *lower_bound(x);
        return l.k * x + l.m;
    }
};

int n;
vll s;
map< tuple<int, int, int>, ll> mem;

ll dp(int l, int r, int k) {
    if (mem.contains({l, r, k})) {
        return mem[{l, r, k}];
    }
    int m = (l + r) / 2;
    LineContainer lc;
    
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    cin >> n;

    s.resize(1 << n);
    rep(i, 0, 1 << n) {
        cin >> s[i];
    }

    vector<vll> dp(n + 1);
    // vector< vector<vll> > dp(n + 1);
    dp[0].assign(1 << n, 0);

    rep(i, 1, n + 1) {
        dp[i].resize(1 << n);
        for (int l = 0, pos = 0; l < (1 << n); l += (1 << i), ++pos) {
            // dp[i].eb(1 << i, linf);

            int r = l + (1 << i);
            int m = (l + r) / 2;

            vi ord(r - l);
            iota(all(ord), l);
            sort(all(ord), [&](int a, int b) { 
                return s[a] > s[b];
            });
            {
                multiset<ll> st;
                rep(j, m, r) {
                    st.insert(dp[i - 1][j]);
                }
                LineContainer lc;
                for (auto &j : ord) {
                    if (j < m) {
                        // if (!lc.empty()) ll askjf = -lc.query(s[j]);
                        dp[i][j] = dp[i - 1][j] + min(lc.empty() ? linf : (-lc.query(s[j]) + s[j] * s[j]), st.empty() ? linf : *st.begin());
                    } else {
                        st.erase(st.find(dp[i - 1][j]));
                        // cerr << (2 * s[j]) << ' ' << -(dp[i - 1][j] + s[j] * s[j]) << endl;
                        lc.add((2 * s[j]), -(dp[i - 1][j] + s[j] * s[j]));
                    }
                }
            }
            {   
                multiset<ll> st;
                rep(j, l, m) {
                    st.insert(dp[i - 1][j]);
                }
                LineContainer lc;
                for (auto &j : ord) {
                    if (j >= m) {
                        dp[i][j] = dp[i - 1][j] + min(lc.empty() ? linf : (-lc.query(s[j]) + s[j] * s[j]), st.empty() ? linf :*st.begin());
                    } else {
                        st.erase(st.find(dp[i - 1][j]));
                        lc.add((2 * s[j]), -(dp[i - 1][j] + s[j] * s[j]));
                    }
                }
            }   
        }
    }
    cout << dp[n][0] << '\n';

    // rep(i, 0, n + 1) {
    //     rep(j, 0, 1 << n) {
    //         cerr << dp[i][j] << ' ';
    //     }
    //     cerr << endl;
    // }

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3860kb

input:

3
77 64 99 28 15 16 76 13

output:

484

result:

ok single line: '484'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

5
50 10 30 20 90 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 40

output:

1600

result:

ok single line: '1600'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3872kb

input:

7
442 77 280 657 4 262 285 147 294 50 290 664 36 563 440 807 687 18 65 931 989 387 567 46 550 1040 166 955 15 493 33 362 451 514 245 922 709 20 542 47 326 21 446 258 196 124 825 237 141 384 626 999 303 897 276 517 164 439 848 492 1044 114 398 692 417 150 877 201 267 3 197 19 91 148 543 603 215 83 17...

output:

774858

result:

ok single line: '774858'

Test #4:

score: 0
Accepted
time: 44ms
memory: 5728kb

input:

14
84176 363252 7222 940122 387873 67223 919681 285066 266835 433861 302154 532900 303940 134071 353383 649043 22485 357464 408969 179312 302766 135536 519923 78300 283204 145196 71006 270468 214973 583824 53854 2398 994608 36115 559438 209115 973036 379297 102554 40113 545652 483384 776036 377162 4...

output:

6474594441930

result:

ok single line: '6474594441930'

Test #5:

score: 0
Accepted
time: 44ms
memory: 5944kb

input:

14
822516 584290 1755 650351 156210 35863 364975 320557 245296 318696 502457 143973 650887 1138 149812 787246 550860 38247 63830 249308 288473 119445 699186 625001 211865 845470 4185 4794 525872 815709 660017 485169 653849 6728 606907 566795 656045 869488 380161 15012 168279 112548 9193 331354 10639...

output:

140680435470

result:

ok single line: '140680435470'

Test #6:

score: 0
Accepted
time: 47ms
memory: 5764kb

input:

14
650346 61400 544190 484669 403942 185263 25977 519 413034 885731 685607 29832 501039 943802 16371 767520 285100 476 82868 35529 4796 6790 312054 914754 49484 962916 877299 23717 561948 498599 28602 10309 250128 19291 82040 677373 924903 155149 684659 7162 3529 52568 792270 252603 70342 27754 4268...

output:

739176081154

result:

ok single line: '739176081154'

Test #7:

score: 0
Accepted
time: 48ms
memory: 5760kb

input:

14
596733 75352 732944 155710 2705 268798 993107 861844 700285 19106 944782 453181 41334 33024 64416 306080 33059 177086 828010 604217 22772 27609 6825 521631 734598 498025 140157 123703 105172 224511 84745 645612 26336 24413 477 130205 415846 26460 780506 67717 202438 336865 530699 146730 206593 83...

output:

1038922772804

result:

ok single line: '1038922772804'

Test #8:

score: 0
Accepted
time: 48ms
memory: 5784kb

input:

14
186065 540891 792520 901282 255711 999531 513002 14772 989436 31074 667464 9632 204302 843410 489753 372772 603499 743 517385 369217 158186 339798 81993 47343 137362 854748 615796 438344 207619 10118 249031 743136 586781 959349 80152 134802 115345 46088 337300 145607 471527 74794 754139 513900 20...

output:

5182406567896

result:

ok single line: '5182406567896'