QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#552689#9252. Penguins in Refrigeratorucup-team180#WA 82ms22632kbC++206.4kb2024-09-08 00:56:232024-09-08 00:56:23

Judging History

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

  • [2024-09-08 00:56:23]
  • 评测
  • 测评结果:WA
  • 用时:82ms
  • 内存:22632kb
  • [2024-09-08 00:56:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define ov3(a, b, c, name, ...) name
#define rep0(n) for(ll aaaaa = 0; aaaaa < (n); aaaaa++)
#define rep1(i, n) for(ll i = 0; i < (n); i++)
#define rep2(i, a, b) for(ll i = (a); i < (b); i++)
#define rep(...) ov3(__VA_ARGS__, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for(ll i = (a) - 1; i >= b; i--)
#define fore(e, v) for(auto &&e : v)
#define all(a) begin(a), end(a)
#define si(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back
template <typename T, typename S> bool chmin(T &a, S &b) { return a > b ? a = b, true : false; }
template <typename T, typename S> bool chmax(T &a, S &b) { return a < b ? a = b, true : false; }

const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;

#define i128 __int128_t

struct _ {
    _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

constexpr ll mod = 1e9 + 7;
struct mint {
    int x;
    mint(ll x_ = 0) : x(x_ % mod) {
        if(x < 0) x += mod;
    }
    mint operator-() { return mint(-x); }
    mint &operator+=(const mint &r) {
        x += r.x;
        if(x >= mod) x -= mod;
        return *this;
    }
    mint &operator-=(const mint &r) {
        x -= r.x;
        if(x < 0) x += mod;
        return *this;
    }
    mint &operator*=(const mint &r) {
        x = 1LL * x * r.x % mod;
        return *this;
    }
    mint inv() const { return pow(mod - 2); }
    mint pow(ll b) const {
        mint a = *this, c = 1;
        while(b) {
            if(b & 1) c *= a;
            a *= a;
            b >>= 1;
        }
        return c;
    }
    mint &operator/=(const mint &r) { return (*this) *= r.inv(); }
    friend mint operator+(mint a, mint b) { return (a += b); }
    friend mint operator-(mint a, mint b) { return (a -= b); }
    friend mint operator*(mint a, mint b) { return (a *= b); }
    friend mint operator/(mint a, mint b) { return (a /= b); }
};
using vm = vector<mint>;

vector<int> cartesian_tree(vi &a) {
    int n = si(a);
    vi pr(n, -1);
    stack<int> st;
    st.push(0);
    rep(i, 1, n) {
        int prev = -1;
        while(!st.empty()) {
            int j = st.top();
            if(a[i] > a[j]) {
                st.pop();
                if(prev != -1) { pr[prev] = j; }
                prev = j;
            } else {
                break;
            }
        }
        if(prev != -1) { pr[prev] = i; }
        st.push(i);
    }
    while(st.size() >= 2) {
        int x = st.top();
        st.pop();
        pr[x] = st.top();
    }
    return pr;
}

template <typename S, S (*op)(S, S), S (*e)()> struct segtree {
    segtree(int n) : segtree(vector<S>(n, e())) {}
    segtree(const vector<S> &v) : n(si(v)) {
        s = bit_ceil(unsigned(n));
        log = countr_zero(unsigned(s));
        d = vector<S>(2 * s, e());
        rep(i, n) d[s + i] = v[i];
        per(i, s, 1) update(i);
    }
    void set(int p, S x) {
        d[p += s] = x;
        rep(i, 1, log + 1) update(p >> i);
    }
    S prod(int l, int r) const {
        S sml = e(), smr = e();
        l += s, r += s;
        while(l < r) {
            if(l & 1) sml = op(sml, d[l++]);
            if(r & 1) smr = op(d[--r], smr);
            l >>= 1, r >>= 1;
        }
        return op(sml, smr);
    }
    S all_prod() const { return d[1]; }

  private:
    int n, s, log;
    vector<S> d;
    void update(int k) { d[k] = op(d[k * 2], d[k * 2 + 1]); }
};

pii f(pii x, pii y) { return min(x, y); }
pii e() { return pii(INF, INF); }

constexpr int N = 1e6 + 100;
mint fact[N], ifact[N];
void pre() {
    fact[0] = 1;
    rep(i, 1, N) fact[i] = i * fact[i - 1];
    ifact[N - 1] = fact[N - 1].inv();
    per(i, N - 1, 0) ifact[i] = ifact[i + 1] * (i + 1);
}
mint C(int n, int m) { return (n < m or m < 0 ? 0 : fact[n] * ifact[m] * ifact[n - m]); }

int f2(int x, int y) { return x + y; }
int e2() { return 0; }

int main() {
    pre();
    int n, W;
    cin >> n >> W;
    vi p(n);
    fore(e, p) cin >> e, e--;

    vi w(n);
    fore(e, w) cin >> e;

    {
        vi ww(n);
        rep(i, n) ww[p[i]] = w[i];
        swap(w, ww);
    }
    reverse(all(p)), reverse(all(w));

    auto c = cartesian_tree(w);
    vector<pii> g(n, pii(-1, -1));
    int r;

    rep(i, n) {
        if(c[i] != -1) {
            if(i < c[i])
                g[c[i]].first = i;
            else
                g[c[i]].second = i;
        } else {
            r = i;
        }
    }

    // rep(i, n) { cout << g[i].first << " " << g[i].second << endl; }

    vector<pii> setter(n);
    rep(i, n) setter[i] = pii(w[i], i);
    segtree<pii, f, e> seg(setter);
    segtree<int, f2, e2> rst(vi(n, 1));

    set<int> s;
    vi res;

    mint ans = 1;
    auto dfs = [&](auto &&f, int l, int r, int t) {
        if(l == r or rst.prod(t, t + 1) == 0) return;
        int off = W - w[t];
        vi v;
        seg.set(t, pii(INF, t));
        while(true) {
            auto [val, i] = seg.prod(l, r);
            if(val > off) break;
            v.eb(i);
            seg.set(i, pii(INF, i));
        }
        sort(all(v));
        fore(e, v) s.emplace(p[e]);

        int n = rst.prod(l, r);
        // cout << l << " " << r << " " << t << endl;
        // cout << n << " " << si(v) << endl;
        // fore(e, v) cout << e << " ";
        // cout << endl;

        ans *= C(n, si(v)) * fact[si(v)];
        fore(e, v) rst.set(e, 0);

        if(g[t].first != -1) f(f, l, t, g[t].first);
        while(!empty(s) and *begin(s) < p[t]) {
            res.eb(*begin(s));
            s.erase(begin(s));
        }
        res.eb(p[t]);
        if(g[t].second != -1) f(f, t + 1, r, g[t].second);
        vi w(si(v));
        rep(i, si(v)) w[i] = p[v[i]];
        sort(all(w));

        // cout << "w" << endl;
        // fore(e, w) cout << e << " ";
        // cout << endl;
        fore(e, w) {
            if(auto it = s.find(e); it != end(s)) {
                while(*begin(s) < e) res.eb(*begin(s)), s.erase(begin(s));
                res.eb(e);
                s.erase(e);
            }
        }
    };
    dfs(dfs, 0, n, r);

    cout << ans.x << endl;
    // reverse(all(res));
    rep(i, n) cout << res[i] + 1 << " \n"[i == n - 1];
}

详细

Test #1:

score: 100
Accepted
time: 12ms
memory: 11352kb

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: 12ms
memory: 11440kb

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: 12ms
memory: 11440kb

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: 8ms
memory: 11440kb

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: 82ms
memory: 22632kb

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: 50ms
memory: 17672kb

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:

120390347
1146 14089 15421 29028 33622 62369 67265 80586 80614 89122 59690 27661 56945 94788 88414 43842 2800 4491 17476 20123 28520 32947 36175 39523 71878 78078 8588 18825 24857 36013 41413 43919 74728 75343 82792 87721 98143 69064 30999 35785 2481 88502 1723 2690 22878 26125 31659 18136 34715 545...

result:

wrong answer 1st lines differ - expected: '524727018', found: '120390347'