QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376363#6545. Connect the DotsMine_KingWA 0ms3888kbC++142.0kb2024-04-04 08:43:442024-04-04 08:43:45

Judging History

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

  • [2024-04-04 08:43:45]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3888kb
  • [2024-04-04 08:43:44]
  • 提交

answer

// 君の人生は月明かりだ、有りがちだなんて言わせるものか
// Think twice, code once.
#include <set>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define eputchar(c) putc(c, stderr)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define eputs(str) fputs(str, stderr), putc('\n', stderr)
using namespace std;

int T, n, m, a[200005], b[200005];
set<int> s, ok;
vector<pair<int, int>> ans;

int nxt(int x) {return *s.upper_bound(x);}
int pre(int x) {return *prev(s.lower_bound(x));}

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d", &n, &m);
		set<int>().swap(s), set<int>().swap(ok);
		vector<pair<int, int>>().swap(ans);
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[a[i]]++, s.insert(i);
		for (int i = 1; i < n; i++)
			if (a[i] != a[i + 1]) ans.emplace_back(i, i + 1);
		for (int i = 2; i < n; i++)
			if (a[i - 1] != a[i + 1]) ok.insert(i);
		while (s.size() > 2) {
			if (ok.empty()) {
				int pos = *next(s.begin());
				int P = pre(pos), N = nxt(pos);
				if (P != 1 && a[pre(P)] != a[N]) ok.insert(P);
				else ok.erase(P);
				if (N != n && a[P] != a[nxt(N)]) ok.insert(N);
				else ok.erase(N);
				s.erase(pos);
				b[a[pos]]--;
			}
			int pos = *ok.begin();
			ok.erase(pos);
			if (b[a[pos]] == 1) {
				while (pre(pos) != 1) ans.emplace_back(pre(pre(pos)), pos), s.erase(pre(pos));
				while (nxt(pos) != n) ans.emplace_back(pos, nxt(nxt(pos))), s.erase(nxt(pos));
				if (a[1] != a[n]) ans.emplace_back(1, n);
				break;
			}
			int P = pre(pos), N = nxt(pos);
			ans.emplace_back(P, N);
			if (P != 1 && a[pre(P)] != a[N]) ok.insert(P);
			else ok.erase(P);
			if (N != n && a[P] != a[nxt(N)]) ok.insert(N);
			else ok.erase(N);
			s.erase(pos);
			b[a[pos]]--;
		}
		printf("%d\n", (int)ans.size());
		for (auto i : ans) printf("%d %d\n", i.first, i.second);
		for (int i = 1; i <= n; i++) b[a[i]] = 0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3872kb

input:

3
4 2
1 1 2 2
4 2
1 2 1 2
3 3
1 2 3

output:

3
2 3
1 3
1 4
4
1 2
2 3
3 4
1 4
3
1 2
2 3
1 3

result:

ok all 3 test passed

Test #2:

score: 0
Accepted
time: 0ms
memory: 3888kb

input:

1
2 2
1 2

output:

1
1 2

result:

ok all 1 test passed

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3804kb

input:

10
5 2
2 2 2 1 2
5 2
2 1 2 1 2
5 2
1 2 2 2 1
5 2
2 1 2 1 1
5 2
1 1 1 2 1
5 2
1 2 2 1 2
5 2
2 1 1 2 2
5 2
2 2 2 1 1
5 2
1 1 2 1 2
5 2
1 2 2 2 1

output:

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

result:

wrong answer output = 5, answer = 4.