답안 #497794

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497794 2021-12-23T20:13:58 Z Ziel Nice sequence (IZhO18_sequence) C++17
76 / 100
2000 ms 51972 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 = 0, 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: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 + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 7 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9676 KB Ok
11 Correct 5 ms 9736 KB Ok
12 Correct 7 ms 9676 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Ok
2 Correct 7 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 8 ms 9932 KB Ok
7 Correct 26 ms 11104 KB Ok
8 Correct 12 ms 10300 KB Ok
9 Correct 29 ms 11328 KB Ok
10 Correct 16 ms 10572 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 6 ms 9676 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9676 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 9676 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 298 ms 30604 KB Ok
7 Correct 233 ms 33124 KB Ok
8 Correct 469 ms 36164 KB Ok
9 Correct 364 ms 34348 KB Ok
10 Correct 212 ms 24064 KB Ok
11 Correct 413 ms 35964 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 7 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9676 KB Ok
11 Correct 5 ms 9736 KB Ok
12 Correct 7 ms 9676 KB Ok
13 Correct 6 ms 9676 KB Ok
14 Correct 5 ms 9676 KB Ok
15 Correct 6 ms 9676 KB Ok
16 Correct 5 ms 9676 KB Ok
17 Correct 5 ms 9676 KB Ok
18 Correct 5 ms 9676 KB Ok
19 Correct 5 ms 9676 KB Ok
20 Correct 5 ms 9676 KB Ok
21 Correct 5 ms 9676 KB Ok
22 Correct 5 ms 9676 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 9 ms 9932 KB Ok
25 Correct 8 ms 9932 KB Ok
26 Correct 8 ms 9864 KB Ok
27 Correct 8 ms 9932 KB Ok
28 Correct 7 ms 9932 KB Ok
29 Correct 7 ms 9932 KB Ok
30 Correct 7 ms 9872 KB Ok
31 Correct 8 ms 9976 KB Ok
32 Correct 8 ms 9932 KB Ok
33 Correct 7 ms 9996 KB Ok
34 Correct 13 ms 10188 KB Ok
35 Correct 13 ms 10188 KB Ok
36 Correct 15 ms 10216 KB Ok
37 Correct 15 ms 10316 KB Ok
38 Correct 13 ms 10316 KB Ok
39 Correct 13 ms 10204 KB Ok
40 Correct 13 ms 10316 KB Ok
41 Correct 13 ms 10316 KB Ok
42 Correct 13 ms 10316 KB Ok
43 Correct 13 ms 10256 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 7 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9676 KB Ok
11 Correct 5 ms 9736 KB Ok
12 Correct 7 ms 9676 KB Ok
13 Correct 6 ms 9676 KB Ok
14 Correct 7 ms 9676 KB Ok
15 Correct 5 ms 9676 KB Ok
16 Correct 5 ms 9676 KB Ok
17 Correct 5 ms 9676 KB Ok
18 Correct 8 ms 9932 KB Ok
19 Correct 26 ms 11104 KB Ok
20 Correct 12 ms 10300 KB Ok
21 Correct 29 ms 11328 KB Ok
22 Correct 16 ms 10572 KB Ok
23 Correct 6 ms 9676 KB Ok
24 Correct 5 ms 9676 KB Ok
25 Correct 6 ms 9676 KB Ok
26 Correct 5 ms 9676 KB Ok
27 Correct 5 ms 9676 KB Ok
28 Correct 5 ms 9676 KB Ok
29 Correct 5 ms 9676 KB Ok
30 Correct 5 ms 9676 KB Ok
31 Correct 5 ms 9676 KB Ok
32 Correct 5 ms 9676 KB Ok
33 Correct 5 ms 9676 KB Ok
34 Correct 9 ms 9932 KB Ok
35 Correct 8 ms 9932 KB Ok
36 Correct 8 ms 9864 KB Ok
37 Correct 8 ms 9932 KB Ok
38 Correct 7 ms 9932 KB Ok
39 Correct 7 ms 9932 KB Ok
40 Correct 7 ms 9872 KB Ok
41 Correct 8 ms 9976 KB Ok
42 Correct 8 ms 9932 KB Ok
43 Correct 7 ms 9996 KB Ok
44 Correct 13 ms 10188 KB Ok
45 Correct 13 ms 10188 KB Ok
46 Correct 15 ms 10216 KB Ok
47 Correct 15 ms 10316 KB Ok
48 Correct 13 ms 10316 KB Ok
49 Correct 13 ms 10204 KB Ok
50 Correct 13 ms 10316 KB Ok
51 Correct 13 ms 10316 KB Ok
52 Correct 13 ms 10316 KB Ok
53 Correct 13 ms 10256 KB Ok
54 Correct 143 ms 17476 KB Ok
55 Correct 172 ms 18024 KB Ok
56 Correct 173 ms 18096 KB Ok
57 Correct 116 ms 16688 KB Ok
58 Correct 165 ms 17968 KB Ok
59 Correct 154 ms 17368 KB Ok
60 Correct 130 ms 16792 KB Ok
61 Correct 119 ms 17196 KB Ok
62 Correct 188 ms 18216 KB Ok
63 Correct 145 ms 16996 KB Ok
64 Correct 184 ms 17904 KB Ok
65 Correct 162 ms 17832 KB Ok
66 Correct 134 ms 17456 KB Ok
67 Correct 131 ms 17132 KB Ok
68 Correct 165 ms 17552 KB Ok
69 Correct 1016 ms 26812 KB Ok
70 Correct 909 ms 27232 KB Ok
71 Correct 816 ms 26744 KB Ok
72 Correct 732 ms 26808 KB Ok
73 Correct 875 ms 27036 KB Ok
74 Correct 729 ms 26668 KB Ok
75 Correct 657 ms 26672 KB Ok
76 Correct 776 ms 26824 KB Ok
77 Correct 741 ms 26744 KB Ok
78 Correct 852 ms 26676 KB Ok
79 Correct 817 ms 27136 KB Ok
80 Correct 732 ms 26820 KB Ok
81 Correct 698 ms 27132 KB Ok
82 Correct 803 ms 26868 KB Ok
83 Correct 742 ms 26840 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 7 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9676 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9676 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9676 KB Ok
11 Correct 5 ms 9736 KB Ok
12 Correct 7 ms 9676 KB Ok
13 Correct 6 ms 9676 KB Ok
14 Correct 7 ms 9676 KB Ok
15 Correct 5 ms 9676 KB Ok
16 Correct 5 ms 9676 KB Ok
17 Correct 5 ms 9676 KB Ok
18 Correct 8 ms 9932 KB Ok
19 Correct 26 ms 11104 KB Ok
20 Correct 12 ms 10300 KB Ok
21 Correct 29 ms 11328 KB Ok
22 Correct 16 ms 10572 KB Ok
23 Correct 6 ms 9676 KB Ok
24 Correct 5 ms 9676 KB Ok
25 Correct 6 ms 9676 KB Ok
26 Correct 5 ms 9676 KB Ok
27 Correct 5 ms 9676 KB Ok
28 Correct 5 ms 9676 KB Ok
29 Correct 5 ms 9676 KB Ok
30 Correct 5 ms 9676 KB Ok
31 Correct 5 ms 9676 KB Ok
32 Correct 5 ms 9676 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 9676 KB Ok
38 Correct 5 ms 9676 KB Ok
39 Correct 298 ms 30604 KB Ok
40 Correct 233 ms 33124 KB Ok
41 Correct 469 ms 36164 KB Ok
42 Correct 364 ms 34348 KB Ok
43 Correct 212 ms 24064 KB Ok
44 Correct 413 ms 35964 KB Ok
45 Correct 9 ms 9932 KB Ok
46 Correct 8 ms 9932 KB Ok
47 Correct 8 ms 9864 KB Ok
48 Correct 8 ms 9932 KB Ok
49 Correct 7 ms 9932 KB Ok
50 Correct 7 ms 9932 KB Ok
51 Correct 7 ms 9872 KB Ok
52 Correct 8 ms 9976 KB Ok
53 Correct 8 ms 9932 KB Ok
54 Correct 7 ms 9996 KB Ok
55 Correct 13 ms 10188 KB Ok
56 Correct 13 ms 10188 KB Ok
57 Correct 15 ms 10216 KB Ok
58 Correct 15 ms 10316 KB Ok
59 Correct 13 ms 10316 KB Ok
60 Correct 13 ms 10204 KB Ok
61 Correct 13 ms 10316 KB Ok
62 Correct 13 ms 10316 KB Ok
63 Correct 13 ms 10316 KB Ok
64 Correct 13 ms 10256 KB Ok
65 Correct 143 ms 17476 KB Ok
66 Correct 172 ms 18024 KB Ok
67 Correct 173 ms 18096 KB Ok
68 Correct 116 ms 16688 KB Ok
69 Correct 165 ms 17968 KB Ok
70 Correct 154 ms 17368 KB Ok
71 Correct 130 ms 16792 KB Ok
72 Correct 119 ms 17196 KB Ok
73 Correct 188 ms 18216 KB Ok
74 Correct 145 ms 16996 KB Ok
75 Correct 184 ms 17904 KB Ok
76 Correct 162 ms 17832 KB Ok
77 Correct 134 ms 17456 KB Ok
78 Correct 131 ms 17132 KB Ok
79 Correct 165 ms 17552 KB Ok
80 Correct 1016 ms 26812 KB Ok
81 Correct 909 ms 27232 KB Ok
82 Correct 816 ms 26744 KB Ok
83 Correct 732 ms 26808 KB Ok
84 Correct 875 ms 27036 KB Ok
85 Correct 729 ms 26668 KB Ok
86 Correct 657 ms 26672 KB Ok
87 Correct 776 ms 26824 KB Ok
88 Correct 741 ms 26744 KB Ok
89 Correct 852 ms 26676 KB Ok
90 Correct 817 ms 27136 KB Ok
91 Correct 732 ms 26820 KB Ok
92 Correct 698 ms 27132 KB Ok
93 Correct 803 ms 26868 KB Ok
94 Correct 742 ms 26840 KB Ok
95 Correct 444 ms 30328 KB Ok
96 Correct 737 ms 40988 KB Ok
97 Correct 654 ms 35180 KB Ok
98 Correct 446 ms 34288 KB Ok
99 Correct 538 ms 34052 KB Ok
100 Correct 623 ms 34180 KB Ok
101 Correct 574 ms 37240 KB Ok
102 Correct 586 ms 35688 KB Ok
103 Correct 612 ms 37108 KB Ok
104 Correct 741 ms 38652 KB Ok
105 Correct 745 ms 42020 KB Ok
106 Correct 559 ms 38972 KB Ok
107 Correct 663 ms 38464 KB Ok
108 Correct 774 ms 40700 KB Ok
109 Correct 589 ms 40752 KB Ok
110 Execution timed out 2085 ms 51972 KB Time limit exceeded
111 Halted 0 ms 0 KB -