Logo Wy Online Judge

WyOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#264#5. 「WyOJ Round 1」炽 · 踏浪而歌cxm10241001731ms10932kbC++231.5kb2025-04-18 14:59:322025-04-18 18:02:59

answer

#include <bits/stdc++.h>
#define deb cout << "in " << __LINE__ << "\t: "
using namespace std;
int n, a[100010], sum = 0, cnt[1000010], mx;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	priority_queue<int, vector<int>, greater<int>> s;
	for (int i = 1; i <= n; i++) {
		cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);
		if (a[i]) s.push(i), sum += a[i];
	}
	vector<pair<int, int>> ans;
	if (sum % 2) {
		if (mx * 2 > sum) {
			for (int i = 1; i <= n; i++) {
				if (a[i] == mx) {
					ans.push_back({i, i});
					cnt[a[i]]--, cnt[--a[i]]++, sum--;
					while (!cnt[mx]) mx--;
					break;
				}
			}
		}
		else {
			for (int i = 1; i <= n; i++)
				if (a[i]) {
					ans.push_back({i, i});
					cnt[a[i]]--, cnt[--a[i]]++, sum--;
					while (!cnt[mx]) mx--;
					break;
				}
		}
	}
	while (!s.empty() && mx * 2 < sum) {
		int x = s.top(); s.pop();
		int y = s.top(); s.pop();
		cnt[a[x]]--, cnt[--a[x]]++;
		cnt[a[y]]--, cnt[--a[y]]++;
		ans.push_back({x, y});
		sum -= 2;
		while (!cnt[mx]) mx--;
		if (a[x]) s.push(x);
		if (a[y]) s.push(y);
	}
	if (mx) {
		for (int i = 1; i <= n; i++)
			if (a[i] == mx) {
				for (int j = 1; j <= n; j++) {
					if (j == i) continue;
					while (a[j]--)
						ans.push_back({min(j, i), max(j, i)}), a[i]--;
				}
				while (a[i]--) ans.push_back({i, i});
				break;
			}
	}
	sort(ans.begin(), ans.end());
	cout << ans.size() << "\n";
	for (auto [x, y] : ans)
		cout << x << " " << y << "\n";
	return 0;
}

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

Details

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

Test #1:

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

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: 111ms
memory: 5020kb

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: 124ms
memory: 10792kb

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: 224ms
memory: 9180kb

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: 222ms
memory: 8784kb

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: 221ms
memory: 10932kb

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: 187ms
memory: 8800kb

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: 199ms
memory: 10580kb

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: 202ms
memory: 8008kb

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: 240ms
memory: 8244kb

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