-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeforces-1040B.cpp
125 lines (68 loc) · 1.7 KB
/
Codeforces-1040B.cpp
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
117
118
119
120
121
122
123
124
125
/*By Eric Moynihan (TooFiveFive). I was in a hurry so this is probably not the most optimized solution but it works :) */
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
class Output {
public:
int actions;
vector<int> nums;
};
Output turn(int i, int n, int k) {
vector<int> nums;
int amount = 0;
for (int j = i; j <= n; j += 2*k + 1) {
nums.push_back(j);
amount += 1;
}
if (nums[nums.size() - 1] > (n - k - 1) && nums[0] <= (k + 1)) {
return {actions: amount, nums: nums};
} else {
return {actions: 0, nums: nums};
}
}
int main() {
int n, k;
cin >> n >> k;
vector<int> is;
int actions = n;
if (k > n) {
k = n;
}
if (k == 0) {
actions = n;
for (int i = 1; i <= n; i++) {
is.push_back(i);
}
} else if (k == n || k == n - 1) {
actions = 1;
is.push_back(1);
} else if (k == 1 && n % 3 == 0) {
actions = 0;
for (int j = 2; j < n; j += 3) {
is.push_back(j);
actions += 1;
}
} else if (k >= n / 2) {
actions = 1;
is.push_back(n - k);
}
else {
for (int i = 1; i < 2*k; ++i) {
Output newT = turn(i, n, k);
if (newT.actions < actions || actions == 0) {
if (newT.actions != 0) {
is = newT.nums;
actions = newT.actions;
}
}
}
}
cout << actions << "\n";
for (int i = 0; i < is.size(); i++) {
cout << is[i];
if (i < is.size() - 1) {
cout << " ";
}
}
return 0;
}