QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#816369#9865. DollspropaneWA 7ms3564kbC++202.1kb2024-12-16 10:08:142024-12-16 10:08:15

Judging History

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

  • [2025-01-08 13:49:40]
  • hack成功,自动添加数据
  • (/hack/1434)
  • [2024-12-16 10:08:15]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:3564kb
  • [2024-12-16 10:08:14]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<array>
using namespace std;
using LL = long long;

const int maxn = 1e5 + 5;
int p[maxn], l[maxn], r[maxn], a[maxn], L[maxn], R[maxn];

void init(int n){
    for(int i = 1; i <= n; i++){
        p[i] = L[i] = R[i] = i;
        l[i] = r[i] = a[i];
    }
}

int find(int x){
    return p[x] == x ? x : p[x] = find(p[x]);
}

void merge(int x, int y){
    x = find(x), y = find(y);
    if (x != y){
        p[x] = y;
        l[y] = min(l[y], l[x]);
        r[y] = max(r[y], r[x]);
        L[y] = min(L[y], L[x]);
        R[y] = max(R[y], R[x]);
    }
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    const int INF = 1e9;
    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++) cin >> a[i];
        init(n);
        auto get = [&](int x, int y){
            if (find(x) == find(y)) return INF;
            if (r[find(x)] > r[find(y)]) swap(x, y);
            if (r[find(x)] < l[find(y)]) return r[find(y)] - l[find(x)];
            return INF;
        };

        int ans = 0;
        using P = array<int, 3>;
        priority_queue<P, vector<P>, greater<P> > q;
        for(int i = 1; i + 1 <= n; i++){
            int cost = get(i, i + 1);
            if (cost != INF) q.push({cost, i, i + 1});
        }
        while(!q.empty()){
            auto [cost, x, y] = q.top();
            q.pop();
            if (get(x, y) == cost){
                ans += 1;
                merge(x, y);
                if (L[find(x)] - 1 >= 1){
                    int cost = get(L[find(x)] - 1, x);
                    if (cost != INF) q.push({cost, L[find(x)] - 1, x});
                }
                if (R[find(x)] + 1 <= n){
                    int cost = get(R[find(x)] + 1, x);
                    if (cost != INF) q.push({cost, R[find(x)] + 1, x});
                }
            }
        }
        cout << ans << '\n';
    }

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3564kb

input:

8
4
2 1 4 3
4
1 4 2 3
4
3 1 4 2
5
1 3 5 2 4
5
1 4 2 5 3
5
2 5 3 1 4
6
1 3 6 5 2 4
6
2 5 1 3 6 4

output:

3
3
2
3
3
3
4
4

result:

ok 8 numbers

Test #2:

score: -100
Wrong Answer
time: 7ms
memory: 3520kb

input:

5913
1
1
2
1 2
2
2 1
3
1 2 3
3
1 3 2
3
2 1 3
3
2 3 1
3
3 1 2
3
3 2 1
4
1 2 3 4
4
1 2 4 3
4
1 3 2 4
4
1 3 4 2
4
1 4 2 3
4
1 4 3 2
4
2 1 3 4
4
2 1 4 3
4
2 3 1 4
4
2 3 4 1
4
2 4 1 3
4
2 4 3 1
4
3 1 2 4
4
3 1 4 2
4
3 2 1 4
4
3 2 4 1
4
3 4 1 2
4
3 4 2 1
4
4 1 2 3
4
4 1 3 2
4
4 2 1 3
4
4 2 3 1
4
4 3 1 2
4...

output:

0
1
1
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
2
3
3
2
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
3
3
4
3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
3
4
3
3
4
4
3
4
3
3
3
4
3
4
4
4
3
3
3
3
4
4
4
4
3
4
4
3
4
4
4
4
3
3
3
3
4
4
4
3
4
3
3
3
4
3
4
4
3
3
4
3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
3
4
3
3
4
4
4
4
4
4
4
...

result:

wrong answer 45th numbers differ - expected: '4', found: '3'