Submission #501413

# Submission time Handle Problem Language Result Execution time Memory
501413 2022-01-03T06:28:12 Z Ziel Nice sequence (IZhO18_sequence) C++17
76 / 100
2000 ms 50176 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 = "");

namespace io {
	inline void vin(ll &x) {
		int flag = 1;
		x = 0;
		char c = getchar_unlocked();
		while (c != '-' && ('0' > c || c > '9')) {
			c = getchar_unlocked();
		}
		if (c == '-')
			flag = -1, c = getchar_unlocked();
		while ('0' <= c && c <= '9') {
			x = x * 10 + c - '0';
			c = getchar_unlocked();
		}
		x *= flag;
	}

	inline void _vout(ll x) {
		if (x) {
			_vout(x / 10);
			putchar_unlocked(x % 10 + '0');
		}
	}
	inline void vout(ll x) {
		if (x < 0) {
			putchar_unlocked('-');
			_vout(-x);
		} else if (!x) {
			putchar_unlocked('0');
		} else {
			_vout(x / 10);
			putchar_unlocked(x % 10 + '0');
		}
	}
}
using namespace io;


#define int ll
const int N = 4e5 + 156;
int was[N], visited[N];
vector<int> g[N], order;

inline 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;
}
inline void dfs2(int v) {
	visited[v] = 1;
	for (int to: g[v]) {
		if (!visited[to])
			dfs2(to);
	}
	order.push_back(v);
}

inline void solve() {
    int n, m;
    vin(n);
    vin(m);

    int lo = min(n, m) - 1, 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;
		}
    }
    vout(res);
    putchar_unlocked('\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++)
   		vout(p[i] - p[i - 1]), putchar_unlocked(' ');

   	order.clear();
   	for (int i = 0; i <= res; i++) {
		was[i] = 0;
		visited[i] = 0;
		if (sz(g[i])) g[i].clear();
	}
	putchar_unlocked('\n');
}
/*
0 2
1 3
3 0

2 1 3 0
*/

