QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#759152 | #9252. Penguins in Refrigerator | Yansuan_HCl | WA | 67ms | 75240kb | C++14 | 2.9kb | 2024-11-17 22:09:57 | 2024-11-17 22:10:01 |
Judging History
answer
#include <bits/stdc++.h>
#define ms(x, v) memset(x, v, sizeof(x))
#define il __attribute__((always_inline)) static
#define U(i,l,r) for(int i(l),END##i(r);i<=END##i;++i)
#define D(i,r,l) for(int i(r),END##i(l);i>=END##i;--i)
using namespace std;
using ll = long long;
#define IC isdigit(c)
#define GC c=getchar()
void rd(auto &x) { x = 0; char GC; bool f = 0;
for (; !IC; GC) f |= c == '-';
for (; IC; GC) x = x * 10 + c - 48;
if (f) x = -x;
}
void rd(auto &x, auto &...y) { rd(x); rd(y...); }
#define meow(...) fprintf(stderr, __VA_ARGS__)
#define Assert(e) if (!(e)) { meow("AF@%d\n", __LINE__); exit(__LINE__); }
#define vc vector
#define pb push_back
#define eb emplace_back
const int N = 1000006;
const ll P = 1000000007;
ll fac[N], ifac[N];
ll qpow(ll x, ll t = P - 2) { ll v = 1;
for (; t; (x *= x) %= P, t >>= 1) if (t & 1)
(v *= x) %= P;
return v;
}
void gen() {
fac[0] = 1; U (i, 1, N - 1) fac[i] = fac[i - 1] * i %P;
ifac[N - 1] = qpow(fac[N - 1]);
D (i, N - 1, 1) ifac[i - 1] = ifac[i] * i %P;
}
int n, m, W, w[N], a[N], ls[N];
int ch[N][2], rt;
void cat() {
int stk[N] {}, sp = 0;
U (i, 1, n) {
while (sp && w[a[i]] >= w[a[stk[sp]]]) --sp;
ch[i][0] = stk[sp + 1];
if (sp) ch[stk[sp]][1] = i;
stk[++sp] = i;
stk[sp + 1] = 0;
}
rt = stk[1];
}
int fa[N][20];
void dfs(int u, int f) {
fa[u][0] = f;
U (k, 1, 19) fa[u][k] = fa[fa[u][k - 1]][k - 1];
for (int v : ch[u]) if (v) {
dfs(v, u);
}
}
bool fix[N];
int rf[N]; vc<int> op[N];
ll ans = 1; int p[N], ptr;
int siz[N];
set<int> pq;
void dfs2(int u) {
for (int i : op[u]) pq.insert(a[i]);
siz[u] = fix[u];
if (ch[u][1]) dfs2(ch[u][1]), siz[u] += siz[ch[u][1]];
if (fix[u]) {
while (pq.size() && *pq.begin() < a[u])
p[++ptr] = *pq.begin(), pq.erase(pq.begin());
p[++ptr] = a[u];
}
if (ch[u][0]) dfs2(ch[u][0]), siz[u] += siz[ch[u][0]];
vc<int> tmp;
for (int i : op[u]) if (pq.find(a[i]) != pq.end()) tmp.pb(a[i]);
sort(tmp.begin(), tmp.end());
for (int x : tmp) {
p[++ptr] = x;
pq.erase(x);
}
(ans *= fac[siz[u] + op[u].size()] * ifac[siz[u]] %P) %= P;
siz[u] += op[u].size();
}
int main() {
// freopen("ava.in", "r", stdin);
// freopen(".out", "w", stdout);
gen();
rd(n, W);
U (i, 1, n) rd(a[i]);
U (i, 1, n) rd(w[i]);
// U (i, 1, n) rd(w[i]), ls[i] = w[i];
// sort(ls + 1, ls + n + 1); m = unique(ls + 1, ls + n + 1) - ls - 1;
// U (i, 1, n) w[i] = lower_bound(ls + 1, ls + m + 1, w[i]) - ls;
cat();
dfs(rt, 0);
U (i, 1, n) {
int u = i;
D (k, 19, 0) {
int v = fa[u][k]; if (!v) continue;
if (w[a[i]] + w[a[v]] <= W)
u = v;
}
if (u == i) {
fix[u] = 1;
} else {
op[u].pb(i);
}
}
dfs2(rt);
printf("%lld\n", ans);
assert(!pq.size());
// while (pq.size())
// p[++ptr] = pq.top(), pq.pop();
U (i, 1, n)
printf("%d ", p[i]);
puts("");
}
详细
Test #1:
score: 100
Accepted
time: 20ms
memory: 57208kb
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: 14ms
memory: 57444kb
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: 58484kb
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: 16ms
memory: 59760kb
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: 67ms
memory: 75240kb
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: 40ms
memory: 71264kb
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 74728 75343 82792 36175 87721 35785 2481 26125 29028 34715...
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 '