QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#717656#1873. Arrayjuan_123AC ✓276ms13840kbC++142.8kb2024-11-06 18:37:302024-11-06 18:37:30

Judging History

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

  • [2024-11-06 18:37:30]
  • 评测
  • 测评结果:AC
  • 用时:276ms
  • 内存:13840kb
  • [2024-11-06 18:37:30]
  • 提交

answer

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int t, n, to[500005], col[500005], a[500005], b[500005], pre[500005], flg, fflg, cnt, vis[500005], aans;
inline void recheck() {
	for (int i = 1; i <= cnt; i++) vis[i] = 0;
	aans = 0;
	int rr = 0;
	//	cout << cnt << ' ' << aans << ' ' << flg << "@\n";
	for (int i = 1; i <= n; i++) {
		if (!col[i]) flg = 0;
		while (aans < cnt && rr != n) {
			rr++;
			vis[col[rr]]++;
			if (vis[col[rr]] == 1) aans++;
		}
		//		cout << rr << ' ' << aans << ' ' << i << "**\n";
		if (a[i] == n + 1) {
			if (aans == cnt) flg = 0;
		} else if (a[i] != rr || aans != cnt) {
			flg = 0;
		}
		vis[col[i]]--;
		if (!vis[col[i]]) aans--;
	}
}
inline bool check(int p) {
	int now = p - 1;
	for (int i = 1; i <= n; i++) {
		if (!pre[i] && !col[i] && !now) return false;
		if (to[i]) now--;
		if (pre[i]) now++;
	}
	return true;
}
inline int gets() {
	int l = 1, r = a[1] + 1;
	int mid = (l + r) >> 1;
	while (l < r) {
		mid = (l + r) >> 1;
		if (check(mid)) r = mid;
		else l = mid + 1;
	}
	return l;
}
signed main() {
	//freopen("sing.in", "r", stdin);
	//freopen("sing.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t--) {
		cin >> n;
		for (int i = 1; i <= n; i++) col[i] = pre[i] = to[i] = 0;
		flg = 0, fflg = 1;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			if (a[i] == i) flg = 1;
			else fflg = 0;
			if (i ^ 1 && a[i] != a[i - 1]) {
				pre[a[i]] = i - 1;
				to[i - 1] = a[i];
			}
		}
		if (flg || a[1] == n + 1 || pre[a[1]]) {
			if (!fflg || a[1] == n + 1 || pre[a[1]]) {
				cout << "-1\n";
			} else {
			//	cout << "YES\n";
				for (int i = 1; i <= n; i++) cout << 1 << " ";
				cout << "\n";
			}
			continue;
		}
		//		for (int i = 1; i <= n; i++) cout << pre[i] << " " << to[i] << "&&\n";
		pre[a[1]] = a[1];
		int op = gets();
		//		cout << op << '*';

		if (op > a[1]) {
			cout << "-1\n";
			continue;
		}
		pre[a[1]] = 0;
		flg = 1;
		int rr = 0;
		set<pair<int, int>> s;
		col[a[1]] = op;
		for (int i = 1; i < op; i++) s.insert(make_pair(0, i)) ;
		s.insert(make_pair(a[1], op));
		cnt = 0;
		cnt = op;
		for (int i = 1; i <= n; i++) {
			if (pre[i]) {
				col[i] = col[pre[i]];
				s.insert(make_pair(i, col[i]));
			}
			if (!col[i]) {
				pair<int, int> ops = (*s.begin());
				s.erase(s.begin());
				col[i] = ops.second;
				s.insert(make_pair(i, col[i]));
			}

			if (to[i]) {
				s.erase(make_pair(i, col[i]));
			}

		}
		//		for (int i = 1; i <= n; i++) {
		//			cout << col[i] << " " << i << " " << pre[i] << " " << to[i] << ' ' << a[i] << "!\n";
		//		}
		//		cout << flg << "||\n";
		recheck();
		if (!flg) {
			cout << "-1\n";
		} else {
	//		cout << "YES\n";
			for (int i = 1; i <= n; i++) cout << col[i] << ' ';
			cout << "\n";
		}
	}
}

詳細信息

Test #1:

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

input:

3
4
3 3 5 5
7
4 6 6 7 8 8 8
5
2 3 4 4 6

output:

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

result:

ok 233

Test #2:

score: 0
Accepted
time: 176ms
memory: 12216kb

input:

23198
7
1 2 3 4 5 6 7
7
1 2 3 4 5 6 8
7
1 2 3 4 5 7 7
7
1 2 3 4 5 7 8
7
1 2 3 4 5 8 8
7
1 2 3 4 6 6 7
7
1 2 3 4 6 6 8
7
1 2 3 4 6 7 7
7
1 2 3 4 6 7 8
7
1 2 3 4 6 8 8
7
1 2 3 4 7 7 7
7
1 2 3 4 7 7 8
7
1 2 3 4 7 8 8
7
1 2 3 4 8 8 8
7
1 2 3 5 5 6 7
7
1 2 3 5 5 6 8
7
1 2 3 5 5 7 7
7
1 2 3 5 5 7 8
7
1 2 ...

output:

1 1 1 1 1 1 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 233

Test #3:

score: 0
Accepted
time: 139ms
memory: 11736kb

input:

2000
1000
964 987 987 989 992 999 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1...

output:

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

result:

ok 233

Test #4:

score: 0
Accepted
time: 145ms
memory: 11780kb

input:

16
100000
44700 98571 99279 99995 99998 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 100001 1...

output:

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

result:

ok 233

Test #5:

score: 0
Accepted
time: 153ms
memory: 11812kb

input:

1006
1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok 233

Test #6:

score: 0
Accepted
time: 276ms
memory: 13840kb

input:

106
20000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok 233

Test #7:

score: 0
Accepted
time: 186ms
memory: 13824kb

input:

58796
10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 11
10
1 2 3 4 5 6 7 8 10 10
10
1 2 3 4 5 6 7 8 10 11
10
1 2 3 4 5 6 7 8 11 11
10
1 2 3 4 5 6 7 9 9 10
10
1 2 3 4 5 6 7 9 9 11
10
1 2 3 4 5 6 7 9 10 10
10
1 2 3 4 5 6 7 9 10 11
10
1 2 3 4 5 6 7 9 11 11
10
1 2 3 4 5 6 7 10 10 10
10
1 2 3 4 5 6 7 10 10...

output:

1 1 1 1 1 1 1 1 1 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 233