답안 #497886

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497886 2021-12-24T04:12:12 Z Ziel Nice sequence (IZhO18_sequence) C++17
76 / 100
2000 ms 52116 KB
/**
 * LES GREATEABLES BRO TEAM
**/
 
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = true;
void setIO(const string &f = "");
 
#define int ll
const int N = 4e5 + 156;
int was[N], visited[N];
vector<int> g[N], order;
 
bool dfs(int v) {
	was[v] = 1;
	for (int to: g[v]) {
		if (!was[to]) {
			if (dfs(to))
				return true;
		} else if (was[to] == 1)
			return true;
	}
	was[v] = 2;
	return false;
}
void dfs2(int v) {
	visited[v] = 1;
	for (int to: g[v]) {
		if (!visited[to])
			dfs2(to);
	}
	order.push_back(v);
}
 
void solve() {
    int n, m;
    cin >> n >> m;

    int lo = min(n, m) - 2, hi = n + m, res = -1;
    while (lo <= hi) {
    	int mid = (lo + hi) / 2;
 
		for (int i = n; i <= mid; i++)
			g[i].push_back(i - n);
		for (int i = m; i <= mid; i++)
			g[i - m].push_back(i);
 
		bool have_cycle = false;
		for (int i = 0; i <= mid; i++) {
			if (!was[i] && dfs(i)) {
				have_cycle = true;
				break;
			}
		}
		for (int i = 0; i <= mid; i++) {
			was[i] = 0;
			if (sz(g[i])) g[i].clear();
		}
 
		if (have_cycle) {
			hi = mid - 1;
		} else {
			lo = mid + 1;
			res = mid;
		}
    }
 
    cout << res << '\n';
    
    for (int i = n; i <= res; i++)
		g[i].push_back(i - n);
	for (int i = m; i <= res; i++)
		g[i - m].push_back(i);
    for (int i = 0; i <= res; i++) {
    	if (!visited[i])
    		dfs2(i);
    }
    reverse(order.begin(), order.end());
    vector<int> p(sz(order));
    for (int i = 0; i < sz(order); i++) {
    	p[order[i]] = i;
   	}
   	int x = p[0];
   	for (int i = 0; i < sz(p); i++)
   		p[i] -= x;
   	for (int i = 1; i < sz(p); i++)
   		cout << p[i] - p[i - 1] << ' ';
 
   	order.clear();
   	for (int i = 0; i <= res; i++) {
		was[i] = 0;
		visited[i] = 0;
		if (sz(g[i])) g[i].clear();
	}
    cout << '\n';
}
/*
0 2
1 3
3 0
 
2 1 3 0
*/
 
signed main() {
    setIO();
    
    int tt = 1;
    if (FLAG) {
    	cin >> tt;
    }
    while (tt--) {
    	solve();
    }
    
    return 0;
}
 
void setIO(const string &f) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen((f + ".in").c_str(), "r")) {
        freopen((f + ".in").c_str(), "r", stdin);
        freopen((f + ".out").c_str(), "w", stdout);
    }
}

Compilation message

