QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#552528 | #9252. Penguins in Refrigerator | ucup-team296# | WA | 150ms | 18808kb | C++14 | 5.8kb | 2024-09-07 23:27:56 | 2024-09-07 23:27:57 |
Judging History
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);
}
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: 3620kb
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: 3760kb
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: 3628kb
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: 3564kb
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: -100
Wrong Answer
time: 150ms
memory: 18808kb
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:
wrong answer 2nd lines differ - expected: '1 2 3 4 5 6 7 8 9 10 11 12 13 ... 99996 99997 99998 99999 100000', found: '1 2 3 4 5 6 7 8 9 10 11 12 13 ... 27570 64295 55848 30108 39370 '