forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
116 lines (102 loc) · 2.92 KB
/
A.cpp
File metadata and controls
116 lines (102 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 1011;
int n;
bool canOdd(int a, int n) {
int x = a * (a - 1) / 2;
if (n == x) return true;
if (n == x + 1) return false;
if (n > x) return true;
return false;
}
bool canEven(int a, int n) {
int x = a * a / 2;
return n >= x;
}
int res[MN], a[MN][MN];
void pattern(int x, int n) {
int k = 0;
FOR(i,1,x) FOR(j,i+1,x) a[i][j] = a[j][i] = 1;
memset(res, 0, sizeof res);
if (x % 2 == 0) {
for(int i = 1; i < x; i += 2) {
a[i][i+1]++;
a[i+1][i]++;
}
}
res[k = 1] = 1;
int bound = (x % 2 == 0) ? x * (x / 2) : x * (x - 1) / 2;
FOR(turn,2,bound) {
int last = res[turn-1];
int cur = x;
while (a[last][cur] == 0) --cur;
res[++k] = cur;
a[last][cur]--;
a[cur][last]--;
}
res[n+1] = res[1];
while (k < n) {
++k;
res[k] = 1;
while (res[k] == res[k-1] || res[k] == res[k+1]) {
res[k] = res[k] % x + 1;
}
}
FOR(i,1,n) cout << res[i] << ' '; cout << endl;
}
int has[1011][1011];
#undef int
int main() {
#define int long long
ios :: sync_with_stdio(0); cin.tie(0);
freopen("achromatic.in", "r", stdin);
freopen("achromatic.out", "w", stdout);
while (cin >> n) {
int best = 0;
// a odd
for(int a = 3; a <= n; a += 2) {
if (canOdd(a, n)) {
best = max(best, a);
}
}
// a even
for(int a = 2; a <= n; a += 2) {
if (canEven(a, n)) {
best = max(best, a);
}
}
cout << best << endl;
pattern(best, n);
res[n+1] = res[1];
FOR(i,1,n) {
if (res[i] < 1 || res[i] > best) {
cout << "Invalid: " << i << " --> " << res[i] << endl;
throw 1;
}
if (res[i] == res[i+1]) {
cout << "Cons " << i << endl;
throw 1;
}
}
memset(has, 0, sizeof has);
FOR(i,1,n) {
has[res[i]][res[i+1]] = has[res[i+1]][res[i]] = 1;
}
FOR(i,1,best) FOR(j,i+1,best) {
if (!has[i][j]) {
cout << "Missing " << i << ' ' << j << endl;
throw 1;
}
}
}
}