質問編集履歴

2

マージ関数を標準のものに差し替えました。

2020/05/16 01:26

投稿

majiponi
majiponi

スコア1720

test CHANGED
File without changes
test CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  ```C++
22
22
 
23
+ #include <iostream>
24
+
23
25
  #include <algorithm>
24
26
 
25
27
  #include <type_traits>
@@ -36,210 +38,188 @@
36
38
 
37
39
  template <class RandomAccessIterator, class Compare>
38
40
 
39
- void merge(const RandomAccessIterator first, const RandomAccessIterator mid, const RandomAccessIterator last, Compare comp)
41
+ void sort(const RandomAccessIterator first, const RandomAccessIterator last, Compare comp)
40
42
 
41
43
  {
42
44
 
45
+ std::size_t n = std::distance(first, last);
46
+
47
+ if(n < 2) return;
48
+
49
+ if(n != 5){
50
+
51
+ auto mid = first + n / 2;
52
+
53
+ practiceB::sort(first, mid, comp);
54
+
55
+ practiceB::sort(mid, last, comp);
56
+
57
+
58
+
43
- std::vector<typename std::remove_const<typename std::remove_reference<decltype(*first)>::type>::type> tmp(first, last);
59
+ std::vector<typename std::remove_const<typename std::remove_reference<decltype(*first)>::type>::type> tmp(n);
60
+
44
-
61
+ std::merge(first, mid, mid, last, tmp.begin(), comp);
62
+
45
- auto i = tmp.begin();
63
+ std::move(tmp.begin(), tmp.end(), first);
46
-
47
- auto l = tmp.end();
48
-
49
- auto m = i + std::distance(i, l) / 2;
50
-
51
- auto j = m;
52
-
53
- auto w = first;
54
-
55
- while(i != m && j != l){
56
-
57
- if(comp(*i, *j)) *w++ = *i++;
58
-
59
- else *w++ = *j++;
60
64
 
61
65
  }
62
66
 
67
+ else{
68
+
69
+ if(!comp(first[0], first[1])){
70
+
71
+ std::swap(first[0], first[1]);
72
+
73
+ }
74
+
75
+ if(!comp(first[2], first[3])){
76
+
77
+ std::swap(first[2], first[3]);
78
+
79
+ }
80
+
81
+ if(!comp(first[0], first[2])){
82
+
83
+ std::swap(first[0], first[2]);
84
+
85
+ std::swap(first[1], first[3]);
86
+
87
+ }
88
+
63
- while(i != m) *w++ = *i++;
89
+ if(comp(first[2], first[4])){
90
+
64
-
91
+ if(comp(first[3], first[4])){
92
+
93
+ if(comp(first[1], first[3])){
94
+
95
+ if(!comp(first[1], first[2])){
96
+
65
- while(j != l) *w++ = *j++;
97
+ std::swap(first[1], first[2]);
98
+
99
+ }
100
+
101
+ }
102
+
103
+ else{
104
+
105
+ std::swap(first[1], first[2]);
106
+
107
+ std::swap(first[2], first[3]);
108
+
109
+ if(!comp(first[3], first[4])){
110
+
111
+ std::swap(first[3], first[4]);
112
+
113
+ }
114
+
115
+ }
116
+
117
+ }
118
+
119
+ else{
120
+
121
+ if(comp(first[1], first[4])){
122
+
123
+ std::swap(first[3], first[4]);
124
+
125
+ if(!comp(first[1], first[2])){
126
+
127
+ std::swap(first[1], first[2]);
128
+
129
+ }
130
+
131
+ }
132
+
133
+ else{
134
+
135
+ std::swap(first[1], first[2]);
136
+
137
+ std::swap(first[2], first[4]);
138
+
139
+ if(!comp(first[3], first[4])){
140
+
141
+ std::swap(first[3], first[4]);
142
+
143
+ }
144
+
145
+ }
146
+
147
+ }
148
+
149
+ }
150
+
151
+ else{
152
+
153
+ if(comp(first[0], first[4])){
154
+
155
+ if(comp(first[1], first[2])){
156
+
157
+ std::swap(first[2], first[3]);
158
+
159
+ std::swap(first[2], first[4]);
160
+
161
+ if(!comp(first[1], first[2])){
162
+
163
+ std::swap(first[1], first[2]);
164
+
165
+ }
166
+
167
+ }
168
+
169
+ else{
170
+
171
+ std::swap(first[1], first[4]);
172
+
173
+ if(!comp(first[3], first[4])){
174
+
175
+ std::swap(first[3], first[4]);
176
+
177
+ }
178
+
179
+ }
180
+
181
+ }
182
+
183
+ else{
184
+
185
+ std::swap(first[0], first[4]);
186
+
187
+ std::swap(first[1], first[4]);
188
+
189
+ if(comp(first[2], first[4])){
190
+
191
+ if(!comp(first[3], first[4])){
192
+
193
+ std::swap(first[3], first[4]);
194
+
195
+ }
196
+
197
+ }
198
+
199
+ else{
200
+
201
+ std::swap(first[2], first[4]);
202
+
203
+ std::swap(first[3], first[4]);
204
+
205
+ }
206
+
207
+ }
208
+
209
+ }
210
+
211
+ }
66
212
 
67
213
  }
68
214
 
69
215
 
70
216
 
71
- template <class RandomAccessIterator, class Compare>
72
-
73
- void sort(const RandomAccessIterator first, const RandomAccessIterator last, Compare comp)
74
-
75
- {
76
-
77
- std::size_t n = std::distance(first, last);
78
-
79
- if(n < 2) return;
80
-
81
- if(n != 5){
82
-
83
- auto mid = first + n / 2;
84
-
85
- sort(first, mid, comp);
86
-
87
- sort(mid, last, comp);
88
-
89
- merge(first, mid, last, comp);
90
-
91
- }
92
-
93
- else{
94
-
95
- if(!comp(first[0], first[1])){
96
-
97
- std::swap(first[0], first[1]);
98
-
99
- }
100
-
101
- if(!comp(first[2], first[3])){
102
-
103
- std::swap(first[2], first[3]);
104
-
105
- }
106
-
107
- if(!comp(first[0], first[2])){
108
-
109
- std::swap(first[0], first[2]);
110
-
111
- std::swap(first[1], first[3]);
112
-
113
- }
114
-
115
- if(comp(first[2], first[4])){
116
-
117
- if(comp(first[3], first[4])){
118
-
119
- if(comp(first[1], first[3])){
120
-
121
- if(!comp(first[1], first[2])){
122
-
123
- std::swap(first[1], first[2]);
124
-
125
- }
126
-
127
- }
128
-
129
- else{
130
-
131
- std::swap(first[1], first[2]);
132
-
133
- std::swap(first[2], first[3]);
134
-
135
- if(!comp(first[3], first[4])){
136
-
137
- std::swap(first[3], first[4]);
138
-
139
- }
140
-
141
- }
142
-
143
- }
144
-
145
- else{
146
-
147
- if(comp(first[1], first[4])){
148
-
149
- std::swap(first[3], first[4]);
150
-
151
- if(!comp(first[1], first[2])){
152
-
153
- std::swap(first[1], first[2]);
154
-
155
- }
156
-
157
- }
158
-
159
- else{
160
-
161
- std::swap(first[1], first[2]);
162
-
163
- std::swap(first[2], first[4]);
164
-
165
- if(!comp(first[3], first[4])){
166
-
167
- std::swap(first[3], first[4]);
168
-
169
- }
170
-
171
- }
172
-
173
- }
174
-
175
- }
176
-
177
- else{
178
-
179
- if(comp(first[0], first[4])){
180
-
181
- if(comp(first[1], first[2])){
182
-
183
- std::swap(first[2], first[3]);
184
-
185
- std::swap(first[2], first[4]);
186
-
187
- if(!comp(first[1], first[2])){
188
-
189
- std::swap(first[1], first[2]);
190
-
191
- }
192
-
193
- }
194
-
195
- else{
196
-
197
- std::swap(first[1], first[4]);
198
-
199
- if(!comp(first[3], first[4])){
200
-
201
- std::swap(first[3], first[4]);
202
-
203
- }
204
-
205
- }
206
-
207
- }
208
-
209
- else{
210
-
211
- std::swap(first[0], first[4]);
212
-
213
- std::swap(first[1], first[4]);
214
-
215
- if(comp(first[2], first[4])){
216
-
217
- if(!comp(first[3], first[4])){
218
-
219
- std::swap(first[3], first[4]);
220
-
221
- }
222
-
223
- }
224
-
225
- else{
226
-
227
- std::swap(first[2], first[4]);
228
-
229
- std::swap(first[3], first[4]);
230
-
231
- }
232
-
233
- }
234
-
235
- }
236
-
237
- }
238
-
239
217
  }
240
218
 
241
219
 
242
220
 
243
- }
244
-
245
221
  ```
222
+
223
+
224
+
225
+ 5/16編集:標準テンプレートにmergeが実装されているらしいので、それを使うようにしました。関数が減り、見やすくはなりましたが依然としてn=5の場合がカオスです…。

1

2020/05/16 01:26

投稿

majiponi
majiponi

スコア1720

test CHANGED
File without changes
test CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
 
12
12
 
13
- の3点です。自力だと力業でゴリ押す汚いコードしか書けないものですから、コンテストに参加するにはまだまだ力不足と痛感しておりますが、ご教授いただけますと助かります。
13
+ の3点です。自力だと力業でゴリ押す汚いコードしか書けないものですから、コンテストに参加するにはまだまだ力不足と痛感しておりますが、ご~~教授~~教示いただけますと助かります。
14
-
15
-
16
-
14
+
15
+
16
+
17
- 問題文https://atcoder.jp/contests/practice/tasks/practice_2
17
+ [問題文](https://atcoder.jp/contests/practice/tasks/practice_2)
18
18
 
19
19
 
20
20