求助,用stl,T了四个点
  • 板块P5462 X龙珠
  • 楼主TheZealous
  • 当前回复4
  • 已保存回复4
  • 发布时间2021/9/15 09:22
  • 上次更新2023/11/4 06:45:22
查看原帖
求助,用stl,T了四个点
375736
TheZealous楼主2021/9/15 09:22

如题.

#include <bits/stdc++.h>
using namespace std;
int n;
int a[100001];
bool mark[100001];
priority_queue<int> q;
queue<int> w;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        q.push(a[i]);
    }
    while (!q.empty()) {
        int x = q.top();
        q.pop();
        if (mark[x])
            continue;
        int k = 0;
        for (int i = 1; i < n; ++i) {
            if (a[i] == x) {
                for (int j = i + 1; j <= n; ++j) {
                    if (a[j] == 0)
                        continue;
                    if (mark[a[j]] == 0) {
                        k = a[j];
                        a[j] = 0;
                        break;
                    }
                }
            }
            if(k!=0) break;
        }
        if (k != 0) {
            mark[x] = 1;
            mark[k] = 1;
            w.push(x);
            w.push(k);
        }
    }
    while (!w.empty()) {
        printf("%d ", w.front());  // queue无top,有front
        w.pop();
    }
    return 0;
}
/*
4
3 1 4 2
*/

2021/9/15 09:22
加载中...