답안 #624247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624247 2022-08-07T14:27:18 Z Arinoor Watermelon (INOI20_watermelon) C++17
31 / 100
34 ms 8484 KB
#include <bits/stdc++.h>
using namespace std;

#define pb				push_back
#define mp				make_pair
#define fi				first
#define se				second
#define all(x)			x.begin(), x.end()
#define bug(x)			cout << #x << " : " << x << '\n'
#define ios				ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 1e9 + 10;

int a[maxn], b[maxn], d[maxn];
int vis[maxn];
vector<int> G[maxn];
priority_queue<int, vector<int>, greater<int>> Q;

void dfs(int v){
	vis[v] = 1;
	for(int u : G[v]){
		if(!vis[u])
			dfs(u);
		else if(vis[u] == 1){
			cout << "-1\n";
			exit(0);
		}
	}
	vis[v] = 2;
}

int main(){
	ios;
	int n, k;
	cin >> n >> k;
	if(k > 1)
		return 0;
	for(int i = 1; i <= n; i ++){
		cin >> b[i];
	}
	if(~b[n])
		return cout << "-1\n", 0;
	vector<pii> S;
	for(int i = n; i; i --){
		if(b[i] == -1){
			if(!S.empty()){
				G[S.front().fi].pb(i);
				d[i] ++;
			}
			S.clear();
			S.pb(mp(i, inf));
		}
		else{
			pii last = mp(0, 0);
			while(S.back().se < b[i]){
				last = S.back();
				S.pop_back();
			}
			if(b[i] != last.se + 1)
				return cout << "-1\n", 0;
			if(last.fi){
				G[last.fi].pb(i);
				d[i] ++;
			}
			G[i].pb(S.back().fi);
			d[S.back().fi] ++;
			if(S.back().se == b[i]){
				S.pop_back();
			}
			S.pb(mp(i, b[i]));
		}
	}
	for(int i = 1; i <= n; i ++)
		if(!vis[i])
			dfs(i);
	for(int i = 1; i <= n; i ++)
		if(!d[i])
			Q.push(i);
	for(int i = 1; i <= n; i ++){
		int ind = Q.top();
		Q.pop();
		a[ind] = i;
		for(int u : G[ind])
			if(!(--d[u]))
				Q.push(u);
	}
	for(int i = 1; i <= n; i ++)
		cout << a[i] << ' ';
	cout << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 3 ms 2684 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Incorrect 2 ms 2568 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 3 ms 2684 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Incorrect 2 ms 2568 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 5580 KB Output is correct
2 Correct 5 ms 3028 KB Output is correct
3 Correct 17 ms 5556 KB Output is correct
4 Correct 5 ms 3032 KB Output is correct
5 Correct 21 ms 5524 KB Output is correct
6 Correct 16 ms 5500 KB Output is correct
7 Correct 18 ms 5592 KB Output is correct
8 Correct 18 ms 5624 KB Output is correct
9 Correct 17 ms 5564 KB Output is correct
10 Correct 17 ms 5588 KB Output is correct
11 Correct 32 ms 8360 KB Output is correct
12 Correct 31 ms 8396 KB Output is correct
13 Correct 34 ms 8360 KB Output is correct
14 Correct 32 ms 8284 KB Output is correct
15 Correct 13 ms 3688 KB Output is correct
16 Correct 10 ms 3608 KB Output is correct
17 Correct 13 ms 3540 KB Output is correct
18 Correct 11 ms 3584 KB Output is correct
19 Correct 9 ms 3540 KB Output is correct
20 Correct 10 ms 3612 KB Output is correct
21 Correct 8 ms 3540 KB Output is correct
22 Correct 9 ms 3500 KB Output is correct
23 Correct 12 ms 3568 KB Output is correct
24 Correct 27 ms 8368 KB Output is correct
25 Correct 28 ms 8324 KB Output is correct
26 Correct 29 ms 8364 KB Output is correct
27 Correct 27 ms 8392 KB Output is correct
28 Correct 28 ms 8432 KB Output is correct
29 Correct 29 ms 8408 KB Output is correct
30 Correct 10 ms 4308 KB Output is correct
31 Correct 15 ms 5468 KB Output is correct
32 Correct 25 ms 7720 KB Output is correct
33 Correct 28 ms 8268 KB Output is correct
34 Correct 4 ms 3244 KB Output is correct
35 Correct 7 ms 3796 KB Output is correct
36 Correct 16 ms 5428 KB Output is correct
37 Correct 33 ms 8188 KB Output is correct
38 Correct 28 ms 8288 KB Output is correct
39 Correct 27 ms 8328 KB Output is correct
40 Correct 24 ms 8472 KB Output is correct
41 Correct 26 ms 8484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 5580 KB Output is correct
2 Correct 5 ms 3028 KB Output is correct
3 Correct 17 ms 5556 KB Output is correct
4 Correct 5 ms 3032 KB Output is correct
5 Correct 21 ms 5524 KB Output is correct
6 Correct 16 ms 5500 KB Output is correct
7 Correct 18 ms 5592 KB Output is correct
8 Correct 18 ms 5624 KB Output is correct
9 Correct 17 ms 5564 KB Output is correct
10 Correct 17 ms 5588 KB Output is correct
11 Correct 32 ms 8360 KB Output is correct
12 Correct 31 ms 8396 KB Output is correct
13 Correct 34 ms 8360 KB Output is correct
14 Correct 32 ms 8284 KB Output is correct
15 Correct 13 ms 3688 KB Output is correct
16 Correct 10 ms 3608 KB Output is correct
17 Correct 13 ms 3540 KB Output is correct
18 Correct 11 ms 3584 KB Output is correct
19 Correct 9 ms 3540 KB Output is correct
20 Correct 10 ms 3612 KB Output is correct
21 Correct 8 ms 3540 KB Output is correct
22 Correct 9 ms 3500 KB Output is correct
23 Correct 12 ms 3568 KB Output is correct
24 Correct 27 ms 8368 KB Output is correct
25 Correct 28 ms 8324 KB Output is correct
26 Correct 29 ms 8364 KB Output is correct
27 Correct 27 ms 8392 KB Output is correct
28 Correct 28 ms 8432 KB Output is correct
29 Correct 29 ms 8408 KB Output is correct
30 Correct 10 ms 4308 KB Output is correct
31 Correct 15 ms 5468 KB Output is correct
32 Correct 25 ms 7720 KB Output is correct
33 Correct 28 ms 8268 KB Output is correct
34 Correct 4 ms 3244 KB Output is correct
35 Correct 7 ms 3796 KB Output is correct
36 Correct 16 ms 5428 KB Output is correct
37 Correct 33 ms 8188 KB Output is correct
38 Correct 28 ms 8288 KB Output is correct
39 Correct 27 ms 8328 KB Output is correct
40 Correct 24 ms 8472 KB Output is correct
41 Correct 26 ms 8484 KB Output is correct
42 Incorrect 2 ms 2700 KB Output isn't correct
43 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2684 KB Output is correct
9 Correct 3 ms 2684 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Incorrect 2 ms 2568 KB Output isn't correct
13 Halted 0 ms 0 KB -