QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#552532#9252. Penguins in Refrigeratorucup-team296#WA 155ms19324kbC++145.9kb2024-09-07 23:31:442024-09-07 23:31:44

Judging History

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

  • [2024-09-07 23:31:44]
  • 评测
  • 测评结果:WA
  • 用时:155ms
  • 内存:19324kb
  • [2024-09-07 23:31:44]
  • 提交

answer

#include <bits/stdc++.h>
#define long long long int
#define DEBUG
using namespace std;

// @author: pashka

int mod = 1000000007;

struct mint {
    int value = 0;

    constexpr mint() : value() {}

    mint(const long &x) {
        value = normalize(x);
    }

    static long normalize(const long &x) {
        long v = x % mod;
        if (v < 0) v += mod;
        return v;
    }

    bool operator==(const mint &x) { return value == x.value; };

    mint operator+(const mint &x) { return value + x.value; };

    mint operator-(const mint &x) { return value - x.value; };

    mint operator*(const mint &x) { return (long) value * x.value; };

    void operator+=(const mint &x) { value = normalize(value + x.value); };

    void operator-=(const mint &x) { value = normalize(value - x.value); };
};

mint power(mint a, long b) {
    mint res = 1;
    while (b > 0) {
        if (b & 1) {
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}

mint inv(mint a) {
    return power(a, mod - 2);
}

vector<mint> fact_precalc(1, 1);
vector<mint> inv_fact_precalc(1, 1);

void precalc(int n) {
    fact_precalc.resize(n + 1);
    fact_precalc[0] = 1;
    for (int i = 1; i < fact_precalc.size(); i++) {
        fact_precalc[i] = fact_precalc[i - 1] * i;
    }
    inv_fact_precalc.resize(n + 1);
    inv_fact_precalc[n] = inv(fact_precalc[n]);
    for (int i = n - 1; i >= 0; i--) {
        inv_fact_precalc[i] = inv_fact_precalc[i + 1] * (i + 1);
    }
}

void ensure_fact(int n) {
    while (n >= fact_precalc.size()) {
        fact_precalc.push_back(fact_precalc.back() * fact_precalc.size());
        inv_fact_precalc.push_back(inv(fact_precalc.back()));
    }
}

mint fact(int n) {
    ensure_fact(n);
    return fact_precalc[n];
}

mint inv_fact(int n) {
    ensure_fact(n);
    return inv_fact_precalc[n];
}

mint calc_c(int n, int k) {
    if (n < 0 || k < 0 || k > n) {
        return 0;
    }
    mint res = fact(n);
    res = res * inv_fact(k);
    res = res * inv_fact(n - k);
    return res;
}

mint calc_a(int n, int k) {
    if (n < 0 || k < 0 || k > n) {
        return 0;
    }
    mint res = fact(n);
    res = res * inv_fact(n - k);
    return res;
}


struct segtree_max {
    typedef pair<long, int> item;

    item zeroSum = {LLONG_MIN, -1};

    item sum(item a, item b) {
        return max(a, b);
    }

    vector<item> sums;

    int size;

    void set(int i, item x, int n, int L, int R) {
        if (R == L + 1) {
            sums[n] = x;
            return;
        }
        int M = (L + R) >> 1;
        if (i < M) {
            set(i, x, 2 * n + 1, L, M);
        } else {
            set(i, x, 2 * n + 2, M, R);
        }
        sums[n] = sum(sums[2 * n + 1], sums[2 * n + 2]);
    }

    item calc(int l, int r, int n, int L, int R) {
        if (l >= R || L >= r) return zeroSum;
        if (L >= l && R <= r) {
            return sums[n];
        }
        int M = (L + R) >> 1;
        return sum(calc(l, r, 2 * n + 1, L, M),
                   calc(l, r, 2 * n + 2, M, R));
    }

    explicit segtree_max(int n) {
        size = 1;
        while (size < n) size *= 2;
        sums.assign(2 * size, zeroSum);
    }

    explicit segtree_max(vector<item> a): segtree_max(a.size()) {
        int n = a.size();
        for (int i = 0; i < n; i++) {
            sums[size - 1 + i] = a[i];
        }
        for (int i = size - 2; i >= 0; i--) {
            sums[i] = sum(sums[2 * i + 1], sums[2 * i + 2]);
        }
    }

    void set(int i, item x) {
        set(i, x, 0, 0, size);
    }

    item calc(int l, int r) {
        return calc(l, r, 0, 0, size);
    }
};

struct fenwick {
    vector<long> f;


    fenwick(int n) {
        f.assign(n + 1, 0);
    }

    long sum(int r) { // exclusive
        r--;
        long result = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            result += f[r];
        return result;
    }

    void inc(int i, long x) {
        for (; i < f.size(); i = (i | (i + 1)))
            f[i] += x;
    }
};

int W;
vector<int> ans;
set<int> av;
vector<int> p;

mint go(int l, int r, segtree_max &mx, segtree_max &mn, fenwick &f) {
    int n = f.sum(r) - f.sum(l);
    if (n == 0) return 1;
    auto [a, b] = mx.calc(l, r);
    mx.set(b, mn.zeroSum);
    mn.set(b, mn.zeroSum);
    f.inc(b, -1);
    vector<int> q;
    while (true) {
        auto [c, d] = mn.calc(l, r);
        if (c == mn.zeroSum.first) break;
        if (-c + a > W) break;
        q.push_back(p[d]);
        av.insert(p[d]);
        mn.set(d, mn.zeroSum);
        mx.set(d, mn.zeroSum);
        f.inc(d, -1);
    }
    sort(q.begin(), q.end());
    mint res = go(l, b, mx, mn, f);
    int xx = p[b];
    while (!av.empty() && *av.begin() < xx) {
        ans.push_back(*av.begin());
        av.erase(av.begin());
    }
    ans.push_back(xx);
    res = res * go(b + 1, r, mx, mn, f);
    for (int t : q) {
        if (av.find(t) != av.end()) {
            av.erase(t);
            ans.push_back(t);
        }
    }
    res = res * calc_a(n, q.size());
    return res;
}

int main() {
    ios::sync_with_stdio(false);

    int n;
    cin >> n >> W;
    p.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i]--;
    }
    vector<long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    reverse(p.begin(), p.end());

    segtree_max mx(n);
    segtree_max mn(n);
    for (int i = 0; i < n; i++) {
        mx.set(i, {a[p[i]], i});
        mn.set(i, {-a[p[i]], i});
    }
    fenwick f(n);
    for (int i = 0; i < n; i++) {
        f.inc(i, 1);
    }

    auto r = go(0, n, mx, mn, f);
    cout << r.value << "\n";
    for (int x : ans) cout << x + 1 << " ";
    cout << "\n";

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 10
1 2 3 4 5
6 5 3 9 2

output:

3
5 4 2 1 3 

result:

ok 2 lines

Test #2:

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

input:

5 10
1 2 3 4 5
2 4 3 3 8

output:

30
1 5 2 3 4 

result:

ok 2 lines

Test #3:

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

input:

5 10
1 2 3 4 5
2 3 4 5 1

output:

120
1 2 3 4 5 

result:

ok 2 lines

Test #4:

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

input:

5 10
1 2 3 4 5
2 3 4 5 6

output:

60
1 2 3 5 4 

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 155ms
memory: 19324kb

input:

100000 96
1996 78922 45321 68844 32404 82013 66552 81163 17216 48170 35495 56660 13480 43118 23173 47257 50168 87069 26167 67231 31758 25694 61063 56642 8923 7727 54528 96554 38964 7604 6822 16256 45300 58869 31359 48638 87645 14779 81505 59585 89293 9291 7002 31810 84701 77648 78295 42595 11394 479...

output:

457992974
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

result:

ok 2 lines

Test #6:

score: -100
Wrong Answer
time: 122ms
memory: 14952kb

input:

100000 84
93330 3894 94859 22134 49668 30606 26739 82976 76701 56323 75537 7626 87226 20857 98177 21811 70827 75898 8111 48223 26186 64222 63002 79024 19126 41638 1048 43857 25379 19764 60207 27675 77665 66327 6274 34861 30287 13449 64505 51490 5804 65843 49014 85795 12365 31565 34411 71697 66568 28...

output:

524727018
1723 2800 15421 26278 31659 42502 42606 56945 60694 62369 70160 73990 80586 88502 89122 59690 27661 33622 94788 14089 1146 4491 17476 39523 2690 3632 5439 8588 17922 18136 18825 20123 24857 28520 30999 32947 36013 41413 43842 43919 54502 58104 60783 65767 69064 71878 74728 75343 82792 3617...

result:

wrong answer 2nd lines differ - expected: '1723 2800 15421 26278 31659 42...6 87226 93330 98177 26739 49668', found: '1723 2800 15421 26278 31659 42... 98177 26739 49668 75537 93330 '