signed main() {
//    setIO();
    
    int tt = 1;
    if (FLAG) {
    	vin(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:167:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  167 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  168 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 5 ms 9672 KB Ok
7 Correct 6 ms 9676 KB Ok
8 Correct 4 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9676 KB Ok
12 Correct 4 ms 9676 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 4 ms 9676 KB Ok
4 Correct 6 ms 9676 KB Ok
5 Correct 5 ms 9608 KB Ok
6 Correct 7 ms 9880 KB Ok
7 Correct 20 ms 10828 KB Ok
8 Correct 11 ms 10200 KB Ok
9 Correct 26 ms 11060 KB Ok
10 Correct 14 ms 10444 KB Ok
# Verdict Execution time Memory 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 9704 KB Ok
6 Correct 5 ms 9676 KB Ok
7 Correct 6 ms 9652 KB Ok
8 Correct 5 ms 9692 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9600 KB Ok
11 Correct 6 ms 9676 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Ok
2 Correct 5 ms 9712 KB Ok
3 Correct 5 ms 9600 KB Ok
4 Correct 6 ms 9676 KB Ok
5 Correct 4 ms 9676 KB Ok
6 Correct 243 ms 28344 KB Ok
7 Correct 190 ms 28812 KB Ok
8 Correct 401 ms 32440 KB Ok
9 Correct 301 ms 30812 KB Ok
10 Correct 168 ms 22596 KB Ok
11 Correct 328 ms 32948 KB Ok
# Verdict Execution time Memory 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 5 ms 9672 KB Ok
7 Correct 6 ms 9676 KB Ok
8 Correct 4 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9676 KB Ok
12 Correct 4 ms 9676 KB Ok
13 Correct 5 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 9704 KB Ok
18 Correct 5 ms 9676 KB Ok
19 Correct 6 ms 9652 KB Ok
20 Correct 5 ms 9692 KB Ok
21 Correct 5 ms 9676 KB Ok
22 Correct 5 ms 9600 KB Ok
23 Correct 6 ms 9676 KB Ok
24 Correct 7 ms 9932 KB Ok
25 Correct 7 ms 9932 KB Ok
26 Correct 6 ms 9932 KB Ok
27 Correct 7 ms 9912 KB Ok
28 Correct 7 ms 9932 KB Ok
29 Correct 6 ms 9904 KB Ok
30 Correct 6 ms 9804 KB Ok
31 Correct 7 ms 9952 KB Ok
32 Correct 8 ms 9928 KB Ok
33 Correct 7 ms 9932 KB Ok
34 Correct 12 ms 10188 KB Ok
35 Correct 12 ms 10164 KB Ok
36 Correct 11 ms 10120 KB Ok
37 Correct 12 ms 10152 KB Ok
38 Correct 12 ms 10188 KB Ok
39 Correct 13 ms 10060 KB Ok
40 Correct 12 ms 10224 KB Ok
41 Correct 11 ms 10188 KB Ok
42 Correct 12 ms 10260 KB Ok
43 Correct 12 ms 10204 KB Ok
# Verdict Execution time Memory 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 5 ms 9672 KB Ok
7 Correct 6 ms 9676 KB Ok
8 Correct 4 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9676 KB Ok
12 Correct 4 ms 9676 KB Ok
13 Correct 4 ms 9676 KB Ok
14 Correct 5 ms 9676 KB Ok
15 Correct 4 ms 9676 KB Ok
16 Correct 6 ms 9676 KB Ok
17 Correct 5 ms 9608 KB Ok
18 Correct 7 ms 9880 KB Ok
19 Correct 20 ms 10828 KB Ok
20 Correct 11 ms 10200 KB Ok
21 Correct 26 ms 11060 KB Ok
22 Correct 14 ms 10444 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 7 ms 9676 KB Ok
25 Correct 5 ms 9676 KB Ok
26 Correct 5 ms 9676 KB Ok
27 Correct 5 ms 9704 KB Ok
28 Correct 5 ms 9676 KB Ok
29 Correct 6 ms 9652 KB Ok
30 Correct 5 ms 9692 KB Ok
31 Correct 5 ms 9676 KB Ok
32 Correct 5 ms 9600 KB Ok
33 Correct 6 ms 9676 KB Ok
34 Correct 7 ms 9932 KB Ok
35 Correct 7 ms 9932 KB Ok
36 Correct 6 ms 9932 KB Ok
37 Correct 7 ms 9912 KB Ok
38 Correct 7 ms 9932 KB Ok
39 Correct 6 ms 9904 KB Ok
40 Correct 6 ms 9804 KB Ok
41 Correct 7 ms 9952 KB Ok
42 Correct 8 ms 9928 KB Ok
43 Correct 7 ms 9932 KB Ok
44 Correct 12 ms 10188 KB Ok
45 Correct 12 ms 10164 KB Ok
46 Correct 11 ms 10120 KB Ok
47 Correct 12 ms 10152 KB Ok
48 Correct 12 ms 10188 KB Ok
49 Correct 13 ms 10060 KB Ok
50 Correct 12 ms 10224 KB Ok
51 Correct 11 ms 10188 KB Ok
52 Correct 12 ms 10260 KB Ok
53 Correct 12 ms 10204 KB Ok
54 Correct 114 ms 17552 KB Ok
55 Correct 155 ms 17920 KB Ok
56 Correct 133 ms 17960 KB Ok
57 Correct 96 ms 16708 KB Ok
58 Correct 142 ms 17776 KB Ok
59 Correct 126 ms 17400 KB Ok
60 Correct 106 ms 16660 KB Ok
61 Correct 123 ms 17072 KB Ok
62 Correct 169 ms 18232 KB Ok
63 Correct 136 ms 16984 KB Ok
64 Correct 155 ms 17876 KB Ok
65 Correct 144 ms 17752 KB Ok
66 Correct 109 ms 17468 KB Ok
67 Correct 97 ms 17144 KB Ok
68 Correct 154 ms 17504 KB Ok
69 Correct 757 ms 25048 KB Ok
70 Correct 794 ms 25144 KB Ok
71 Correct 646 ms 24760 KB Ok
72 Correct 622 ms 24976 KB Ok
73 Correct 758 ms 24900 KB Ok
74 Correct 633 ms 24544 KB Ok
75 Correct 560 ms 24772 KB Ok
76 Correct 743 ms 24992 KB Ok
77 Correct 666 ms 24636 KB Ok
78 Correct 784 ms 25048 KB Ok
79 Correct 677 ms 25024 KB Ok
80 Correct 671 ms 24544 KB Ok
81 Correct 648 ms 25140 KB Ok
82 Correct 767 ms 24764 KB Ok
83 Correct 637 ms 25136 KB Ok
# Verdict Execution time Memory 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 5 ms 9672 KB Ok
7 Correct 6 ms 9676 KB Ok
8 Correct 4 ms 9676 KB Ok
9 Correct 5 ms 9676 KB Ok
10 Correct 5 ms 9716 KB Ok
11 Correct 5 ms 9676 KB Ok
12 Correct 4 ms 9676 KB Ok
13 Correct 4 ms 9676 KB Ok
14 Correct 5 ms 9676 KB Ok
15 Correct 4 ms 9676 KB Ok
16 Correct 6 ms 9676 KB Ok
17 Correct 5 ms 9608 KB Ok
18 Correct 7 ms 9880 KB Ok
19 Correct 20 ms 10828 KB Ok
20 Correct 11 ms 10200 KB Ok
21 Correct 26 ms 11060 KB Ok
22 Correct 14 ms 10444 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 7 ms 9676 KB Ok
25 Correct 5 ms 9676 KB Ok
26 Correct 5 ms 9676 KB Ok
27 Correct 5 ms 9704 KB Ok
28 Correct 5 ms 9676 KB Ok
29 Correct 6 ms 9652 KB Ok
30 Correct 5 ms 9692 KB Ok
31 Correct 5 ms 9676 KB Ok
32 Correct 5 ms 9600 KB Ok
33 Correct 6 ms 9676 KB Ok
34 Correct 6 ms 9676 KB Ok
35 Correct 5 ms 9712 KB Ok
36 Correct 5 ms 9600 KB Ok
37 Correct 6 ms 9676 KB Ok
38 Correct 4 ms 9676 KB Ok
39 Correct 243 ms 28344 KB Ok
40 Correct 190 ms 28812 KB Ok
41 Correct 401 ms 32440 KB Ok
42 Correct 301 ms 30812 KB Ok
43 Correct 168 ms 22596 KB Ok
44 Correct 328 ms 32948 KB Ok
45 Correct 7 ms 9932 KB Ok
46 Correct 7 ms 9932 KB Ok
47 Correct 6 ms 9932 KB Ok
48 Correct 7 ms 9912 KB Ok
49 Correct 7 ms 9932 KB Ok
50 Correct 6 ms 9904 KB Ok
51 Correct 6 ms 9804 KB Ok
52 Correct 7 ms 9952 KB Ok
53 Correct 8 ms 9928 KB Ok
54 Correct 7 ms 9932 KB Ok
55 Correct 12 ms 10188 KB Ok
56 Correct 12 ms 10164 KB Ok
57 Correct 11 ms 10120 KB Ok
58 Correct 12 ms 10152 KB Ok
59 Correct 12 ms 10188 KB Ok
60 Correct 13 ms 10060 KB Ok
61 Correct 12 ms 10224 KB Ok
62 Correct 11 ms 10188 KB Ok
63 Correct 12 ms 10260 KB Ok
64 Correct 12 ms 10204 KB Ok
65 Correct 114 ms 17552 KB Ok
66 Correct 155 ms 17920 KB Ok
67 Correct 133 ms 17960 KB Ok
68 Correct 96 ms 16708 KB Ok
69 Correct 142 ms 17776 KB Ok
70 Correct 126 ms 17400 KB Ok
71 Correct 106 ms 16660 KB Ok
72 Correct 123 ms 17072 KB Ok
73 Correct 169 ms 18232 KB Ok
74 Correct 136 ms 16984 KB Ok
75 Correct 155 ms 17876 KB Ok
76 Correct 144 ms 17752 KB Ok
77 Correct 109 ms 17468 KB Ok
78 Correct 97 ms 17144 KB Ok
79 Correct 154 ms 17504 KB Ok
80 Correct 757 ms 25048 KB Ok
81 Correct 794 ms 25144 KB Ok
82 Correct 646 ms 24760 KB Ok
83 Correct 622 ms 24976 KB Ok
84 Correct 758 ms 24900 KB Ok
85 Correct 633 ms 24544 KB Ok
86 Correct 560 ms 24772 KB Ok
87 Correct 743 ms 24992 KB Ok
88 Correct 666 ms 24636 KB Ok
89 Correct 784 ms 25048 KB Ok
90 Correct 677 ms 25024 KB Ok
91 Correct 671 ms 24544 KB Ok
92 Correct 648 ms 25140 KB Ok
93 Correct 767 ms 24764 KB Ok
94 Correct 637 ms 25136 KB Ok
95 Correct 312 ms 30512 KB Ok
96 Correct 631 ms 40932 KB Ok
97 Correct 626 ms 35140 KB Ok
98 Correct 357 ms 34340 KB Ok
99 Correct 463 ms 33836 KB Ok
100 Correct 516 ms 34388 KB Ok
101 Correct 514 ms 37232 KB Ok
102 Correct 452 ms 35564 KB Ok
103 Correct 527 ms 36980 KB Ok
104 Correct 588 ms 38752 KB Ok
105 Correct 617 ms 41948 KB Ok
106 Correct 456 ms 38864 KB Ok
107 Correct 561 ms 38440 KB Ok
108 Correct 706 ms 40496 KB Ok
109 Correct 479 ms 40540 KB Ok
110 Execution timed out 2088 ms 50176 KB Time limit exceeded
111 Halted 0 ms 0 KB -