sequence.cpp: In function 'void setIO(const string&)':
sequence.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9712 KB Ok
2 Correct 6 ms 9676 KB Ok
3 Correct 6 ms 9720 KB Ok
4 Correct 6 ms 9676 KB Ok
5 Correct 6 ms 9668 KB Ok
6 Correct 6 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 6 ms 9696 KB Ok
9 Correct 5 ms 9720 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9724 KB Ok
12 Correct 6 ms 9716 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Ok
2 Correct 5 ms 9716 KB Ok
3 Correct 5 ms 9720 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 6 ms 9676 KB Ok
6 Correct 9 ms 9972 KB Ok
7 Correct 23 ms 10992 KB Ok
8 Correct 13 ms 10268 KB Ok
9 Correct 30 ms 11336 KB Ok
10 Correct 18 ms 10572 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 6 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 5 ms 9716 KB Ok
6 Correct 5 ms 9676 KB Ok
7 Correct 5 ms 9716 KB Ok
8 Correct 7 ms 9804 KB Ok
9 Correct 6 ms 9676 KB Ok
10 Correct 5 ms 9696 KB Ok
11 Correct 5 ms 9676 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9612 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 299 ms 30524 KB Ok
7 Correct 245 ms 32968 KB Ok
8 Correct 476 ms 36124 KB Ok
9 Correct 377 ms 34396 KB Ok
10 Correct 214 ms 24152 KB Ok
11 Correct 408 ms 36060 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9712 KB Ok
2 Correct 6 ms 9676 KB Ok
3 Correct 6 ms 9720 KB Ok
4 Correct 6 ms 9676 KB Ok
5 Correct 6 ms 9668 KB Ok
6 Correct 6 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 6 ms 9696 KB Ok
9 Correct 5 ms 9720 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9724 KB Ok
12 Correct 6 ms 9716 KB Ok
13 Correct 5 ms 9676 KB Ok
14 Correct 6 ms 9676 KB Ok
15 Correct 5 ms 9676 KB Ok
16 Correct 5 ms 9676 KB Ok
17 Correct 5 ms 9716 KB Ok
18 Correct 5 ms 9676 KB Ok
19 Correct 5 ms 9716 KB Ok
20 Correct 7 ms 9804 KB Ok
21 Correct 6 ms 9676 KB Ok
22 Correct 5 ms 9696 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 10 ms 9972 KB Ok
25 Correct 8 ms 9932 KB Ok
26 Correct 8 ms 9932 KB Ok
27 Correct 8 ms 9980 KB Ok
28 Correct 7 ms 9928 KB Ok
29 Correct 7 ms 9828 KB Ok
30 Correct 8 ms 9936 KB Ok
31 Correct 8 ms 9932 KB Ok
32 Correct 8 ms 9932 KB Ok
33 Correct 9 ms 9976 KB Ok
34 Correct 14 ms 10260 KB Ok
35 Correct 14 ms 10196 KB Ok
36 Correct 13 ms 10228 KB Ok
37 Correct 13 ms 10316 KB Ok
38 Correct 15 ms 10316 KB Ok
39 Correct 13 ms 10188 KB Ok
40 Correct 14 ms 10208 KB Ok
41 Correct 13 ms 10228 KB Ok
42 Correct 13 ms 10308 KB Ok
43 Correct 15 ms 10232 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9712 KB Ok
2 Correct 6 ms 9676 KB Ok
3 Correct 6 ms 9720 KB Ok
4 Correct 6 ms 9676 KB Ok
5 Correct 6 ms 9668 KB Ok
6 Correct 6 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 6 ms 9696 KB Ok
9 Correct 5 ms 9720 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9724 KB Ok
12 Correct 6 ms 9716 KB Ok
13 Correct 6 ms 9676 KB Ok
14 Correct 5 ms 9716 KB Ok
15 Correct 5 ms 9720 KB Ok
16 Correct 5 ms 9676 KB Ok
17 Correct 6 ms 9676 KB Ok
18 Correct 9 ms 9972 KB Ok
19 Correct 23 ms 10992 KB Ok
20 Correct 13 ms 10268 KB Ok
21 Correct 30 ms 11336 KB Ok
22 Correct 18 ms 10572 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 6 ms 9676 KB Ok
25 Correct 5 ms 9676 KB Ok
26 Correct 5 ms 9676 KB Ok
27 Correct 5 ms 9716 KB Ok
28 Correct 5 ms 9676 KB Ok
29 Correct 5 ms 9716 KB Ok
30 Correct 7 ms 9804 KB Ok
31 Correct 6 ms 9676 KB Ok
32 Correct 5 ms 9696 KB Ok
33 Correct 5 ms 9676 KB Ok
34 Correct 10 ms 9972 KB Ok
35 Correct 8 ms 9932 KB Ok
36 Correct 8 ms 9932 KB Ok
37 Correct 8 ms 9980 KB Ok
38 Correct 7 ms 9928 KB Ok
39 Correct 7 ms 9828 KB Ok
40 Correct 8 ms 9936 KB Ok
41 Correct 8 ms 9932 KB Ok
42 Correct 8 ms 9932 KB Ok
43 Correct 9 ms 9976 KB Ok
44 Correct 14 ms 10260 KB Ok
45 Correct 14 ms 10196 KB Ok
46 Correct 13 ms 10228 KB Ok
47 Correct 13 ms 10316 KB Ok
48 Correct 15 ms 10316 KB Ok
49 Correct 13 ms 10188 KB Ok
50 Correct 14 ms 10208 KB Ok
51 Correct 13 ms 10228 KB Ok
52 Correct 13 ms 10308 KB Ok
53 Correct 15 ms 10232 KB Ok
54 Correct 144 ms 17536 KB Ok
55 Correct 172 ms 17968 KB Ok
56 Correct 165 ms 18088 KB Ok
57 Correct 113 ms 16768 KB Ok
58 Correct 175 ms 18048 KB Ok
59 Correct 149 ms 17320 KB Ok
60 Correct 139 ms 16800 KB Ok
61 Correct 114 ms 17124 KB Ok
62 Correct 199 ms 18284 KB Ok
63 Correct 159 ms 17076 KB Ok
64 Correct 180 ms 17912 KB Ok
65 Correct 179 ms 17840 KB Ok
66 Correct 132 ms 17392 KB Ok
67 Correct 113 ms 17156 KB Ok
68 Correct 174 ms 17536 KB Ok
69 Correct 883 ms 26896 KB Ok
70 Correct 924 ms 27196 KB Ok
71 Correct 913 ms 26860 KB Ok
72 Correct 806 ms 26680 KB Ok
73 Correct 873 ms 27152 KB Ok
74 Correct 885 ms 26616 KB Ok
75 Correct 739 ms 26700 KB Ok
76 Correct 989 ms 26908 KB Ok
77 Correct 875 ms 26776 KB Ok
78 Correct 1058 ms 26636 KB Ok
79 Correct 876 ms 27140 KB Ok
80 Correct 843 ms 26704 KB Ok
81 Correct 721 ms 27112 KB Ok
82 Correct 830 ms 26792 KB Ok
83 Correct 858 ms 27108 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9712 KB Ok
2 Correct 6 ms 9676 KB Ok
3 Correct 6 ms 9720 KB Ok
4 Correct 6 ms 9676 KB Ok
5 Correct 6 ms 9668 KB Ok
6 Correct 6 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 6 ms 9696 KB Ok
9 Correct 5 ms 9720 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9724 KB Ok
12 Correct 6 ms 9716 KB Ok
13 Correct 6 ms 9676 KB Ok
14 Correct 5 ms 9716 KB Ok
15 Correct 5 ms 9720 KB Ok
16 Correct 5 ms 9676 KB Ok
17 Correct 6 ms 9676 KB Ok
18 Correct 9 ms 9972 KB Ok
19 Correct 23 ms 10992 KB Ok
20 Correct 13 ms 10268 KB Ok
21 Correct 30 ms 11336 KB Ok
22 Correct 18 ms 10572 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 6 ms 9676 KB Ok
25 Correct 5 ms 9676 KB Ok
26 Correct 5 ms 9676 KB Ok
27 Correct 5 ms 9716 KB Ok
28 Correct 5 ms 9676 KB Ok
29 Correct 5 ms 9716 KB Ok
30 Correct 7 ms 9804 KB Ok
31 Correct 6 ms 9676 KB Ok
32 Correct 5 ms 9696 KB Ok
33 Correct 5 ms 9676 KB Ok
34 Correct 5 ms 9676 KB Ok
35 Correct 5 ms 9676 KB Ok
36 Correct 5 ms 9676 KB Ok
37 Correct 5 ms 9612 KB Ok
38 Correct 5 ms 9676 KB Ok
39 Correct 299 ms 30524 KB Ok
40 Correct 245 ms 32968 KB Ok
41 Correct 476 ms 36124 KB Ok
42 Correct 377 ms 34396 KB Ok
43 Correct 214 ms 24152 KB Ok
44 Correct 408 ms 36060 KB Ok
45 Correct 10 ms 9972 KB Ok
46 Correct 8 ms 9932 KB Ok
47 Correct 8 ms 9932 KB Ok
48 Correct 8 ms 9980 KB Ok
49 Correct 7 ms 9928 KB Ok
50 Correct 7 ms 9828 KB Ok
51 Correct 8 ms 9936 KB Ok
52 Correct 8 ms 9932 KB Ok
53 Correct 8 ms 9932 KB Ok
54 Correct 9 ms 9976 KB Ok
55 Correct 14 ms 10260 KB Ok
56 Correct 14 ms 10196 KB Ok
57 Correct 13 ms 10228 KB Ok
58 Correct 13 ms 10316 KB Ok
59 Correct 15 ms 10316 KB Ok
60 Correct 13 ms 10188 KB Ok
61 Correct 14 ms 10208 KB Ok
62 Correct 13 ms 10228 KB Ok
63 Correct 13 ms 10308 KB Ok
64 Correct 15 ms 10232 KB Ok
65 Correct 144 ms 17536 KB Ok
66 Correct 172 ms 17968 KB Ok
67 Correct 165 ms 18088 KB Ok
68 Correct 113 ms 16768 KB Ok
69 Correct 175 ms 18048 KB Ok
70 Correct 149 ms 17320 KB Ok
71 Correct 139 ms 16800 KB Ok
72 Correct 114 ms 17124 KB Ok
73 Correct 199 ms 18284 KB Ok
74 Correct 159 ms 17076 KB Ok
75 Correct 180 ms 17912 KB Ok
76 Correct 179 ms 17840 KB Ok
77 Correct 132 ms 17392 KB Ok
78 Correct 113 ms 17156 KB Ok
79 Correct 174 ms 17536 KB Ok
80 Correct 883 ms 26896 KB Ok
81 Correct 924 ms 27196 KB Ok
82 Correct 913 ms 26860 KB Ok
83 Correct 806 ms 26680 KB Ok
84 Correct 873 ms 27152 KB Ok
85 Correct 885 ms 26616 KB Ok
86 Correct 739 ms 26700 KB Ok
87 Correct 989 ms 26908 KB Ok
88 Correct 875 ms 26776 KB Ok
89 Correct 1058 ms 26636 KB Ok
90 Correct 876 ms 27140 KB Ok
91 Correct 843 ms 26704 KB Ok
92 Correct 721 ms 27112 KB Ok
93 Correct 830 ms 26792 KB Ok
94 Correct 858 ms 27108 KB Ok
95 Correct 398 ms 30688 KB Ok
96 Correct 754 ms 40920 KB Ok
97 Correct 708 ms 35208 KB Ok
98 Correct 444 ms 34212 KB Ok
99 Correct 547 ms 33952 KB Ok
100 Correct 621 ms 34396 KB Ok
101 Correct 628 ms 37256 KB Ok
102 Correct 586 ms 35572 KB Ok
103 Correct 726 ms 37056 KB Ok
104 Correct 726 ms 38720 KB Ok
105 Correct 778 ms 41968 KB Ok
106 Correct 567 ms 38936 KB Ok
107 Correct 635 ms 38440 KB Ok
108 Correct 822 ms 40520 KB Ok
109 Correct 601 ms 40512 KB Ok
110 Execution timed out 2064 ms 52116 KB Time limit exceeded
111 Halted 0 ms 0 KB -