0pts 玄关求助
查看原帖
0pts 玄关求助
741732
small_Dongpo楼主2024/4/13 17:32

请大佬改我的代码而不要发自己的代码,否则思路不一样本蒟蒻看不懂QAQ

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long l;
string s;
l n, cnt = 0, a[200005], out[200005][2];
bool b[200005];

struct Node {
    l a, b, x;
    bool operator<(const Node & a) const {
        return x > a.x;
    }
};
priority_queue<Node> q;

int main() {
    scanf("%lld", &n);
    cin >> s;
    s = ' ' + s;
    for (l i = 1; i <= n; ++i) {
        scanf("%lld", &a[i]);
    }
    for (l i = 1; i < n; ++i) {
        if (s[i] != s[i + 1]) q.push({i, i + 1, abs(a[i] - a[i + 1])});
    }
    while (!q.empty()) {
        Node x = q.top();
        q.pop();
        if (b[x.a] || b[x.b]) continue;
        if (x.a > 1 && x.b < n && s[x.a - 1] != s[x.b + 1]) {
            q.push({x.a - 1, x.b + 1, abs(a[x.a - 1] - a[x.b + 1])});
        }
        out[++cnt][0] = x.a;
        out[cnt][1] = x.b;
        b[x.a] = b[x.b] = 1;
    }
    printf("%lld\n", cnt);
    for (l i = 1; i <= cnt; ++i) {
        printf("%lld %lld\n", out[i][0], out[i][1]);
    }
    return 0;
}
2024/4/13 17:32
加载中...