Submission #497790

# Submission time Handle Problem Language Result Execution time Memory
497790 2021-12-23T20:08:47 Z Ziel Nice sequence (IZhO18_sequence) C++17
76 / 100
2000 ms 66072 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 = 1e6 + 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;
		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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB Ok
2 Correct 13 ms 23808 KB Ok
3 Correct 16 ms 23756 KB Ok
4 Correct 13 ms 23720 KB Ok
5 Correct 13 ms 23716 KB Ok
6 Correct 13 ms 23756 KB Ok
7 Correct 12 ms 23716 KB Ok
8 Correct 13 ms 23712 KB Ok
9 Correct 13 ms 23756 KB Ok
10 Correct 16 ms 23804 KB Ok
11 Correct 13 ms 23756 KB Ok
12 Correct 13 ms 23756 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB Ok
2 Correct 12 ms 23828 KB Ok
3 Correct 12 ms 23756 KB Ok
4 Correct 12 ms 23756 KB Ok
5 Correct 15 ms 23756 KB Ok
6 Correct 16 ms 24060 KB Ok
7 Correct 33 ms 25132 KB Ok
8 Correct 24 ms 24396 KB Ok
9 Correct 37 ms 25436 KB Ok
10 Correct 24 ms 24704 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23736 KB Ok
2 Correct 13 ms 23756 KB Ok
3 Correct 12 ms 23756 KB Ok
4 Correct 12 ms 23704 KB Ok
5 Correct 12 ms 23812 KB Ok
6 Correct 13 ms 23756 KB Ok
7 Correct 12 ms 23804 KB Ok
8 Correct 17 ms 23816 KB Ok
9 Correct 13 ms 23756 KB Ok
10 Correct 16 ms 23768 KB Ok
11 Correct 13 ms 23756 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23724 KB Ok
2 Correct 15 ms 23808 KB Ok
3 Correct 13 ms 23800 KB Ok
4 Correct 13 ms 23868 KB Ok
5 Correct 12 ms 23756 KB Ok
6 Correct 302 ms 44696 KB Ok
7 Correct 242 ms 47048 KB Ok
8 Correct 495 ms 50184 KB Ok
9 Correct 364 ms 48416 KB Ok
10 Correct 251 ms 38212 KB Ok
11 Correct 418 ms 50140 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB Ok
2 Correct 13 ms 23808 KB Ok
3 Correct 16 ms 23756 KB Ok
4 Correct 13 ms 23720 KB Ok
5 Correct 13 ms 23716 KB Ok
6 Correct 13 ms 23756 KB Ok
7 Correct 12 ms 23716 KB Ok
8 Correct 13 ms 23712 KB Ok
9 Correct 13 ms 23756 KB Ok
10 Correct 16 ms 23804 KB Ok
11 Correct 13 ms 23756 KB Ok
12 Correct 13 ms 23756 KB Ok
13 Correct 12 ms 23736 KB Ok
14 Correct 13 ms 23756 KB Ok
15 Correct 12 ms 23756 KB Ok
16 Correct 12 ms 23704 KB Ok
17 Correct 12 ms 23812 KB Ok
18 Correct 13 ms 23756 KB Ok
19 Correct 12 ms 23804 KB Ok
20 Correct 17 ms 23816 KB Ok
21 Correct 13 ms 23756 KB Ok
22 Correct 16 ms 23768 KB Ok
23 Correct 13 ms 23756 KB Ok
24 Correct 16 ms 24012 KB Ok
25 Correct 15 ms 24004 KB Ok
26 Correct 16 ms 24064 KB Ok
27 Correct 16 ms 24080 KB Ok
28 Correct 14 ms 23968 KB Ok
29 Correct 15 ms 23912 KB Ok
30 Correct 15 ms 24012 KB Ok
31 Correct 15 ms 23996 KB Ok
32 Correct 16 ms 24012 KB Ok
33 Correct 19 ms 24052 KB Ok
34 Correct 20 ms 24396 KB Ok
35 Correct 20 ms 24352 KB Ok
36 Correct 20 ms 24428 KB Ok
37 Correct 21 ms 24396 KB Ok
38 Correct 21 ms 24316 KB Ok
39 Correct 20 ms 24340 KB Ok
40 Correct 20 ms 24396 KB Ok
41 Correct 22 ms 24376 KB Ok
42 Correct 24 ms 24420 KB Ok
43 Correct 22 ms 24396 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB Ok
2 Correct 13 ms 23808 KB Ok
3 Correct 16 ms 23756 KB Ok
4 Correct 13 ms 23720 KB Ok
5 Correct 13 ms 23716 KB Ok
6 Correct 13 ms 23756 KB Ok
7 Correct 12 ms 23716 KB Ok
8 Correct 13 ms 23712 KB Ok
9 Correct 13 ms 23756 KB Ok
10 Correct 16 ms 23804 KB Ok
11 Correct 13 ms 23756 KB Ok
12 Correct 13 ms 23756 KB Ok
13 Correct 12 ms 23756 KB Ok
14 Correct 12 ms 23828 KB Ok
15 Correct 12 ms 23756 KB Ok
16 Correct 12 ms 23756 KB Ok
17 Correct 15 ms 23756 KB Ok
18 Correct 16 ms 24060 KB Ok
19 Correct 33 ms 25132 KB Ok
20 Correct 24 ms 24396 KB Ok
21 Correct 37 ms 25436 KB Ok
22 Correct 24 ms 24704 KB Ok
23 Correct 12 ms 23736 KB Ok
24 Correct 13 ms 23756 KB Ok
25 Correct 12 ms 23756 KB Ok
26 Correct 12 ms 23704 KB Ok
27 Correct 12 ms 23812 KB Ok
28 Correct 13 ms 23756 KB Ok
29 Correct 12 ms 23804 KB Ok
30 Correct 17 ms 23816 KB Ok
31 Correct 13 ms 23756 KB Ok
32 Correct 16 ms 23768 KB Ok
33 Correct 13 ms 23756 KB Ok
34 Correct 16 ms 24012 KB Ok
35 Correct 15 ms 24004 KB Ok
36 Correct 16 ms 24064 KB Ok
37 Correct 16 ms 24080 KB Ok
38 Correct 14 ms 23968 KB Ok
39 Correct 15 ms 23912 KB Ok
40 Correct 15 ms 24012 KB Ok
41 Correct 15 ms 23996 KB Ok
42 Correct 16 ms 24012 KB Ok
43 Correct 19 ms 24052 KB Ok
44 Correct 20 ms 24396 KB Ok
45 Correct 20 ms 24352 KB Ok
46 Correct 20 ms 24428 KB Ok
47 Correct 21 ms 24396 KB Ok
48 Correct 21 ms 24316 KB Ok
49 Correct 20 ms 24340 KB Ok
50 Correct 20 ms 24396 KB Ok
51 Correct 22 ms 24376 KB Ok
52 Correct 24 ms 24420 KB Ok
53 Correct 22 ms 24396 KB Ok
54 Correct 159 ms 31644 KB Ok
55 Correct 209 ms 32108 KB Ok
56 Correct 171 ms 32168 KB Ok
57 Correct 129 ms 30888 KB Ok
58 Correct 181 ms 31864 KB Ok
59 Correct 182 ms 31488 KB Ok
60 Correct 138 ms 30800 KB Ok
61 Correct 127 ms 31276 KB Ok
62 Correct 227 ms 32364 KB Ok
63 Correct 155 ms 31184 KB Ok
64 Correct 199 ms 32024 KB Ok
65 Correct 178 ms 31860 KB Ok
66 Correct 139 ms 31500 KB Ok
67 Correct 160 ms 31148 KB Ok
68 Correct 169 ms 31700 KB Ok
69 Correct 852 ms 40824 KB Ok
70 Correct 885 ms 41352 KB Ok
71 Correct 731 ms 40852 KB Ok
72 Correct 783 ms 40688 KB Ok
73 Correct 903 ms 41120 KB Ok
74 Correct 726 ms 40764 KB Ok
75 Correct 674 ms 40820 KB Ok
76 Correct 784 ms 40960 KB Ok
77 Correct 790 ms 40784 KB Ok
78 Correct 1049 ms 40724 KB Ok
79 Correct 895 ms 41212 KB Ok
80 Correct 778 ms 40732 KB Ok
81 Correct 712 ms 41112 KB Ok
82 Correct 933 ms 40856 KB Ok
83 Correct 754 ms 40936 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23756 KB Ok
2 Correct 13 ms 23808 KB Ok
3 Correct 16 ms 23756 KB Ok
4 Correct 13 ms 23720 KB Ok
5 Correct 13 ms 23716 KB Ok
6 Correct 13 ms 23756 KB Ok
7 Correct 12 ms 23716 KB Ok
8 Correct 13 ms 23712 KB Ok
9 Correct 13 ms 23756 KB Ok
10 Correct 16 ms 23804 KB Ok
11 Correct 13 ms 23756 KB Ok
12 Correct 13 ms 23756 KB Ok
13 Correct 12 ms 23756 KB Ok
14 Correct 12 ms 23828 KB Ok
15 Correct 12 ms 23756 KB Ok
16 Correct 12 ms 23756 KB Ok
17 Correct 15 ms 23756 KB Ok
18 Correct 16 ms 24060 KB Ok
19 Correct 33 ms 25132 KB Ok
20 Correct 24 ms 24396 KB Ok
21 Correct 37 ms 25436 KB Ok
22 Correct 24 ms 24704 KB Ok
23 Correct 12 ms 23736 KB Ok
24 Correct 13 ms 23756 KB Ok
25 Correct 12 ms 23756 KB Ok
26 Correct 12 ms 23704 KB Ok
27 Correct 12 ms 23812 KB Ok
28 Correct 13 ms 23756 KB Ok
29 Correct 12 ms 23804 KB Ok
30 Correct 17 ms 23816 KB Ok
31 Correct 13 ms 23756 KB Ok
32 Correct 16 ms 23768 KB Ok
33 Correct 13 ms 23756 KB Ok
34 Correct 13 ms 23724 KB Ok
35 Correct 15 ms 23808 KB Ok
36 Correct 13 ms 23800 KB Ok
37 Correct 13 ms 23868 KB Ok
38 Correct 12 ms 23756 KB Ok
39 Correct 302 ms 44696 KB Ok
40 Correct 242 ms 47048 KB Ok
41 Correct 495 ms 50184 KB Ok
42 Correct 364 ms 48416 KB Ok
43 Correct 251 ms 38212 KB Ok
44 Correct 418 ms 50140 KB Ok
45 Correct 16 ms 24012 KB Ok
46 Correct 15 ms 24004 KB Ok
47 Correct 16 ms 24064 KB Ok
48 Correct 16 ms 24080 KB Ok
49 Correct 14 ms 23968 KB Ok
50 Correct 15 ms 23912 KB Ok
51 Correct 15 ms 24012 KB Ok
52 Correct 15 ms 23996 KB Ok
53 Correct 16 ms 24012 KB Ok
54 Correct 19 ms 24052 KB Ok
55 Correct 20 ms 24396 KB Ok
56 Correct 20 ms 24352 KB Ok
57 Correct 20 ms 24428 KB Ok
58 Correct 21 ms 24396 KB Ok
59 Correct 21 ms 24316 KB Ok
60 Correct 20 ms 24340 KB Ok
61 Correct 20 ms 24396 KB Ok
62 Correct 22 ms 24376 KB Ok
63 Correct 24 ms 24420 KB Ok
64 Correct 22 ms 24396 KB Ok
65 Correct 159 ms 31644 KB Ok
66 Correct 209 ms 32108 KB Ok
67 Correct 171 ms 32168 KB Ok
68 Correct 129 ms 30888 KB Ok
69 Correct 181 ms 31864 KB Ok
70 Correct 182 ms 31488 KB Ok
71 Correct 138 ms 30800 KB Ok
72 Correct 127 ms 31276 KB Ok
73 Correct 227 ms 32364 KB Ok
74 Correct 155 ms 31184 KB Ok
75 Correct 199 ms 32024 KB Ok
76 Correct 178 ms 31860 KB Ok
77 Correct 139 ms 31500 KB Ok
78 Correct 160 ms 31148 KB Ok
79 Correct 169 ms 31700 KB Ok
80 Correct 852 ms 40824 KB Ok
81 Correct 885 ms 41352 KB Ok
82 Correct 731 ms 40852 KB Ok
83 Correct 783 ms 40688 KB Ok
84 Correct 903 ms 41120 KB Ok
85 Correct 726 ms 40764 KB Ok
86 Correct 674 ms 40820 KB Ok
87 Correct 784 ms 40960 KB Ok
88 Correct 790 ms 40784 KB Ok
89 Correct 1049 ms 40724 KB Ok
90 Correct 895 ms 41212 KB Ok
91 Correct 778 ms 40732 KB Ok
92 Correct 712 ms 41112 KB Ok
93 Correct 933 ms 40856 KB Ok
94 Correct 754 ms 40936 KB Ok
95 Correct 555 ms 44412 KB Ok
96 Correct 840 ms 55064 KB Ok
97 Correct 733 ms 49268 KB Ok
98 Correct 555 ms 48408 KB Ok
99 Correct 604 ms 48204 KB Ok
100 Correct 755 ms 48316 KB Ok
101 Correct 717 ms 51352 KB Ok
102 Correct 891 ms 49760 KB Ok
103 Correct 925 ms 51184 KB Ok
104 Correct 923 ms 52680 KB Ok
105 Correct 938 ms 55980 KB Ok
106 Correct 696 ms 52968 KB Ok
107 Correct 858 ms 52556 KB Ok
108 Correct 930 ms 54608 KB Ok
109 Correct 754 ms 54588 KB Ok
110 Execution timed out 2078 ms 66072 KB Time limit exceeded
111 Halted 0 ms 0 KB -