답안 #501409

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
501409 2022-01-03T06:20:54 Z Ziel Nice sequence (IZhO18_sequence) C++17
76 / 100
2000 ms 50212 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();
		while (c != '-' && ('0' > c || c > '9')) {
			c = getchar();
		}
		if (c == '-')
			flag = -1, c = getchar();
		while ('0' <= c && c <= '9') {
			x = x * 10 + c - '0';
			c = getchar();
		}
		x *= flag;
	}

	inline void _vout(ll x) {
		if (x) {
			_vout(x / 10);
			putchar(x % 10 + '0');
		}
	}
	inline void vout(ll x) {
		if (x < 0) {
			putchar('-');
			_vout(-x);
		} else if (!x) {
			putchar('0');
		} else {
			_vout(x / 10);
			putchar(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 = 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;
		}
    }
    vout(res);
    puts("");
    
    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(' ');

   	order.clear();
   	for (int i = 0; i <= res; i++) {
		was[i] = 0;
		visited[i] = 0;
		if (sz(g[i])) g[i].clear();
	}
	puts("");
}
/*
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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9804 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 4 ms 9700 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9688 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9700 KB Ok
9 Correct 5 ms 9680 KB Ok
10 Correct 5 ms 9700 KB Ok
11 Correct 6 ms 9700 KB Ok
12 Correct 5 ms 9688 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 5 ms 9680 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 5 ms 9700 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 7 ms 9824 KB Ok
7 Correct 19 ms 10828 KB Ok
8 Correct 12 ms 10188 KB Ok
9 Correct 24 ms 11100 KB Ok
10 Correct 13 ms 10444 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 6 ms 9676 KB Ok
4 Correct 7 ms 9700 KB Ok
5 Correct 6 ms 9676 KB Ok
6 Correct 5 ms 9588 KB Ok
7 Correct 5 ms 9700 KB Ok
8 Correct 5 ms 9676 KB Ok
9 Correct 5 ms 9700 KB Ok
10 Correct 5 ms 9684 KB Ok
11 Correct 5 ms 9676 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Ok
2 Correct 5 ms 9612 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 6 ms 9596 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 250 ms 28220 KB Ok
7 Correct 197 ms 28936 KB Ok
8 Correct 393 ms 32488 KB Ok
9 Correct 336 ms 30800 KB Ok
10 Correct 186 ms 22568 KB Ok
11 Correct 341 ms 32956 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9804 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 4 ms 9700 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9688 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9700 KB Ok
9 Correct 5 ms 9680 KB Ok
10 Correct 5 ms 9700 KB Ok
11 Correct 6 ms 9700 KB Ok
12 Correct 5 ms 9688 KB Ok
13 Correct 5 ms 9676 KB Ok
14 Correct 5 ms 9676 KB Ok
15 Correct 6 ms 9676 KB Ok
16 Correct 7 ms 9700 KB Ok
17 Correct 6 ms 9676 KB Ok
18 Correct 5 ms 9588 KB Ok
19 Correct 5 ms 9700 KB Ok
20 Correct 5 ms 9676 KB Ok
21 Correct 5 ms 9700 KB Ok
22 Correct 5 ms 9684 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 7 ms 9828 KB Ok
25 Correct 7 ms 9932 KB Ok
26 Correct 7 ms 9932 KB Ok
27 Correct 8 ms 9932 KB Ok
28 Correct 7 ms 9804 KB Ok
29 Correct 7 ms 9804 KB Ok
30 Correct 7 ms 9804 KB Ok
31 Correct 8 ms 9932 KB Ok
32 Correct 7 ms 9820 KB Ok
33 Correct 7 ms 9932 KB Ok
34 Correct 12 ms 10220 KB Ok
35 Correct 12 ms 10188 KB Ok
36 Correct 12 ms 10212 KB Ok
37 Correct 12 ms 10220 KB Ok
38 Correct 13 ms 10148 KB Ok
39 Correct 13 ms 10160 KB Ok
40 Correct 13 ms 10232 KB Ok
41 Correct 13 ms 10188 KB Ok
42 Correct 15 ms 10188 KB Ok
43 Correct 13 ms 10188 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9804 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 4 ms 9700 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9688 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9700 KB Ok
9 Correct 5 ms 9680 KB Ok
10 Correct 5 ms 9700 KB Ok
11 Correct 6 ms 9700 KB Ok
12 Correct 5 ms 9688 KB Ok
13 Correct 5 ms 9676 KB Ok
14 Correct 5 ms 9680 KB Ok
15 Correct 5 ms 9676 KB Ok
16 Correct 5 ms 9700 KB Ok
17 Correct 5 ms 9676 KB Ok
18 Correct 7 ms 9824 KB Ok
19 Correct 19 ms 10828 KB Ok
20 Correct 12 ms 10188 KB Ok
21 Correct 24 ms 11100 KB Ok
22 Correct 13 ms 10444 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 5 ms 9676 KB Ok
25 Correct 6 ms 9676 KB Ok
26 Correct 7 ms 9700 KB Ok
27 Correct 6 ms 9676 KB Ok
28 Correct 5 ms 9588 KB Ok
29 Correct 5 ms 9700 KB Ok
30 Correct 5 ms 9676 KB Ok
31 Correct 5 ms 9700 KB Ok
32 Correct 5 ms 9684 KB Ok
33 Correct 5 ms 9676 KB Ok
34 Correct 7 ms 9828 KB Ok
35 Correct 7 ms 9932 KB Ok
36 Correct 7 ms 9932 KB Ok
37 Correct 8 ms 9932 KB Ok
38 Correct 7 ms 9804 KB Ok
39 Correct 7 ms 9804 KB Ok
40 Correct 7 ms 9804 KB Ok
41 Correct 8 ms 9932 KB Ok
42 Correct 7 ms 9820 KB Ok
43 Correct 7 ms 9932 KB Ok
44 Correct 12 ms 10220 KB Ok
45 Correct 12 ms 10188 KB Ok
46 Correct 12 ms 10212 KB Ok
47 Correct 12 ms 10220 KB Ok
48 Correct 13 ms 10148 KB Ok
49 Correct 13 ms 10160 KB Ok
50 Correct 13 ms 10232 KB Ok
51 Correct 13 ms 10188 KB Ok
52 Correct 15 ms 10188 KB Ok
53 Correct 13 ms 10188 KB Ok
54 Correct 134 ms 17556 KB Ok
55 Correct 156 ms 17928 KB Ok
56 Correct 154 ms 18012 KB Ok
57 Correct 100 ms 16672 KB Ok
58 Correct 152 ms 17920 KB Ok
59 Correct 133 ms 17360 KB Ok
60 Correct 115 ms 16724 KB Ok
61 Correct 95 ms 17188 KB Ok
62 Correct 179 ms 18240 KB Ok
63 Correct 118 ms 17032 KB Ok
64 Correct 166 ms 17852 KB Ok
65 Correct 147 ms 17812 KB Ok
66 Correct 113 ms 17400 KB Ok
67 Correct 116 ms 17076 KB Ok
68 Correct 145 ms 17532 KB Ok
69 Correct 729 ms 25060 KB Ok
70 Correct 855 ms 25208 KB Ok
71 Correct 720 ms 24744 KB Ok
72 Correct 690 ms 25024 KB Ok
73 Correct 734 ms 24928 KB Ok
74 Correct 670 ms 24552 KB Ok
75 Correct 584 ms 24752 KB Ok
76 Correct 757 ms 25124 KB Ok
77 Correct 628 ms 24540 KB Ok
78 Correct 750 ms 25024 KB Ok
79 Correct 693 ms 25080 KB Ok
80 Correct 662 ms 24544 KB Ok
81 Correct 612 ms 25140 KB Ok
82 Correct 759 ms 24764 KB Ok
83 Correct 652 ms 25116 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9804 KB Ok
2 Correct 5 ms 9676 KB Ok
3 Correct 5 ms 9676 KB Ok
4 Correct 4 ms 9700 KB Ok
5 Correct 5 ms 9676 KB Ok
6 Correct 5 ms 9688 KB Ok
7 Correct 5 ms 9676 KB Ok
8 Correct 5 ms 9700 KB Ok
9 Correct 5 ms 9680 KB Ok
10 Correct 5 ms 9700 KB Ok
11 Correct 6 ms 9700 KB Ok
12 Correct 5 ms 9688 KB Ok
13 Correct 5 ms 9676 KB Ok
14 Correct 5 ms 9680 KB Ok
15 Correct 5 ms 9676 KB Ok
16 Correct 5 ms 9700 KB Ok
17 Correct 5 ms 9676 KB Ok
18 Correct 7 ms 9824 KB Ok
19 Correct 19 ms 10828 KB Ok
20 Correct 12 ms 10188 KB Ok
21 Correct 24 ms 11100 KB Ok
22 Correct 13 ms 10444 KB Ok
23 Correct 5 ms 9676 KB Ok
24 Correct 5 ms 9676 KB Ok
25 Correct 6 ms 9676 KB Ok
26 Correct 7 ms 9700 KB Ok
27 Correct 6 ms 9676 KB Ok
28 Correct 5 ms 9588 KB Ok
29 Correct 5 ms 9700 KB Ok
30 Correct 5 ms 9676 KB Ok
31 Correct 5 ms 9700 KB Ok
32 Correct 5 ms 9684 KB Ok
33 Correct 5 ms 9676 KB Ok
34 Correct 5 ms 9676 KB Ok
35 Correct 5 ms 9612 KB Ok
36 Correct 5 ms 9676 KB Ok
37 Correct 6 ms 9596 KB Ok
38 Correct 5 ms 9676 KB Ok
39 Correct 250 ms 28220 KB Ok
40 Correct 197 ms 28936 KB Ok
41 Correct 393 ms 32488 KB Ok
42 Correct 336 ms 30800 KB Ok
43 Correct 186 ms 22568 KB Ok
44 Correct 341 ms 32956 KB Ok
45 Correct 7 ms 9828 KB Ok
46 Correct 7 ms 9932 KB Ok
47 Correct 7 ms 9932 KB Ok
48 Correct 8 ms 9932 KB Ok
49 Correct 7 ms 9804 KB Ok
50 Correct 7 ms 9804 KB Ok
51 Correct 7 ms 9804 KB Ok
52 Correct 8 ms 9932 KB Ok
53 Correct 7 ms 9820 KB Ok
54 Correct 7 ms 9932 KB Ok
55 Correct 12 ms 10220 KB Ok
56 Correct 12 ms 10188 KB Ok
57 Correct 12 ms 10212 KB Ok
58 Correct 12 ms 10220 KB Ok
59 Correct 13 ms 10148 KB Ok
60 Correct 13 ms 10160 KB Ok
61 Correct 13 ms 10232 KB Ok
62 Correct 13 ms 10188 KB Ok
63 Correct 15 ms 10188 KB Ok
64 Correct 13 ms 10188 KB Ok
65 Correct 134 ms 17556 KB Ok
66 Correct 156 ms 17928 KB Ok
67 Correct 154 ms 18012 KB Ok
68 Correct 100 ms 16672 KB Ok
69 Correct 152 ms 17920 KB Ok
70 Correct 133 ms 17360 KB Ok
71 Correct 115 ms 16724 KB Ok
72 Correct 95 ms 17188 KB Ok
73 Correct 179 ms 18240 KB Ok
74 Correct 118 ms 17032 KB Ok
75 Correct 166 ms 17852 KB Ok
76 Correct 147 ms 17812 KB Ok
77 Correct 113 ms 17400 KB Ok
78 Correct 116 ms 17076 KB Ok
79 Correct 145 ms 17532 KB Ok
80 Correct 729 ms 25060 KB Ok
81 Correct 855 ms 25208 KB Ok
82 Correct 720 ms 24744 KB Ok
83 Correct 690 ms 25024 KB Ok
84 Correct 734 ms 24928 KB Ok
85 Correct 670 ms 24552 KB Ok
86 Correct 584 ms 24752 KB Ok
87 Correct 757 ms 25124 KB Ok
88 Correct 628 ms 24540 KB Ok
89 Correct 750 ms 25024 KB Ok
90 Correct 693 ms 25080 KB Ok
91 Correct 662 ms 24544 KB Ok
92 Correct 612 ms 25140 KB Ok
93 Correct 759 ms 24764 KB Ok
94 Correct 652 ms 25116 KB Ok
95 Correct 406 ms 30152 KB Ok
96 Correct 619 ms 40964 KB Ok
97 Correct 586 ms 35104 KB Ok
98 Correct 366 ms 34228 KB Ok
99 Correct 465 ms 34188 KB Ok
100 Correct 551 ms 34132 KB Ok
101 Correct 523 ms 37200 KB Ok
102 Correct 502 ms 35680 KB Ok
103 Correct 592 ms 36976 KB Ok
104 Correct 632 ms 38592 KB Ok
105 Correct 639 ms 41972 KB Ok
106 Correct 458 ms 38848 KB Ok
107 Correct 577 ms 38440 KB Ok
108 Correct 698 ms 40644 KB Ok
109 Correct 497 ms 40408 KB Ok
110 Execution timed out 2085 ms 50212 KB Time limit exceeded
111 Halted 0 ms 0 KB -