Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#251#5. 「WyOJ Round 1」炽 · 踏浪而歌jxy20121003742ms12668kbC++232.9kb2025-04-18 14:50:462025-04-18 18:01:57

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const int N = 200005;
const int INF = 0x3f3f3f3f;
int val[N << 2];
int a[N];
void build(int u, int l, int r) {
    if (l == r) {
        val[u] = a[l];
        return;
    }
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    val[u] = max(val[u << 1], val[u << 1 | 1]);
}
void update(int u, int l, int r, int x, int v) {
    if (l == r) {
        val[u] = v;
        return;
    }
    int mid = l + r >> 1;
    if (x <= mid) {
        update(u << 1, l, mid, x, v);
    } else {
        update(u << 1 | 1, mid + 1, r, x, v);
    }
    val[u] = max(val[u << 1], val[u << 1 | 1]);
}
int querymax() {
    return val[1];
}
int findmax(int u, int l, int r, int x) {
    if (val[u] < x) return -1;
    if (l == r) return l;
    int mid = l + r >> 1;
    if (val[u << 1] == x) {
        return findmax(u << 1, l, mid, x);
    } else {
        return findmax(u << 1 | 1, mid + 1, r, x);
    }
}
int n;
int findmaxidx() {
    return findmax(1, 1, n, val[1]);
}
int main() {
    cin >> n;
    ll s = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        s += a[i];
    }
    int mx = *max_element(a + 1, a + n + 1);
    int k = max((ll)mx, (s + 1) / 2);
    build(1, 1, n);
    set<int> pos;
    for (int i = 1; i <= n; i++) {
        if (a[i] > 0) pos.insert(i);
    }
    vector<pair<int, int>> ans;
    ll sum = s;
    for (int ss = 1; ss <= k; ss++) {
        int tmp = k - ss;
        int i = *pos.begin();
        vector<int> vec;
        vec.push_back(i);
        auto it = pos.begin();
        if (*it == i) {
            it++;
            if (it != pos.end()) vec.push_back(*it);
        }
        int pmax = findmaxidx();
        if (pmax != i) vec.push_back(pmax);
        sort(vec.begin(), vec.end());
        vec.erase(unique(vec.begin(), vec.end()), vec.end());
        bool ok = 0;
        for (auto j : vec) {
            int d = (i == j ? 1 : 2);
            int lai = a[i], laj = a[j];
            a[i]--;
            update(1, 1, n, i, a[i]);
            if (i != j) {
                a[j]--;
                update(1, 1, n, j, a[j]);
            }
            int now = querymax();
            ll nowsum = sum - d;
            if (now <= tmp && nowsum <= 2ll * tmp) {
                ans.push_back({i, j});
                sum = nowsum;
                if (a[i] == 0) pos.erase(i);
                if (i != j && a[j] == 0) pos.erase(j);
                ok = 1;
                break;
            }
            a[i] = lai;
            update(1, 1, n, i, lai);
            if (j != i) {
                a[j] = laj;
                update(1, 1, n, j, laj);
            }
        }
        assert(ok);
    }
    cout << (int)ans.size() << "\n";
    for (auto [x, y] : ans) {
        cout << x << " " << y << "\n";
    }
    return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 1ms
memory: 3404kb

input:

6
3 4 1 2 4 6

output:

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

result:

ok 21 numbers

Test #2:

score: 10
Accepted
time: 222ms
memory: 5216kb

input:

1000
1000 1000 1000 999 996 993 991 991 991 991 991 991 988 988 988 988 987 984 980 980 978 978 977 ...

output:

259517
1 1
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1...

result:

ok 519035 numbers

Test #3:

score: 10
Accepted
time: 249ms
memory: 7912kb

input:

5
93761 154479 466694 134030 151036

output:

500000
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1...

result:

ok 1000001 numbers

Test #4:

score: 10
Accepted
time: 533ms
memory: 10348kb

input:

56700
20 9 3 26 6 14 11 26 15 4 7 7 18 3 55 16 0 2 71 8 21 3 15 21 2 25 38 10 2 15 3 1 3 15 6 2 26 5...

output:

500000
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 3
1 3
1 3
1 4
1 4
1 4
1 4
1 4
1 4
1 4
1 4
4 5
4 5
4 5
4...

result:

ok 1000001 numbers

Test #5:

score: 10
Accepted
time: 471ms
memory: 9320kb

input:

18574
19 245 80 2 53 157 18 27 35 19 109 14 30 97 26 10 5 22 142 102 13 114 67 70 142 131 10 177 18 ...

output:

500000
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
2 3
2 3
2 3
2 3
2...

result:

ok 1000001 numbers

Test #6:

score: 10
Accepted
time: 499ms
memory: 10200kb

input:

36045
6 62 19 5 21 38 42 13 52 3 10 4 14 65 24 15 9 21 19 24 1 3 2 59 2 19 80 28 9 39 49 26 20 20 14...

output:

500000
1 2
1 2
1 2
1 2
1 2
1 2
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2...

result:

ok 1000001 numbers

Test #7:

score: 10
Accepted
time: 336ms
memory: 8676kb

input:

100
442 37503 827 703 10394 1215 23599 313 1705 10819 6152 89 341 3448 778 7153 20397 5166 2735 2100...

output:

500000
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1...

result:

ok 1000001 numbers

Test #8:

score: 10
Accepted
time: 391ms
memory: 8256kb

input:

1000
6 436 820 182 327 3272 993 1042 321 2717 69 4321 706 4565 1280 528 1632 1317 916 125 262 1712 6...

output:

500000
1 2
1 2
1 2
1 2
1 2
1 2
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2...

result:

ok 1000001 numbers

Test #9:

score: 10
Accepted
time: 449ms
memory: 8348kb

input:

10000
6 107 38 107 3 28 89 15 30 19 89 2 435 75 45 55 119 125 15 42 101 83 9 134 3 278 292 55 255 2 ...

output:

500000
1 2
1 2
1 2
1 2
1 2
1 2
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2 3
2...

result:

ok 1000001 numbers

Test #10:

score: 10
Accepted
time: 591ms
memory: 12668kb

input:

100000
3 3 62 19 5 21 0 17 21 2 1 36 3 11 2 3 10 22 17 3 10 0 4 3 11 20 11 22 12 12 12 1 10 4 9 6 4 ...

output:

500000
1 2
1 2
1 2
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 4
3 5
3...

result:

ok 1000001 numbers

Extra Test:

score: 0
Extra Test Passed