QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#751730#9252. Penguins in RefrigeratorHunsterWA 0ms24228kbC++232.5kb2024-11-15 20:20:172024-11-15 20:20:17

Judging History

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

  • [2024-11-15 20:20:17]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:24228kb
  • [2024-11-15 20:20:17]
  • 提交

answer

#include <bits/stdc++.h>

constexpr int mod = (int)(1e9) + 7;

constexpr int N = 1000006;

int n, W;
int p[N], w[N];
int max[20][N], hl[N], hr[N], deg[N];
int qmax(int l, int r) {
    int k = std::__lg(r - l + 1);
    return std::max(max[k][l], max[k][r - (1 << k) + 1]);
}

struct BIT {
    int sum[N];
    void add(int x, int v) { for (; x < N; x += x & -x) sum[x] += v; }
    int query(int x) { int res = 0; for (; x; x -= x & -x) res += sum[x]; return res; }
};
BIT bit;

std::vector<int> graph[N];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n >> W;
    for (int i = 1; i <= n; i++) std::cin >> p[i];
    for (int i = 1; i <= n; i++) std::cin >> w[i];
    for (int i = 1; i <= n; i++) max[0][i] = w[p[i]];
    for (int i = 1; i < 20; i++) for (int j = 1; j + (1 << i) - 1 <= n; j++) max[i][j] = std::max(max[i - 1][j], max[i - 1][j + (1 << i - 1)]);
    std::map<int, std::vector<int>> map;
    for (int i = 1, l, r, lst = 0; i <= n; i++) {
        if (w[p[i]] * 2 <= W) {
            l = 0, r = i - 1;
            while (l < r) {
                int mid = (l + r + 1) >> 1;
                if (qmax(mid, i - 1) + w[p[i]] > W) l = mid;
                else r = mid - 1;
            }
            hl[i] = l;
            l = i + 1, r = n + 1;
            while (l < r) {
                int mid = (l + r) >> 1;
                if (qmax(i + 1, mid) + w[p[i]] > W) r = mid;
                else l = mid + 1;
            }
            hr[i] = r;
            if (hl[i] >= 1) graph[p[hl[i]]].push_back(p[i]);
            if (hr[i] <= n) graph[p[i]].push_back(p[hr[i]]);
            map[w[p[i]]].push_back(i);
        }
        else {
            if (lst) graph[p[lst]].push_back(p[i]);
            bit.add(i, 1);
            lst = i;
        }
    }
    int ans = 1;
    for (auto it = map.rbegin(); it != map.rend(); it = std::next(it)) {
        auto &[v, h] = *it;
        for (int i : h) {
            bit.add(i, 1);
            int t = bit.query(hr[i] - 1) - bit.query(hl[i]);
            ans = 1ll * ans * t % mod;
        }
    }
    printf("%d\n", (ans + mod) % mod);
    for (int i = 1; i <= n; i++) for (int j : graph[i]) deg[j]++;
    std::priority_queue<int, std::vector<int>, std::greater<int>> que;
    for (int i = 1; i <= n; i++) if (!deg[i]) que.push(i);
    while (que.size()) {
        int u = que.top();
        que.pop();
        printf("%d ", u);
        for (int v : graph[u]) if (--deg[v] == 0) que.push(v);
    }
    putchar('\n');
    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 24228kb

input:

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

output:

3
1 2 3 4 5 

result:

wrong answer 2nd lines differ - expected: '5 4 2 1 3', found: '1 2 3 4 5 '