QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#259124#5466. Permutation Compressionkai_weiWA 1ms3836kbC++202.1kb2023-11-20 16:54:372023-11-20 16:54:38

Judging History

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

  • [2023-11-20 16:54:38]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3836kb
  • [2023-11-20 16:54:37]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;
int tr[N];  // n数的个数
int n, m, k;
int lowbit(int x) {
    return x & (-x);
}
void add(int i, int a) {  // 单点增加
    while (i <= n) {
        tr[i] += a;
        i += lowbit(i);
    }
}
int ask(int i) {  // 前缀和查询
    int ans = 0;
    while (i != 0) {
        ans += tr[i];
        i -= lowbit(i);
    }
    return ans;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m >> k;
        vector<int> pos(n + 1), query(n + 1);
        set<int> tool, inter;
        for (int i = 1; i <= n; i++) {
            int x;
            cin >> x;
            pos[x] = i;
            tr[i] = 0;
        }
        int last = 0;
        int fl = 0;
        for (int i = 1; i <= m; i++) {
            int x;
            cin >> x;
            if (pos[last] > pos[x]) {  // 判断顺序
                fl = 1;
            }
            query[x] = 1;
            last = x;
        }
        for (int i = 1; i <= k; i++) {
            int x;
            cin >> x;
            tool.insert(x);
        }
        if (fl) {
            //cout << "x" << endl;
            cout << "NO" << endl;
            continue;
        }
        inter.insert(0);
        inter.insert(n + 1);
        for (int i = n; i >= 1; i--) {
            if (query[i]) {  // 如果要留则直接放入区间
                inter.insert(pos[i]);
                continue;
            }
            auto r = inter.lower_bound(pos[i]);  // 找到右区间
            auto l = prev(r);                    // 找到左区间
            int L = *l, R = *r;
            int len = R - L - (ask(R) - ask(L)) - 1;
            auto it = tool.upper_bound(len);
            if (it == tool.begin()) {
                fl = 1;
                break;
            }
            it = prev(it);  // 找小于等于len的最大值
            tool.erase(it);
            add(pos[i], 1);
        }
        if (fl) {
            cout << "NO" << endl;
        } else {
            cout << "YES" << endl;
        }
    }
}
//4 1 4|3 2 4 1 |3 |4 3 2 1

详细

Test #1:

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

input:

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

output:

YES
YES
NO

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3540kb

input:

100
2 1 2
2 1
2
1 1
2 1 2
1 2
1
2 2
2 1 1
1 2
1
2
6 1 5
3 4 2 5 6 1
3
5 2 1 1 1
6 1 6
2 1 3 6 4 5
1
4 1 2 2 1 4
3 3 2
2 1 3
2 1 3
2 2
1 1 1
1
1
1
1 1 1
1
1
1
2 1 2
2 1
2
1 2
4 4 3
2 1 3 4
2 1 3 4
4 3 1
1 1 1
1
1
1
6 5 1
6 2 5 4 3 1
6 2 4 3 1
4
1 1 1
1
1
1
6 5 3
3 6 1 4 5 2
3 6 1 4 2
3 3 4
4 3 4
3 4 ...

output:

YES
YES
YES
NO
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
NO
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
NO
NO
YES
YES
YES
NO
NO
NO
NO
YES
NO
NO
YES
NO
YES
NO
YES
NO
NO
NO
YES
NO
YES
NO
NO
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES...

result:

wrong answer 4th lines differ - expected: 'YES', found: 'NO'