-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
561 lines (269 loc) · 239 KB
/
search.xml
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>最短路</title>
<link href="/2023/04/25/%E6%9C%80%E7%9F%AD%E8%B7%AF/"/>
<url>/2023/04/25/%E6%9C%80%E7%9F%AD%E8%B7%AF/</url>
<content type="html"><![CDATA[<h1 id="最短路">最短路</h1><h2 id="总结">总结</h2><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202304122040970.png" alt="image-20230412204030858" /><figcaption aria-hidden="true">image-20230412204030858</figcaption></figure><h2 id="dijkstra朴素">Dijkstra(朴素)</h2><h3 id="思路">思路:</h3><ol type="1"><li>n次循环,每次找到点集外最近的点</li><li>更新所有点最近距离</li></ol><p><a href="https://www.acwing.com/problem/content/851/">AcWing 849. Dijkstra求最短路 I</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">510</span>;</span><br><span class="line"><span class="keyword">int</span> g[N][N],d[N],st[N];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dj</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(d,<span class="number">0x3f</span>,<span class="keyword">sizeof</span> d);</span><br><span class="line"> d[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> t=<span class="number">-1</span>;</span><br><span class="line"> <span class="comment">//找到集合外最小距离</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=n;j++)</span><br><span class="line"> <span class="keyword">if</span>(!st[j] && (t==<span class="number">-1</span> || d[t]>d[j]))t=j;</span><br><span class="line"> st[t]=<span class="number">1</span>;</span><br><span class="line"> <span class="comment">//更新所有点</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=n;j++)</span><br><span class="line"> d[j]=<span class="built_in">min</span>(d[j],d[t]+g[t][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(d[n]==<span class="number">0x3f3f3f3f</span>)<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> d[n];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(g,<span class="number">0x3f</span>,<span class="keyword">sizeof</span> g);</span><br><span class="line"> </span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=m;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a,b,c;</span><br><span class="line"> cin>>a>>b>>c;</span><br><span class="line"> g[a][b]=<span class="built_in">min</span>(g[a][b],c);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> t=<span class="built_in">dj</span>();</span><br><span class="line"> cout<<t;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="dijkstra堆优化">Dijkstra(堆优化)</h2><h3 id="思路-1">思路:</h3><ol type="1"><li>n次循环,每次找到点集外最近的点(小根堆实现)</li><li>更新所有点最近距离</li></ol><p><a href="https://www.acwing.com/problem/content/852/">AcWing 850. Dijkstra求最短路 II</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1e6</span> + <span class="number">10</span>, M = N * <span class="number">2</span>;</span><br><span class="line"><span class="keyword">typedef</span> pair<<span class="keyword">int</span>, <span class="keyword">int</span>> PII;</span><br><span class="line"><span class="keyword">int</span> h[N], e[N], ne[N], w[N], idx;</span><br><span class="line"><span class="keyword">int</span> dist[N];</span><br><span class="line"><span class="keyword">bool</span> st[N];</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> c)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx ++;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dj</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(dist, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> dist);</span><br><span class="line"> dist[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> priority_queue<PII, vector<PII>, greater<PII>> heap;</span><br><span class="line"> heap.<span class="built_in">push</span>({<span class="number">0</span>,<span class="number">1</span>});</span><br><span class="line"> <span class="keyword">while</span>(heap.<span class="built_in">size</span>())</span><br><span class="line"> {</span><br><span class="line"> PII t = heap.<span class="built_in">top</span>();</span><br><span class="line"> heap.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="keyword">int</span> ver = t.second, distance = t.first;</span><br><span class="line"> <span class="keyword">if</span>(st[ver])<span class="keyword">continue</span>;</span><br><span class="line"> st[ver] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = h[ver]; i != <span class="number">-1</span>; i = ne[i])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> j = e[i];</span><br><span class="line"> <span class="keyword">if</span>(dist[j] > distance + w[i])</span><br><span class="line"> {</span><br><span class="line"> dist[j] = distance + w[i];</span><br><span class="line"> heap.<span class="built_in">push</span>({dist[j], j});</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(dist[n] == <span class="number">0x3f3f3f3f</span>)<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">return</span> dist[n];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(h, <span class="number">-1</span>, <span class="keyword">sizeof</span> h);</span><br><span class="line"> cin >> n >> m;</span><br><span class="line"> <span class="keyword">while</span>(m --)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> cin >> a >> b >> c;</span><br><span class="line"> <span class="built_in">add</span>(a, b, c);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> t = <span class="built_in">dj</span>();</span><br><span class="line"> cout << t <<endl;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="bellman-ford算法">bellman-ford算法</h2><h3 id="思路-2">思路</h3><ol type="1"><li>n次循环,循环所有边。</li><li>循环所有边时更新所有边</li></ol><p><a href="acwing.com/activity/content/problem/content/922/">AcWing 853. 有边数限制的最短路</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">510</span>, M = <span class="number">1e4</span> + <span class="number">10</span>;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> {</span></span><br><span class="line"> <span class="keyword">int</span> a, b, w;</span><br><span class="line">} h[M];</span><br><span class="line"><span class="keyword">int</span> dist[N],backcopy[N];</span><br><span class="line"><span class="keyword">int</span> n, m ,k;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">bf</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(dist, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> dist);</span><br><span class="line"> dist[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= k ; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">memcpy</span>(backcopy, dist, <span class="keyword">sizeof</span> backcopy);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= m; j ++)</span><br><span class="line"> {</span><br><span class="line"> dist[h[j].b] = <span class="built_in">min</span>(dist[h[j].b], backcopy[h[j].a] + h[j].w);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(dist[n] > <span class="number">0x3f3f3f3f</span> / <span class="number">2</span>)<span class="built_in">puts</span>(<span class="string">"impossible"</span>);</span><br><span class="line"> <span class="keyword">else</span> cout << dist[n];</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin >> n >> m >> k;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= m; i ++)</span><br><span class="line"> {</span><br><span class="line"> cin >> h[i].a >> h[i].b >> h[i].w;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">bf</span>();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="spfa算法">spfa算法</h4><h3 id="思路-3">思路</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">queue <- 初点</span><br><span class="line"></span><br><span class="line">while(queue不空)</span><br><span class="line">t <- q.front();</span><br><span class="line">q.pop();</span><br><span class="line">// 更新t的所有出边</span><br><span class="line">t -w-> b </span><br><span class="line">(如果b变小了)queue <- b</span><br></pre></td></tr></table></figure><p><a href="https://www.acwing.com/activity/content/problem/content/920/">AcWing 851. spfa求最短路</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1e5</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> h[N], e[N], ne[N], w[N], idx;</span><br><span class="line"><span class="keyword">int</span> dist[N];</span><br><span class="line"><span class="keyword">bool</span> st[N];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> c)</span></span>{</span><br><span class="line"> e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx ++;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">spfa</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(dist, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> dist);</span><br><span class="line"> dist[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> queue<<span class="keyword">int</span>> q;</span><br><span class="line"> q.<span class="built_in">push</span>(<span class="number">1</span>);</span><br><span class="line"> st[<span class="number">1</span>] = <span class="literal">true</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">while</span>(q.<span class="built_in">size</span>())</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> t = q.<span class="built_in">front</span>();</span><br><span class="line"> q.<span class="built_in">pop</span>();</span><br><span class="line"> </span><br><span class="line"> st[t] = <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = h[t]; i != <span class="number">-1</span>; i = ne[i])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> j = e[i];</span><br><span class="line"> <span class="keyword">if</span>(dist[j] > dist[t] + w[i])</span><br><span class="line"> {</span><br><span class="line"> dist[j] = dist[t] + w[i];</span><br><span class="line"> <span class="keyword">if</span>(!st[t])</span><br><span class="line"> {</span><br><span class="line"> st[j] = <span class="literal">true</span>;</span><br><span class="line"> q.<span class="built_in">push</span>(j); </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> dist[n];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(h, <span class="number">-1</span>, <span class="keyword">sizeof</span> h);</span><br><span class="line"> cin >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= m; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> cin >> a >> b >> c;</span><br><span class="line"> <span class="built_in">add</span>(a, b, c);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> t = <span class="built_in">spfa</span>();</span><br><span class="line"> <span class="keyword">if</span>(t > <span class="number">0x3f3f3f3f</span> / <span class="number">2</span>)cout << <span class="string">"impossible"</span>;</span><br><span class="line"> <span class="keyword">else</span> cout << t;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="floyd">Floyd</h2><h3 id="思路-4">思路</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">for(k 1~n)</span><br><span class="line">for(i 1~n)</span><br><span class="line">for(j 1~n)</span><br><span class="line">g[i][j] = min(g[i][j], g[i][k] + g[k][j]);</span><br></pre></td></tr></table></figure><p><a href="https://www.acwing.com/problem/content/856/">AcWing 854. Floyd求最短路</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">210</span>;</span><br><span class="line"><span class="keyword">int</span> g[N][N];</span><br><span class="line"><span class="keyword">int</span> n, m, t;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">floyd</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= n; k ++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= n; j ++)</span><br><span class="line"> g[i][j] = <span class="built_in">min</span>(g[i][j], g[i][k] + g[k][j]);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin >> n >> m >> t;</span><br><span class="line"> </span><br><span class="line"> <span class="built_in">memset</span>(g, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> g);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)g[i][i] = <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= m; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> cin >> a >> b >> c;</span><br><span class="line"> g[a][b] = <span class="built_in">min</span>(g[a][b], c);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">floyd</span>();</span><br><span class="line"> <span class="keyword">while</span>(t --)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a, b;</span><br><span class="line"> cin >> a >> b;</span><br><span class="line"> <span class="keyword">if</span>(g[a][b] > <span class="number">1e9</span> / <span class="number">2</span>)<span class="built_in">puts</span>(<span class="string">"impossible"</span>);</span><br><span class="line"> <span class="keyword">else</span> cout << g[a][b] << endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 图论 </tag>
<tag> 最短路 </tag>
</tags>
</entry>
<entry>
<title>slidev</title>
<link href="/2023/04/19/slidev/"/>
<url>/2023/04/19/slidev/</url>
<content type="html"><![CDATA[<h1 id="slidev">Slidev</h1><p><a href="https://cn.sli.dev">官网</a></p><p>介于官网有相应文档,一些东西不会很详细,主打的是个人使用的坑之类的。</p><h2 id="安装使用">安装使用</h2><h3 id="本地安装">本地安装</h3><h4 id="安装node.js">1. 安装node.js</h4><p>略</p><h4 id="安装slidev">2.安装Slidev</h4><p>使用 NPM:</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> npm init slidev@latest</span></span><br></pre></td></tr></table></figure><p>使用Yarn:</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> yarn create slidev</span></span><br></pre></td></tr></table></figure><p>跟随命令行的提示,它将自动为你打开幻灯片,网址是 http://localhost:3030/。</p><p>同时包含了一些基本配置和简单的 demo,为你说明如何开始使用 Slidev。</p><h3 id="构建单网页应用">构建单网页应用</h3><p>因为build完之后可以在 <code>dist</code> 目录下将你的幻灯片生成静态 HTML 文件。</p><p>所以怎么放都可以</p><h4 id="硬放到服务器上">硬放到服务器上</h4><ol type="1"><li>完成PPT</li><li>执行命令<code>slidev --base 你需要部署到的位置</code></li><li>粘(传)到服务器上(dist目录内所有),配置nginx。</li><li>访问相应IP或域名</li></ol><h5 id="例子">例子</h5><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">slidev --base /slidev</span><br><span class="line"></span><br><span class="line">配置完nginx后访问:</span><br><span class="line"> IP/slidev</span><br><span class="line"> 域名/slidev (配置域名)</span><br></pre></td></tr></table></figure><h3 id="导出pdf">导出(PDF)</h3><p>安装 <code>playwright-chromium</code>:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ npm i -D playwright-chromium</span><br></pre></td></tr></table></figure><p>接着,使用如下命令即可将你的幻灯片导出为 PDF:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ slidev export</span><br></pre></td></tr></table></figure><p>稍作等待,即可在 <code>./slides-export.pdf</code> 路径下看到你幻灯片的 PDF 文件。</p><p>如果你想要导出使用暗色主题的幻灯片,请使用 <code>--dark</code> 选项:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ slidev export --dark</span><br></pre></td></tr></table></figure><h4 id="导出点击步骤">导出点击步骤</h4><p>默认情况下,Slidev 会将每张幻灯片导出为 1 页,并忽略点击动画。如果你想将多个步骤的幻灯片,分解为多个页面,请使用 <code>--with-clicks</code> 选项。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ slidev export --with-clicks</span><br></pre></td></tr></table></figure><h4 id="pngs">PNGs</h4><p>当为命令传入 <code>--format png</code> 选项时,Slidev 会将每张幻灯片导出为 PNG 图片格式。</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ slidev export --format png</span><br></pre></td></tr></table></figure><h2 id="vscode插件推荐slidev">VSCode插件推荐:slidev</h2><p><a href="https://marketplace.visualstudio.com/items?itemName=antfu.slidev">slidev插件网址</a></p><p>优点:</p><ol type="1"><li>代码中显示这是 第几页 幻灯片</li><li>侧边栏上半:可以显示每一页幻灯片名字及快速转到某一页幻灯片</li><li>侧边栏下半:slidev打开后可以实时显示幻灯片(VSCode内,不用再开浏览器)</li></ol><h2 id="markdown-语法">Markdown 语法</h2><h3 id="分隔符">分隔符</h3><p>使用<code>---</code>来分割幻灯片。</p><h6 id="两行-分隔符上下间隔两行不然认不出来直接寄">两行 分隔符上下间隔两行,不然认不出来直接寄</h6><h3 id="扉页和布局">扉页和布局</h3><p>扉页尽量贴着前后的<code>---</code></p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="attr">layout:</span> <span class="string">cover</span></span><br><span class="line"><span class="meta">---</span></span><br></pre></td></tr></table></figure><p><code>yaml</code>格式的对象,语法如下:</p><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># 主题id 或 主题包名称</span></span><br><span class="line"><span class="comment"># 了解更多:https://sli.dev/themes/use.html</span></span><br><span class="line"><span class="attr">theme:</span> <span class="string">'default'</span></span><br><span class="line"><span class="comment"># 幻灯片的总标题,如果没有指定,那么将以第一张拥有标题的幻灯片的标题作为总标题</span></span><br><span class="line"><span class="attr">title:</span> <span class="string">'Slidev'</span></span><br><span class="line"><span class="comment"># titleTemplate for the webpage, `%s` will be replaced by the page's title</span></span><br><span class="line"><span class="attr">titleTemplate:</span> <span class="string">'%s - Slidev'</span></span><br><span class="line"><span class="comment"># information for your slides, can be a markdown string</span></span><br><span class="line"><span class="attr">info:</span> <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在单页(SPA)构建中启用 pdf 下载,也可以指定一个自定义 url</span></span><br><span class="line"><span class="attr">download:</span> <span class="literal">false</span></span><br><span class="line"><span class="comment"># 要导出文件的文件名称</span></span><br><span class="line"><span class="attr">exportFilename:</span> <span class="string">'slidev-exported.pdf'</span></span><br><span class="line"><span class="comment"># 语法高亮设置,可以使用 'prism' 或 'shiki' 方案</span></span><br><span class="line"><span class="attr">highlighter:</span> <span class="string">'prism'</span></span><br><span class="line"><span class="comment"># 在代码块中显示行号</span></span><br><span class="line"><span class="attr">lineNumbers:</span> <span class="literal">false</span></span><br><span class="line"><span class="comment"># 启用 monaco 编辑器,可以是 boolean,'dev' 或者 'build'</span></span><br><span class="line"><span class="attr">monaco:</span> <span class="string">'dev'</span></span><br><span class="line"><span class="comment"># 使用 vite-plugin-remote-assets 在本地下载远程资源,可以是 boolean,'dev' 或者 'build'</span></span><br><span class="line"><span class="attr">remoteAssets:</span> <span class="literal">false</span></span><br><span class="line"><span class="comment"># 控制幻灯片中的文本是否可以选择</span></span><br><span class="line"><span class="attr">selectable:</span> <span class="literal">true</span></span><br><span class="line"><span class="comment"># 启用幻灯片录制,可以是 boolean,'dev' 或者 'build'</span></span><br><span class="line"><span class="attr">record:</span> <span class="string">'dev'</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 幻灯片的配色方案,可以使用 'auto','light' 或者 'dark'</span></span><br><span class="line"><span class="attr">colorSchema:</span> <span class="string">'auto'</span></span><br><span class="line"><span class="comment"># vue-router 模式,可以使用 'history' 或 'hash' 模式</span></span><br><span class="line"><span class="attr">routerMode:</span> <span class="string">'history'</span></span><br><span class="line"><span class="comment"># 幻灯片的长宽比</span></span><br><span class="line"><span class="attr">aspectRatio:</span> <span class="string">'16/9'</span></span><br><span class="line"><span class="comment"># canvas 的真实宽度,单位为 px</span></span><br><span class="line"><span class="attr">canvasWidth:</span> <span class="number">980</span></span><br><span class="line"><span class="comment"># 用于主题定制,会将属性 `x` 注入根样式 `--slidev-theme-x`</span></span><br><span class="line"><span class="attr">themeConfig:</span></span><br><span class="line"> <span class="attr">primary:</span> <span class="string">'#5d8392'</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># favicon 可以是本地文件路径,也可以是一个 URL</span></span><br><span class="line"><span class="attr">favicon:</span> <span class="string">'https://cdn.jsdelivr.net/gh/slidevjs/slidev/assets/favicon.png'</span></span><br><span class="line"><span class="comment"># 用于渲染图表的 PlantUML 服务器的 URL</span></span><br><span class="line"><span class="attr">plantUmlServer:</span> <span class="string">'https://www.plantuml.com/plantuml'</span></span><br><span class="line"><span class="comment"># 字体将从 Google 字体自动导入</span></span><br><span class="line"><span class="comment"># 了解更多:https://sli.dev/custom/fonts</span></span><br><span class="line"><span class="attr">fonts:</span></span><br><span class="line"> <span class="attr">sans:</span> <span class="string">'Roboto'</span></span><br><span class="line"> <span class="attr">serif:</span> <span class="string">'Roboto Slab'</span></span><br><span class="line"> <span class="attr">mono:</span> <span class="string">'Fira Code'</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 为所有幻灯片添加默认的 frontmatter</span></span><br><span class="line"><span class="attr">defaults:</span></span><br><span class="line"> <span class="attr">layout:</span> <span class="string">'default'</span></span><br><span class="line"> <span class="comment"># ...</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 绘制选项</span></span><br><span class="line"><span class="comment"># 了解更多:https://sli.dev/guide/drawing.html</span></span><br><span class="line"><span class="attr">drawings:</span></span><br><span class="line"> <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line"> <span class="attr">persist:</span> <span class="literal">false</span></span><br><span class="line"> <span class="attr">presenterOnly:</span> <span class="literal">false</span></span><br><span class="line"> <span class="attr">syncAll:</span> <span class="literal">true</span></span><br><span class="line"><span class="meta">---</span></span><br></pre></td></tr></table></figure><p>布局很常用,下单列。</p><h3 id="代码块">代码块</h3><p>基础同Markdown</p><h4 id="通过在语言名后写大括号的形式来指定特定行高亮">通过在语言名后写大括号的形式来指定特定行高亮<code>{}</code></h4><p>注:行号从1开始。</p><h3 id="内联样式">内联样式</h3><p>你可以在 Markdown 中直接使用 <code><style></code> 标签来覆盖<strong>当前幻灯片</strong>的样式。</p><p>Slidev支持windicss的原子化写法和指令集 例如<code>@apply</code></p><h3 id="静态资源">静态资源</h3><p>和编写 Markdown 的方式一样,你可以使用本地或远程的 URL 的图片。</p><p>远程资源会缓存</p><p>本地资源请放到public文件夹</p>]]></content>
<categories>
<category> slidev </category>
</categories>
<tags>
<tag> slidev </tag>
</tags>
</entry>
<entry>
<title>hexo使用</title>
<link href="/2023/04/12/hexo%E4%BD%BF%E7%94%A8/"/>
<url>/2023/04/12/hexo%E4%BD%BF%E7%94%A8/</url>
<content type="html"><![CDATA[<h2 id="hexo使用">hexo使用</h2><p>新建文章:<code>hexo n 文章名</code></p><p>清除文章:<code>hexo clean</code></p><p>生成:<code>hexo g</code></p><p>更新页面:<code>hexo d</code>或<code>hexo deploy</code></p><p>生成+更新页面:<code>hexo g -d</code></p><p>端口部署:<code>hexo s -p 555</code></p><h2 id="hexo相关可能">hexo相关(可能)</h2><h3 id="git-bash-here">git Bash here</h3><h4 id="typora打开文件">Typora打开文件:</h4><p><code>start 文件名</code></p><h3 id="部署">部署</h3><h4 id="githubgitee部署省略">github、gitee部署省略</h4><h4 id="服务器部署没用自动部署github钩子那种">服务器部署(没用自动部署,github钩子那种)</h4><ul><li><code>git pull</code>到服务器上。</li><li>改nginx使其可以被展示出来。(详见<a href="#">占位</a>)</li></ul><h5 id="缺点">缺点:</h5><ul><li>每次更新需要去服务器上 'git pull' 和 <code>sudo nginx -s reload</code></li></ul><h5 id="改善可以写个shell脚本试试">改善:可以写个shell脚本试试</h5>]]></content>
<categories>
<category> hexo </category>
</categories>
<tags>
<tag> hexo </tag>
</tags>
</entry>
<entry>
<title>基础图论</title>
<link href="/2023/04/12/%E5%9F%BA%E7%A1%80%E5%9B%BE%E8%AE%BA/"/>
<url>/2023/04/12/%E5%9F%BA%E7%A1%80%E5%9B%BE%E8%AE%BA/</url>
<content type="html"><![CDATA[<h1 id="基础图论">基础图论</h1><h2 id="树与图的存储和遍历">树与图的存储和遍历</h2><h3 id="树与图的-存储">树与图的 存储</h3><p>因为树是无环连通图,所以可以只看图。而图分为无向图和有向图,无向图每条边可以表示为两条有向边,所以可以只看有向图。</p><h4 id="邻接表">1.邻接表</h4><p>用多个单链表构成的邻接表存储这个点指向的边(即出度),多用于稀疏图(n== m)。</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100010</span>,M=N*<span class="number">2</span>;</span><br><span class="line"><span class="keyword">int</span> h[N],e[M],ne[M],idx;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">e[idx]=b;ne[idx]=h[a],h[a]=idx++;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="built_in">memset</span>(h,<span class="number">-1</span>,<span class="keyword">sizeof</span> h);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="邻接矩阵">2.邻接矩阵</h4><p>用一个二维数组存储点边的关系,多用于稠密图(n == m^2)。</p><p>g[a] [b]指从a指向b的边,为0即为无边,非0即为有边(值可以存成权值等)</p><p>缺点:不能存重边。</p><h3 id="树与图的-遍历">树与图的 遍历</h3><h4 id="dfs">DFS</h4><p>同普通DFS</p><p><a href="https://www.acwing.com/problem/content/848/">AcWing 846. 树的重心</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100010</span>,M=N*<span class="number">2</span>;</span><br><span class="line"><span class="keyword">int</span> h[N],e[M],ne[M],idx;</span><br><span class="line"><span class="keyword">int</span> ans=N,st[N];</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[idx]=b;ne[idx]=h[a];h[a]=idx++;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> u)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> st[u]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> sum=<span class="number">1</span>,res=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=h[u];i!=<span class="number">-1</span>;i=ne[i])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> j=e[i];</span><br><span class="line"> <span class="keyword">if</span>(!st[j])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> s=<span class="built_in">dfs</span>(j);</span><br><span class="line"> res=<span class="built_in">max</span>(res,s);</span><br><span class="line"> sum+=s;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> res=<span class="built_in">max</span>(res,n-sum);</span><br><span class="line"> ans=<span class="built_in">min</span>(ans,res);</span><br><span class="line"> <span class="keyword">return</span> sum;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="built_in">memset</span>(h,<span class="number">-1</span>,<span class="keyword">sizeof</span> h);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<n;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a,b;cin>>a>>b;</span><br><span class="line"> <span class="built_in">add</span>(a,b);<span class="built_in">add</span>(b,a);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">dfs</span>(<span class="number">1</span>);</span><br><span class="line"> cout<<ans;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="bfs">BFS</h4><p>同普通BFS</p><p><a href="https://www.acwing.com/problem/content/849/">AcWing 847. 图中点的层次</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100010</span>,M=N*<span class="number">2</span>;</span><br><span class="line"><span class="keyword">int</span> h[N],e[M],ne[M],idx;</span><br><span class="line"><span class="keyword">int</span> d[N];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[idx]=b;ne[idx]=h[a];h[a]=idx++;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">bfs</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> queue<<span class="keyword">int</span>> q;</span><br><span class="line"> q.<span class="built_in">push</span>(<span class="number">1</span>);</span><br><span class="line"> <span class="built_in">memset</span>(d,<span class="number">-1</span>,<span class="keyword">sizeof</span> d);</span><br><span class="line"> d[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(q.<span class="built_in">size</span>())</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> t=q.<span class="built_in">front</span>();</span><br><span class="line"> q.<span class="built_in">pop</span>();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=h[t];i!=<span class="number">-1</span>;i=ne[i])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> j=e[i];</span><br><span class="line"> <span class="keyword">if</span>(d[j]!=<span class="number">-1</span>)<span class="keyword">continue</span>;</span><br><span class="line"> d[j]=d[t]+<span class="number">1</span>;</span><br><span class="line"> q.<span class="built_in">push</span>(j);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> d[n];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="built_in">memset</span>(h,<span class="number">-1</span>,<span class="keyword">sizeof</span> h);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=m;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a,b;cin>>a>>b;</span><br><span class="line"> <span class="built_in">add</span>(a,b);</span><br><span class="line"> }</span><br><span class="line"> cout<<<span class="built_in">bfs</span>();</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="常用算法">常用算法</h2><h3 id="拓扑排序">拓扑排序</h3><p>有向无环图。(bfs)</p><h4 id="做法">做法</h4><p>类比多元bfs</p><ol type="1"><li>将所有入度为0的点放入queue。</li><li>遍历queue每个点,将点所有出度对应的点入读-1。</li><li>将入读为0的点再放到queue中。</li><li>最后判断所有的点入度是否都是0。</li></ol><p><a href="https://www.acwing.com/problem/content/850/">AcWing 848. 有向图的拓扑序列</a></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100010</span>;</span><br><span class="line"><span class="keyword">int</span> h[N],e[N],ne[N],idx;</span><br><span class="line"><span class="keyword">int</span> d[N],c[N],num;</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line">queue<<span class="keyword">int</span>> q;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[idx]=b;ne[idx]=h[a];h[a]=idx++;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">bfs</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">while</span>(q.<span class="built_in">size</span>())</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> t=q.<span class="built_in">front</span>();</span><br><span class="line"> q.<span class="built_in">pop</span>();</span><br><span class="line"> c[++num]=t;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=h[t];i!=<span class="number">-1</span>;i=ne[i])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> j=e[i];</span><br><span class="line"> d[j]--;</span><br><span class="line"> <span class="keyword">if</span>(!d[j])q.<span class="built_in">push</span>(j);</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin>>n>>m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)h[i]=<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=m;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a,b;cin>>a>>b;</span><br><span class="line"> <span class="built_in">add</span>(a,b);</span><br><span class="line"> d[b]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)<span class="keyword">if</span>(!d[i])q.<span class="built_in">push</span>(i);</span><br><span class="line"> <span class="built_in">bfs</span>();</span><br><span class="line"> <span class="keyword">if</span>(num!=n)cout<<<span class="string">"-1"</span>;</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)cout<<c[i]<<<span class="string">' '</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="最短路">最短路</h3><p>详见最短路。</p><h4 id="总结">总结</h4><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202304122040970.png" alt="image-20230412204030858" /><figcaption aria-hidden="true">image-20230412204030858</figcaption></figure><h4 id="dijkstra朴素">Dijkstra(朴素)</h4><h5 id="思路">思路:</h5><ol type="1"><li>n次循环,每次找到点集外最近的点</li><li>更新所有点最近距离</li></ol><h4 id="dijkstra堆优化">Dijkstra(堆优化)</h4><h5 id="思路-1">思路:</h5><ol type="1"><li>n次循环,每次找到点集外最近的点(小根堆实现)</li><li>更新所有点最近距离</li></ol><h4 id="bellman-ford算法">bellman-ford算法</h4><h5 id="思路-2">思路:</h5><ol type="1"><li>n次循环,循环所有边。</li><li>循环所有边时更新所有边</li></ol><h4 id="spfa算法">spfa算法</h4><h5 id="思路-3">思路:</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">queue <- 初点</span><br><span class="line"></span><br><span class="line">while(queue不空)</span><br><span class="line">t <- q.front();</span><br><span class="line">q.pop();</span><br><span class="line">// 更新t的所有出边</span><br><span class="line">t -w-> b </span><br><span class="line">(如果b变小了)queue <- b</span><br><span class="line"></span><br></pre></td></tr></table></figure><h4 id="floyd">Floyd</h4><h5 id="思路-4">思路</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">for(k 1~n)</span><br><span class="line">for(i 1~n)</span><br><span class="line">for(j 1~n)</span><br><span class="line">g[i][j] = min(g[i][j], g[i][k] + g[k][j]);</span><br></pre></td></tr></table></figure><h3 id="最小生成树">最小生成树</h3><h4 id="prim">Prim</h4><h4 id="kruskal">Kruskal</h4><h3 id="其它">其它</h3><h4 id="染色法判定二分图">染色法判定二分图</h4><h4 id="匈牙利算法">匈牙利算法</h4>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 图论 </tag>
</tags>
</entry>
<entry>
<title>第十四届CB蓝桥省赛</title>
<link href="/2023/04/11/%E7%AC%AC%E5%8D%81%E5%9B%9B%E5%B1%8ACB%E8%93%9D%E6%A1%A5%E7%9C%81%E8%B5%9B/"/>
<url>/2023/04/11/%E7%AC%AC%E5%8D%81%E5%9B%9B%E5%B1%8ACB%E8%93%9D%E6%A1%A5%E7%9C%81%E8%B5%9B/</url>
<content type="html"><![CDATA[<h1 id="第十四届蓝桥杯大赛软件赛省赛">第十四届蓝桥杯大赛软件赛省赛</h1><p>C/C++ 大学 B 组</p><h2 id="试题a日期统计">试题A日期统计</h2><h3 id="题目">题目</h3><p><a href="#">题目链接</a></p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202304110850392.png" alt="image-20230411085046212" /><figcaption aria-hidden="true">image-20230411085046212</figcaption></figure><p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">5 6 8 6 9 1 6 1 2 4 9 1 9 8 2 3 6 4 7 7 5 9 5 0 3 8 7 5 8 1 5 8 6 1 8 3 0 3 7 9 2</span><br><span class="line">7 0 5 8 8 5 7 0 9 9 1 9 4 4 6 8 6 3 3 8 5 1 6 3 4 6 7 0 7 8 2 7 6 8 9 5 6 5 6 1 4 0 1</span><br><span class="line">0 0 9 4 8 0 9 1 2 8 5 0 2 5 3 3</span><br></pre></td></tr></table></figure></p><h4 id="答案235">答案:235</h4><h3 id="思路">思路</h3><h4 id="考场思路">考场思路:</h4><p>暴力2023xxxx约是O(100<sup>8</sup>)?铁T,根本跑不完。</p><p>又因为2023不变,所以先找到2023(眼看,第58个是3,前面出现了202子序列),再对后面遍历所有子序列情况,存到map里。O(41<sup>4</sup>)</p><p>最后遍历所有日期,map出现过就res++。</p><h4 id="代码">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">100</span>];</span><br><span class="line">map<<span class="keyword">int</span>,<span class="keyword">int</span>> mp;</span><br><span class="line"><span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> days[<span class="number">13</span>] = {<span class="number">0</span>, <span class="number">31</span>, <span class="number">28</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="built_in">freopen</span>(<span class="string">"A.txt"</span>,<span class="string">"r"</span>, stdin);</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">41</span>; i ++)</span><br><span class="line">{</span><br><span class="line">cin >> a[i];</span><br><span class="line">}</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i1 = <span class="number">1</span>; i1 <= <span class="number">41</span>; i1 ++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i2 = i1 + <span class="number">1</span>; i2 <= <span class="number">41</span>; i2 ++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i3 = i2 + <span class="number">1</span>; i3 <= <span class="number">41</span>; i3 ++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i4 = i3 + <span class="number">1</span>; i4 <= <span class="number">41</span>; i4 ++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">int</span> num = a[i1] * <span class="number">1000</span> + a[i2] * <span class="number">100</span> + a[i3] * <span class="number">10</span> + a[i4];</span><br><span class="line">mp[num] = <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">} </span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">12</span>; i ++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= days[i]; j ++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(mp[<span class="number">100</span> * i + j] == <span class="number">1</span>)res ++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">cout << res;</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="可能正解">可能正解:</h4><p>对所有天数判断。O(365*100)</p><h4 id="代码-1">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">1000</span>];</span><br><span class="line"><span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> days[<span class="number">13</span>] = {<span class="number">0</span>, <span class="number">31</span>, <span class="number">28</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>};</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">check</span><span class="params">(string s)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> num = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">100</span>; i ++)</span><br><span class="line">{</span><br><span class="line"><span class="comment">// 转成string才能比较 </span></span><br><span class="line">string d = <span class="built_in">to_string</span>(a[i]);</span><br><span class="line"><span class="keyword">if</span>(s[num] == d[<span class="number">0</span>])num ++;</span><br><span class="line"><span class="keyword">if</span>(num > <span class="number">8</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="comment">// freopen("A2.txt","r", stdin);</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">100</span>; i ++)</span><br><span class="line">{</span><br><span class="line">cin >> a[i];</span><br><span class="line">}</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">12</span>; i ++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= days[i]; j ++)</span><br><span class="line">{</span><br><span class="line"> <span class="comment">// i,j用string存才不乱码</span></span><br><span class="line">string s = <span class="string">"02023"</span>;</span><br><span class="line"><span class="keyword">if</span>(i < <span class="number">10</span>)s += <span class="string">"0"</span>;</span><br><span class="line">s += <span class="built_in">to_string</span>(i);</span><br><span class="line"><span class="keyword">if</span>(j < <span class="number">10</span>)s += <span class="string">"0"</span>;</span><br><span class="line">s += <span class="built_in">to_string</span>(j);</span><br><span class="line"><span class="keyword">if</span>(<span class="built_in">check</span>(s))res ++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">cout << res;</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="试题-b-01-串的熵">试题 B: 01 串的熵</h2><h3 id="题目-1">题目</h3><p><a href="#">题目链接</a></p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202304110944877.png" alt="image-20230411094436776" /><figcaption aria-hidden="true">image-20230411094436776</figcaption></figure><h3 id="思路-1">思路</h3><h4 id="考场思路-1">考场思路:</h4><p>看不懂,心态崩了不想写。</p><p>填个114514得了。</p><h4 id="可能正解暴力">可能正解:暴力</h4>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 蓝桥杯 </tag>
<tag> 竞赛复盘 </tag>
</tags>
</entry>
<entry>
<title>4275. Dijkstra序列</title>
<link href="/2023/04/04/4275-Dijkstra%E5%BA%8F%E5%88%97/"/>
<url>/2023/04/04/4275-Dijkstra%E5%BA%8F%E5%88%97/</url>
<content type="html"><![CDATA[<h1 id="dijkstra序列">4275.Dijkstra序列</h1><p><a href="https://www.acwing.com/problem/content/4278/">原题链接</a></p><h2 id="思路">思路</h2><p>Dijkstra板子改改(内部判断一下能否实现即可)</p><h2 id="代码">代码</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1010</span>;</span><br><span class="line"><span class="keyword">int</span> g[N][N];</span><br><span class="line"><span class="keyword">int</span> dist[N];</span><br><span class="line"><span class="keyword">bool</span> st[N];</span><br><span class="line"><span class="keyword">int</span> fl[N];</span><br><span class="line"><span class="keyword">int</span> n,m,k;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">dj</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(dist, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> dist);</span><br><span class="line"> <span class="comment">//这里st忘了复原了,所以我当时出错。</span></span><br><span class="line"> <span class="built_in">memset</span>(st, <span class="number">0</span>, <span class="keyword">sizeof</span> st);</span><br><span class="line"> dist[fl[<span class="number">1</span>]] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> t = <span class="number">1</span>; t <= n; t ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">// 我的写法</span></span><br><span class="line"> <span class="keyword">int</span> i = <span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= n; j ++)</span><br><span class="line"> <span class="keyword">if</span>(!st[j] && (i == <span class="number">-1</span> || dist[i] >= dist[j]))</span><br><span class="line"> { <span class="comment">// 需要判断是否是数列给的数,所以等于时需要加上</span></span><br><span class="line"> <span class="keyword">if</span>(dist[i] == dist[j])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(i != fl[t])i = j;</span><br><span class="line"> <span class="comment">// i == fl[t]时i不变即可</span></span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> i = j;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(i != fl[t])<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="comment">// y总写法</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">//int i = fl[i];</span></span><br><span class="line"> <span class="comment">// for(int j = 1; j <= n; j ++)</span></span><br><span class="line"> <span class="comment">// if(!st[j] && dist[i] >= dist[j])</span></span><br><span class="line"> <span class="comment">// { </span></span><br><span class="line"> <span class="comment">// return false;</span></span><br><span class="line"> <span class="comment">// }</span></span><br><span class="line"> </span><br><span class="line"> st[i] = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= n; j ++)</span><br><span class="line"> {</span><br><span class="line"> dist[j] = <span class="built_in">min</span>(dist[j], dist[i] + g[i][j]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memset</span>(g, <span class="number">0x3f</span>, <span class="keyword">sizeof</span> g);</span><br><span class="line"> cin >> n >> m;</span><br><span class="line"> <span class="keyword">while</span>(m --)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> cin >> a >> b >> c;</span><br><span class="line"> g[a][b] = <span class="built_in">min</span>(g[a][b], c);</span><br><span class="line"> g[b][a] = <span class="built_in">min</span>(g[b][a], c);</span><br><span class="line"> }</span><br><span class="line"> cin >> k;</span><br><span class="line"> <span class="keyword">while</span>(k --)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)cin >> fl[i];</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">dj</span>())cout << <span class="string">"Yes"</span> << endl;</span><br><span class="line"> <span class="keyword">else</span> cout << <span class="string">"No"</span> << endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
<tag> Dijkstra </tag>
</tags>
</entry>
<entry>
<title>3777. 砖块(每日一题)</title>
<link href="/2023/03/30/3777-%E7%A0%96%E5%9D%97%EF%BC%88%E6%AF%8F%E6%97%A5%E4%B8%80%E9%A2%98%EF%BC%89/"/>
<url>/2023/03/30/3777-%E7%A0%96%E5%9D%97%EF%BC%88%E6%AF%8F%E6%97%A5%E4%B8%80%E9%A2%98%EF%BC%89/</url>
<content type="html"><![CDATA[<h1 id="acwing-3777.-砖块每日一题">AcWing 3777. 砖块(每日一题)</h1><p><a href="https://www.acwing.com/problem/content/3780/">原题链接</a></p><h2 id="思路">思路</h2><h3 id="审题判断出当b砖和w砖都为奇数个时不成立">1.审题:判断出当B砖和W砖都为奇数个时不成立</h3><p>每次翻转会使两个砖变为另一种颜色或两个砖交换颜色。所以颜色都为奇数个时将无法翻转成同一种颜色。</p><h3 id="暴力-判暴力复杂度">2.暴力 + 判暴力复杂度</h3><p>思路:分别判断全为B或全为W时需要多少步。</p><p>方法:</p><ol type="1"><li>从左到右==-1个==判断是否为需要颜色,不是就翻一下它和它后面的(-1的由来)。</li><li>判断最后一个和前面颜色是否相同,不同则选择另一个(下题解将此总数+1e7保证选到另一个)</li></ol><p>时间复杂度:O(nt)(t组数,n操作数)</p><h2 id="代码">代码</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">210</span>;</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line">string s,cp;</span><br><span class="line"><span class="keyword">int</span> num1[N],sum1 = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> num2[N],sum2 = <span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">change</span><span class="params">(<span class="keyword">int</span> i)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(s[i] == <span class="string">'B'</span>)s[i] = <span class="string">'W'</span>;</span><br><span class="line"> <span class="keyword">else</span> s[i] = <span class="string">'B'</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;cin >> t;</span><br><span class="line"> <span class="keyword">while</span>(t --)</span><br><span class="line"> {</span><br><span class="line"> m = <span class="number">0</span>,sum1 = sum2 = <span class="number">0</span>;</span><br><span class="line"> cin >> n >> s;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i ++)cp[i] = s[i];</span><br><span class="line"> <span class="comment">// cout << cp << endl;</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(s[i] == <span class="string">"B"</span>)m ++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(m % <span class="number">2</span> == <span class="number">1</span> && (n - m) % <span class="number">2</span> == <span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> cout << <span class="string">"-1"</span> << endl;</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 化成全B</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n - <span class="number">1</span>; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(s[i] != <span class="string">'B'</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">change</span>(i),<span class="built_in">change</span>(i + <span class="number">1</span>);</span><br><span class="line"> num1[++ sum1] = i + <span class="number">1</span>; </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(s[n<span class="number">-1</span>] == <span class="string">'W'</span>)sum1 +=<span class="number">1e7</span>;</span><br><span class="line"> <span class="comment">// 化成全W</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i ++)s[i] = cp[i];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n - <span class="number">1</span>; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(s[i] != <span class="string">'W'</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">change</span>(i),<span class="built_in">change</span>(i + <span class="number">1</span>);</span><br><span class="line"> num2[++ sum2] = i + <span class="number">1</span>; </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(s[n<span class="number">-1</span>] == <span class="string">'B'</span>)sum2 +=<span class="number">1e7</span>;</span><br><span class="line"> <span class="keyword">if</span>(sum1 < sum2)</span><br><span class="line"> {</span><br><span class="line"> cout << sum1 <<endl;</span><br><span class="line"> <span class="keyword">if</span>(sum1 != <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= sum1; i ++)cout << num1[i] << <span class="string">" "</span>;</span><br><span class="line"> cout << endl;</span><br><span class="line"> }</span><br><span class="line"> }<span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> cout << sum2 <<endl;</span><br><span class="line"> <span class="keyword">if</span>(sum2 != <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= sum2; i ++)cout << num2[i] << <span class="string">" "</span>;</span><br><span class="line"> cout << endl;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="问题我出的">问题(我出的)</h2><ol type="1"><li>memcpy不能对string用,要换成char s[N]</li></ol><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303302004174.png" alt="image-20230330200432110" /><figcaption aria-hidden="true">image-20230330200432110</figcaption></figure><ol start="2" type="1"><li>char,string的某一位s[i]与 字符 求 相等 时要用单引号而不是双引号!</li></ol><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303302003303.png" alt="image-20230330200339142" /><figcaption aria-hidden="true">image-20230330200339142</figcaption></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
<tag> 递推 </tag>
</tags>
</entry>
<entry>
<title>导数的计算</title>
<link href="/2023/03/19/%E5%AF%BC%E6%95%B0%E7%9A%84%E8%AE%A1%E7%AE%97/"/>
<url>/2023/03/19/%E5%AF%BC%E6%95%B0%E7%9A%84%E8%AE%A1%E7%AE%97/</url>
<content type="html"><![CDATA[<h1 id="导数的计算">导数的计算</h1><h2 id="导数表部分">导数表(部分)</h2>]]></content>
<categories>
<category> 考研 </category>
</categories>
<tags>
<tag> 数学 </tag>
<tag> 考研 </tag>
</tags>
</entry>
<entry>
<title>极限</title>
<link href="/2023/03/17/%E6%9E%81%E9%99%90/"/>
<url>/2023/03/17/%E6%9E%81%E9%99%90/</url>
<content type="html"><![CDATA[<h1 id="求极限">求极限</h1><h2 id="方法-先定型后定法定法之前先四化">方法: 先定型后定法,定法之前先四化</h2><h3 id="定型">定型</h3><p>未定式:化简 -> 定法</p><p>已定式:带入求极限</p><h5 id="化简">化简</h5><ol type="1"><li>非0因子可淡化</li><li>加减法中存在可拆化</li><li>"<span class="math inline">\(\sqrt{x}\)</span>"有理化</li><li>幂指函数幂指转换</li></ol><h5 id="定法">定法</h5><ol type="1"><li>等价无穷小代换(x->0时)</li><li>泰勒公式</li><li>洛必达法则</li><li>极限四则运算</li></ol><h2 id="种未定式题型">7种未定式(题型)</h2><h3 id="比0型">0比0型</h3><p><span class="math display">\[\frac{0}{0} \]</span></p><ol type="1"><li>等价无穷小代换(x->0时)</li><li>泰勒公式</li><li>洛必达法则</li><li>极限四则运算</li></ol><h3 id="infty比infty-型"><span class="math inline">\(\infty\)</span>比<span class="math inline">\(\infty\)</span> 型</h3><p><span class="math display">\[\frac{\infty}{\infty} \]</span></p><ol type="1"><li>抓大头</li><li>上下同除最大项</li><li>洛必达法则</li></ol><h3 id="infty型">0 * <span class="math inline">\(\infty\)</span>型</h3><ol type="1"><li><p>将其化为0比0 <span class="math display">\[\frac{ 0 }{ \frac{ 1 }{ \infty }} = \frac{0}{0} \]</span></p></li><li><p>将其化为<span class="math inline">\(\infty\)</span>比<span class="math inline">\(\infty\)</span> <span class="math display">\[\frac{\infty}{\frac{1}{\infty}} = \frac{\infty}{\infty} \]</span></p></li></ol><h3 id="infty---infty-型"><span class="math inline">\(\infty - \infty\)</span> 型</h3><ol type="1"><li>通分</li><li>使 <span class="math display">\[ x=\frac{1}{t} \]</span></li></ol><h3 id="infty型-1"><span class="math inline">\(1^{\infty}\)</span>型</h3><h4 id="重要公式">重要公式:</h4><p><span class="math inline">\(\lim f(x)^{g(x)} = e^{\lim g(x)\ln f(x)-1}\)</span></p><h3 id="infty和00-型"><span class="math inline">\(0^{\infty}\)</span>和<span class="math inline">\(0^{0}\)</span> 型</h3><p>幂指转换</p>]]></content>
<categories>
<category> 考研 </category>
</categories>
<tags>
<tag> 数学 </tag>
<tag> 考研 </tag>
</tags>
</entry>
<entry>
<title>1230. K倍区间</title>
<link href="/2023/02/18/1230-K%E5%80%8D%E5%8C%BA%E9%97%B4/"/>
<url>/2023/02/18/1230-K%E5%80%8D%E5%8C%BA%E9%97%B4/</url>
<content type="html"><![CDATA[<h1 id="k倍区间">1230. K倍区间</h1><p><a href="https://www.acwing.com/problem/content/1232/">原题链接</a></p><h2 id="思路">思路</h2><h4 id="前缀和应用">前缀和应用</h4><h3 id="正常想法on2">正常想法O(n^2)</h3><p>求前缀和后根据端点遍历另一个端点。</p><h3 id="想到求余数性质可优化到on">想到求余数(性质)可优化到O(n)</h3><p>前缀和是a1~ai的和,那么a? ~ ai的和的余数为0只要a1~a?的余数等于a? ~ai的余数即可。</p><p>所以用一个新数组存前缀和余数,然后直接根据判断满足的个数即可。</p><h5 id="相当于优化掉了根据端点遍历另一个端点的时间">相当于优化掉了根据端点遍历另一个端点的时间。</h5><h3 id="注意">注意</h3><h4 id="ll问题ai要用ll我这里k了可以不用">LL问题(a[i]要用LL,我这里%k了可以不用)</h4><h2 id="代码">代码</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1e5</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> n, k;</span><br><span class="line"><span class="keyword">int</span> a[N], c[N];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> cin >> n >> k;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)</span><br><span class="line"> {</span><br><span class="line"> cin >> a[i];</span><br><span class="line"> a[i] = (a[i] + a[i - <span class="number">1</span>]) % k;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line"> c[<span class="number">0</span>] ++;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)<span class="comment">//右端点 </span></span><br><span class="line"> {</span><br><span class="line"> res += c[a[i] % k];</span><br><span class="line"> c[a[i] % k] ++;</span><br><span class="line"> }</span><br><span class="line"> cout<< res;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
<tag> 前缀和 </tag>
</tags>
</entry>
<entry>
<title>3956. 截断数组</title>
<link href="/2023/02/15/3956-%E6%88%AA%E6%96%AD%E6%95%B0%E7%BB%84/"/>
<url>/2023/02/15/3956-%E6%88%AA%E6%96%AD%E6%95%B0%E7%BB%84/</url>
<content type="html"><![CDATA[<h1 id="截断数组">3956.截断数组</h1><p><a href="https://www.acwing.com/problem/content/3959/">原题链接</a></p><h2 id="思路">思路</h2><p>截两段,三数组各元素和相等。</p><p>求截断方法数。</p><ol start="0" type="1"><li>判断特殊情况<ol type="1"><li>输入时求总数sum,sum不是3的倍数直接输出0结束。</li><li>sum为0时,公式求解。</li><li>记得long long。</li></ol></li><li>前缀和求<code>a[1] ~ a[i]</code>是多少,求等于<code>sum/3</code>和<code>sum/3*2</code>的个数</li><li>从头到尾求方案数</li></ol><p>事件复杂度:O(n)</p><h2 id="代码">代码</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1e5</span> + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> a[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> cin >> n;</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)</span><br><span class="line"> {</span><br><span class="line"> cin >> a[i];</span><br><span class="line"> sum +=a[i];</span><br><span class="line"> a[i] += a[i - <span class="number">1</span>];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(n < <span class="number">3</span>)</span><br><span class="line"> {</span><br><span class="line"> cout << <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(sum % <span class="number">3</span> != <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> cout << <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(sum == <span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment"> 3 0 1 (n-2)</span></span><br><span class="line"><span class="comment"> 4 0 3 (n-2) +(n-3) </span></span><br><span class="line"><span class="comment"> ...</span></span><br><span class="line"><span class="comment"> n 0 res (1 + (n-2)) * (n-2) </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">int</span> num = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i] == sum)num ++;</span><br><span class="line"> }</span><br><span class="line"> cout << (<span class="keyword">long</span> <span class="keyword">long</span>)(<span class="number">1</span> + (num - <span class="number">2</span>)) * (num - <span class="number">2</span>) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> sum /= <span class="number">3</span>;</span><br><span class="line"> <span class="keyword">int</span> num1 = <span class="number">0</span>, num2 = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i] == sum)num1 ++;</span><br><span class="line"> <span class="keyword">if</span>(a[i] == sum * <span class="number">2</span>)num2 ++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// cout << num1 << " " << num2 << endl;</span></span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i] == sum)</span><br><span class="line"> {</span><br><span class="line"> res += num2;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(a[i] == sum * <span class="number">2</span>){</span><br><span class="line"> num2 --;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> cout << res;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
<tag> 前缀和 </tag>
</tags>
</entry>
<entry>
<title>hexo上线服务器</title>
<link href="/2023/02/14/hexo%E4%B8%8A%E7%BA%BF%E6%9C%8D%E5%8A%A1%E5%99%A8/"/>
<url>/2023/02/14/hexo%E4%B8%8A%E7%BA%BF%E6%9C%8D%E5%8A%A1%E5%99%A8/</url>
<content type="html"><![CDATA[<h1 id="hexo部署服务器">hexo部署服务器</h1><h2 id="实现原理">实现原理</h2><p>nginx应用</p><h2 id="步骤">步骤</h2><h3 id="上线github或gitee">上线github或gitee</h3><p>省略</p><h3 id="新建镜像">新建镜像</h3><p>有原来镜像(新建)直接</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -p 80:80 -p 443:443 -p 20025:22 -itd --name blog seal:latest</span><br></pre></td></tr></table></figure><h3 id="安装nginx">安装nginx</h3><p><code>sudo apt install nginx</code></p><h3 id="修改etcnginxsites-avaliable的default">修改/etc/nginx/sites-avaliable的default</h3><p><strong>sudo打开default</strong>(<em>谨防改完发现改不了诈骗</em>)</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ../../etc/nginx/sites-avaliable</span><br><span class="line">sudo vim default</span><br></pre></td></tr></table></figure><h5 id="修改部分">修改部分</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">root /var/www/html 改为 root hexo博客文件夹绝对目录。</span><br><span class="line">server_name _;添加IP或服务器域名。</span><br></pre></td></tr></table></figure><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202302141745326.png" alt="image-20230214174457128" /><figcaption aria-hidden="true">image-20230214174457128</figcaption></figure><h3 id="重启nginx">重启nginx</h3><p><code>nginx -s reload</code></p><h2 id="缺点">缺点</h2><p>每次都要</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 博客 </category>
</categories>
<tags>
<tag> 部署服务器 </tag>
</tags>
</entry>
<entry>
<title>95.费解的开关</title>
<link href="/2022/03/02/95.%E8%B4%B9%E8%A7%A3%E7%9A%84%E5%BC%80%E5%85%B3/"/>
<url>/2022/03/02/95.%E8%B4%B9%E8%A7%A3%E7%9A%84%E5%BC%80%E5%85%B3/</url>
<content type="html"><![CDATA[<h3 id="费解的开关">95.费解的开关</h3><p><a href="https://www.acwing.com/problem/content/description/97/">AcWing 95. 费解的开关</a></p><p>你玩过“拉灯”游戏吗?</p><p>25盏灯排成一个 5×5的方形。</p><p>每一个灯都有一个开关,游戏者可以改变它的状态。</p><p>每一步,游戏者可以改变某一个灯的状态。</p><p>游戏者改变一个灯的状态会产生连锁反应:和这个灯上下左右相邻的灯也要相应地改变其状态。</p><p>我们用数字 1表示一盏开着的灯,用数字 0表示关着的灯。</p><p>下面这种状态</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">10111</span><br><span class="line">01101</span><br><span class="line">10111</span><br><span class="line">10000</span><br><span class="line">11011</span><br></pre></td></tr></table></figure><p>在改变了最左上角的灯的状态后将变成:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">01111</span><br><span class="line">11101</span><br><span class="line">10111</span><br><span class="line">10000</span><br><span class="line">11011</span><br></pre></td></tr></table></figure><p>再改变它正中间的灯后状态将变成:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">01111</span><br><span class="line">11001</span><br><span class="line">11001</span><br><span class="line">10100</span><br><span class="line">11011</span><br></pre></td></tr></table></figure><p>给定一些游戏的初始状态,编写程序判断游戏者是否可能在 6步以内使所有的灯都变亮。</p><h4 id="输入格式">输入格式</h4><p>第一行输入正整数 <em>n</em>,代表数据中共有 <em>n</em>个待解决的游戏初始状态。</p><p>以下若干行数据分为 <em>n</em>组,每组数据有 5 行,每行 5个字符。</p><p>每组数据描述了一个游戏的初始状态。</p><p>各组数据间用一个空行分隔。</p><h4 id="输出格式">输出格式</h4><p>一共输出 <em>n</em>行数据,每行有一个小于等于 6的整数,它表示对于输入数据中对应的游戏状态最少需要几步才能使所有灯变亮。</p><p>对于某一个游戏初始状态,若 6步以内无法使所有灯变亮,则输出 −1。</p><h4 id="数据范围">数据范围</h4><p>0<<em>n</em>≤500</p><h4 id="输入样例">输入样例:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">3</span><br><span class="line">00111</span><br><span class="line">01011</span><br><span class="line">10001</span><br><span class="line">11010</span><br><span class="line">11100</span><br><span class="line"></span><br><span class="line">11101</span><br><span class="line">11101</span><br><span class="line">11110</span><br><span class="line">11111</span><br><span class="line">11111</span><br><span class="line"></span><br><span class="line">01111</span><br><span class="line">11111</span><br><span class="line">11111</span><br><span class="line">11111</span><br><span class="line">11111</span><br></pre></td></tr></table></figure><h4 id="输出样例">输出样例:</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">3</span><br><span class="line">2</span><br><span class="line">-1</span><br></pre></td></tr></table></figure><h4 id="题解">题解:</h4><ol type="1"><li>首先我们要想到更改第i行的某一个灯(不改变同一行其它灯的状态)是改变这个灯上(下)的灯。</li><li>由此我们可以定下从上到下的方向来一行一行枚举灯的亮灭情况(灭就点亮)。(递推由来)</li><li>由于我们从上向下枚举,而这种枚举只改变第2-5行的灯,无法判断第一行是否有所更改会导致更少的步数,所以需要枚举第一行所有情况(32钟)。</li></ol><p>ps:因为3中的原因,为了简化枚举所有情况的代码[doge],使用了位运算的操作。</p><h4 id="代码">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">10</span>;</span><br><span class="line"><span class="keyword">char</span> a[N][N],b[N][N];</span><br><span class="line"><span class="keyword">int</span> dx[<span class="number">5</span>]={<span class="number">0</span>,<span class="number">-1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">0</span>},dy[<span class="number">5</span>]={<span class="number">0</span>,<span class="number">0</span>,<span class="number">-1</span>,<span class="number">0</span>,<span class="number">1</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">turn</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<<span class="number">5</span>;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> tx=x+dx[i],ty=y+dy[i];</span><br><span class="line"> <span class="keyword">if</span>(tx<<span class="number">0</span> && tx>=<span class="number">5</span> && ty<<span class="number">0</span> && ty>=<span class="number">5</span>)<span class="keyword">continue</span>;</span><br><span class="line"> a[tx][ty]^=<span class="number">1</span>;<span class="comment">//0->1,1->0的高级操作。 </span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;cin>>t;</span><br><span class="line"> <span class="keyword">while</span>(t--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<<span class="number">5</span>;i++)cin>>a[i];</span><br><span class="line"> <span class="built_in">memcpy</span>(b,a,<span class="keyword">sizeof</span> a);</span><br><span class="line"> <span class="keyword">int</span> res=<span class="number">10</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> op=<span class="number">0</span>;op<<span class="number">32</span>;op++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<<span class="number">5</span>;i++)<span class="keyword">if</span>(op>>i&<span class="number">1</span>)<span class="built_in">turn</span>(<span class="number">0</span>,i),sum++;<span class="comment">//如果这个数第i位是1,turn(0,i)</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<<span class="number">4</span>;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<<span class="number">5</span>;j++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i][j]==<span class="string">'0'</span>)<span class="built_in">turn</span>(i+<span class="number">1</span>,j),sum++;</span><br><span class="line"> <span class="keyword">if</span>(sum>res)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(sum>res)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> fl=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<<span class="number">5</span>;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[<span class="number">4</span>][i]==<span class="string">'0'</span>)fl=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(fl==<span class="number">1</span>)res=<span class="built_in">min</span>(res,sum);</span><br><span class="line"> <span class="built_in">memcpy</span>(a,b,<span class="keyword">sizeof</span> b);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(res<=<span class="number">6</span>)cout<<res<<endl;</span><br><span class="line"> <span class="keyword">else</span> cout<<<span class="number">-1</span><<endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
</tags>
</entry>
<entry>
<title>#767div2</title>
<link href="/2022/01/27/767div2/"/>
<url>/2022/01/27/767div2/</url>
<content type="html"><![CDATA[<h1 id="div2">#767(div2)</h1><h2 id="感受">感受</h2><p>掉大分,思路没问题代码各种手误,卡手了。</p><p>还有如果有思路不要一会换一道,一会换一道,认真思考而不是换题。</p><h2 id="a.-download-more-ram">A. Download More RAM</h2><p><a href="https://codeforces.com/contest/1629/problem/A">原题链接</a></p><h4 id="题意">题意:</h4><p>加内存,有n个加内存的软件,每个需要a<sub>i</sub>,打开后返回a<sub>i</sub>并增加b<sub>i</sub>。求最大的内存。</p><h4 id="分析">分析:</h4><p>按a从小到大排序,从头开始遍历,到第一个不满足条件的跳出,此时内存为最大值。</p><h4 id="代码">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> x first</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> y second</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">110</span>;</span><br><span class="line"><span class="keyword">typedef</span> pair<<span class="keyword">int</span>, <span class="keyword">int</span>> PII;</span><br><span class="line">PII a[N];</span><br><span class="line"><span class="keyword">int</span> n,k;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;cin>>t;</span><br><span class="line"> <span class="keyword">while</span>(t--)</span><br><span class="line"> {</span><br><span class="line"> cin>>n>>k;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)cin>>a[i].x;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)cin>>a[i].y;</span><br><span class="line"> <span class="built_in">sort</span>(a+<span class="number">1</span>,a+n+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i].x<=k)k+=a[i].y;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> cout<<k<<endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="b.-gcd-arrays">B. GCD Arrays</h2><p><a href="https://codeforces.com/contest/1629/problem/B">原题链接</a></p><h4 id="题意-1">题意:</h4><p>给l,r,k。求区间[l,r]内k个操作内其中任意两个数gcd不为1。</p><h4 id="分析-1">分析:</h4><p>因为数据极大,所以不可能求gcd,肯定是找规律。</p><p>特判+求奇数个数。</p><p>如果k大于等于奇数个数,输出YES,不然输出NO。</p><p>特判:l==r的时候,除了1是NO,以外均是YES</p><h4 id="代码-1">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> l,r,k;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;cin>>t;</span><br><span class="line"> <span class="keyword">while</span>(t--)</span><br><span class="line"> {</span><br><span class="line"> cin>>l>>r>>k;</span><br><span class="line"> <span class="keyword">if</span>(r==<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> cout<<<span class="string">"NO"</span><<endl;</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(l==r && l!=<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> cout<<<span class="string">"YES"</span><<endl;</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> num=(r-l)/<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>((r-l)%<span class="number">2</span>==<span class="number">1</span>)num++;</span><br><span class="line"> <span class="keyword">if</span>(r%<span class="number">2</span>==<span class="number">1</span> && l%<span class="number">2</span>==<span class="number">1</span>)num++;</span><br><span class="line"> <span class="keyword">if</span>(num<=k)cout<<<span class="string">"YES"</span><<endl;</span><br><span class="line"> <span class="keyword">else</span> cout<<<span class="string">"NO"</span><<endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="c.-meximum-array">C. Meximum Array</h2><p><a href="https://codeforces.com/contest/1629/problem/C">原题链接</a></p><h4 id="题意-2">题意:</h4><p>新定义MEX概念:数组内第一个没有的非负整数</p><p>e.g.<strong>MEX</strong>({1,2,31,2,3}) =0=0 and <strong>MEX</strong>({0,1,2,4,50,1,2,4,5}) =3=3.</p><p>给定一个数组a,取出前k个数求MEX放到最后,然后从剩下的继续,直到a数组为空。求b数组最大(仅按第几位比较,不比长度)的情况下的长度及每次取出k个数的MEX值。</p><h4 id="分析-2">分析:</h4><p>贪心。</p><p>因为n<2*10<sup>5</sup>,所以可以维护一个桶寻找MEX最大值num,然后根据每次找到的MEX最大值遍历a数组,直到满足。记录sum,num的值并重复,直到a数组为空。</p><h4 id="代码-2">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">200010</span>;</span><br><span class="line"><span class="keyword">int</span> a[N];</span><br><span class="line"><span class="keyword">int</span> s[N],st[N];</span><br><span class="line"><span class="keyword">int</span> b[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;cin>>t;</span><br><span class="line"> <span class="keyword">while</span>(t--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> n;cin>>n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> num;</span><br><span class="line"> cin>>num;</span><br><span class="line"> s[num]++;</span><br><span class="line"> a[i]=num;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">1</span>;k<=n;k++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> num=<span class="number">0</span>,op=<span class="number">0</span>,fl=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (s[num])num++;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=k;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> s[a[i]]--;</span><br><span class="line"> <span class="keyword">if</span>(!st[a[i]] && a[i]<=num)op++,st[a[i]]=<span class="number">1</span>;</span><br><span class="line"> <span class="comment">//当时判断写错了,写成st[i]了艹</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span>(op==num)</span><br><span class="line"> {</span><br><span class="line"> b[sum++]=num;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<=num;j++)st[j]=<span class="number">0</span>;</span><br><span class="line"> k=i;fl=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> cout<<sum<<endl;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<sum;i++)cout<<b[i]<<<span class="string">' '</span>;</span><br><span class="line"> cout<<endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="d.-peculiar-movie-preferences">D. Peculiar Movie Preferences</h2><p><a href="https://codeforces.com/contest/1629/problem/D">原题链接</a></p><h4 id="题意-3">题意:</h4><p>n个长度不超过3的字符串,使其组合成回文串。</p><p>跳过场景,而不是随意选。</p><h4 id="分析-3">分析:</h4><p>分情况讨论</p><ol type="1"><li>字符长度是1:是回文。</li><li>字符长度是2:存进map,reverse然后看是否有回文。</li><li>字符长度是3:判自己是否是回文,不是的话存进map,reverse然后看是否有回文。没有的话<ol type="1"><li>前两位放到map中并于之前长度为二的分开(map存的值不一样)。</li><li>后两位reverse后从之前字符长度为2中找是否有回文。</li></ol></li><li>以上所有只要有一个回文条件满足,那么输出YES,不然输出NO</li></ol><h4 id="代码-3">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100010</span>;</span><br><span class="line">string s;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;cin>>t;</span><br><span class="line"> <span class="keyword">while</span>(t--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> n;cin>>n;</span><br><span class="line"> map<string,<span class="keyword">int</span>> q,p;</span><br><span class="line"> <span class="keyword">int</span> fl=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> cin>>s;</span><br><span class="line"> <span class="keyword">if</span>(fl==<span class="number">1</span>)<span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">if</span>(s.<span class="built_in">size</span>()==<span class="number">1</span>)<span class="comment">//串长为1,回文</span></span><br><span class="line"> {</span><br><span class="line"> fl=<span class="number">1</span>;<span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(s.<span class="built_in">size</span>()==<span class="number">2</span>)<span class="comment">//串长为2,找之前是否有对应回文出现,并放入新串</span></span><br><span class="line"> {</span><br><span class="line"> q[s]=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">swap</span>(s[<span class="number">0</span>],s[<span class="number">1</span>]);</span><br><span class="line"> <span class="keyword">if</span>(q[s]!=<span class="number">0</span>)fl=<span class="number">1</span>;<span class="comment">//2-2</span></span><br><span class="line"> }<span class="keyword">else</span> </span><br><span class="line"> {</span><br><span class="line"> <span class="comment">//串长为3,前中两位放map里,中后两位找前面是否有对应回文</span></span><br><span class="line"> <span class="keyword">if</span>(s[<span class="number">0</span>]==s[<span class="number">2</span>])<span class="comment">//回文</span></span><br><span class="line"> {</span><br><span class="line"> fl=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//3-3 必须先判3-3,不然用3-2的判法会有问题</span></span><br><span class="line"> q[s]=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">swap</span>(s[<span class="number">0</span>],s[<span class="number">2</span>]);</span><br><span class="line"> <span class="keyword">if</span>(q[s]==<span class="number">1</span>)fl=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">swap</span>(s[<span class="number">0</span>],s[<span class="number">2</span>]);</span><br><span class="line"> string l,r;</span><br><span class="line"> <span class="comment">//l 3-2</span></span><br><span class="line"> l+=s[<span class="number">0</span>];l+=s[<span class="number">1</span>];</span><br><span class="line"> q[l]=<span class="number">2</span>;</span><br><span class="line"> <span class="comment">//r 2-3</span></span><br><span class="line"> r+=s[<span class="number">1</span>];r+=s[<span class="number">2</span>];</span><br><span class="line"> <span class="built_in">swap</span>(r[<span class="number">0</span>],r[<span class="number">1</span>]);</span><br><span class="line"> <span class="keyword">if</span>(q[r]==<span class="number">1</span>)fl=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(fl)cout<<<span class="string">"YES"</span><<endl;</span><br><span class="line"> <span class="keyword">else</span> cout<<<span class="string">"NO"</span><<endl;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">特殊数据</span></span><br><span class="line"><span class="comment">2</span></span><br><span class="line"><span class="comment">cab</span></span><br><span class="line"><span class="comment">aac</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 题解 </tag>
<tag> codeforce </tag>
</tags>
</entry>
<entry>
<title>传智杯</title>
<link href="/2021/12/19/%E4%BC%A0%E6%99%BA%E6%9D%AF/"/>
<url>/2021/12/19/%E4%BC%A0%E6%99%BA%E6%9D%AF/</url>
<content type="html"><![CDATA[<h2 id="传智杯">传智杯</h2><h3 id="感受">感受:</h3><p> 个人参加的b组,3道签到题+1道暴力/算法题+1道算法题。</p><p> 前三题卡到我的是看错题了,舍去小数部分看成整数输出,所以拿了double整数输出(四舍五入),导致wa了。</p><p> 第四题找二进制规律优化,因为不会就只写了一个暴力的解法过了3/4左右(?可能吧,一个代码TLE1-16都有过)数据。</p><h3 id="题解">题解:</h3><p>A.<a href="https://www.luogu.com.cn/problem/T216906?contestId=58542">组原成绩</a></p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191049994.png" alt="A" /><figcaption aria-hidden="true">A</figcaption></figure><h4 id="等级语法题">等级:语法题</h4><h4 id="解析">解析:</h4><p> 依据题意直接输出整数即可。</p><h4 id="代码">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">double</span> t,h,e;</span><br><span class="line"> cin>>t>>h>>e;</span><br><span class="line"> <span class="keyword">double</span> s=<span class="number">0.2</span>*t+h*<span class="number">0.3</span>+e*<span class="number">0.5</span>;</span><br><span class="line"> <span class="keyword">int</span> s1=s;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,s1);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>B.<a href="https://www.luogu.com.cn/problem/T216907?contestId=58542">报告赋分</a></p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191049450.png" alt="B" /><figcaption aria-hidden="true">B</figcaption></figure><h4 id="等级语法题-1">等级:语法题</h4><h4 id="解析-1">解析:</h4><p> 给定一个t,接下来t行每行有一个卷面基础分a和页数p。</p><p>根据页数p减卷面分(题意)就行了。</p><h4 id="代码-1">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> t;cin>>t;</span><br><span class="line"> <span class="keyword">while</span>(t--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> a,b;</span><br><span class="line"> cin>>a>>b;</span><br><span class="line"> <span class="keyword">if</span>(b<<span class="number">16</span>)a=<span class="built_in">max</span>(a<span class="number">-10</span>,<span class="number">0</span>);</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(b><span class="number">20</span>)a=<span class="built_in">max</span>(a-(b<span class="number">-20</span>),<span class="number">0</span>);</span><br><span class="line"> cout<<a<<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>C.<a href="https://www.luogu.com.cn/problem/T216908?contestId=58542">竞争得分</a></p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191049986.png" alt="C" /><figcaption aria-hidden="true">C</figcaption></figure><h4 id="等级语法题-2">等级:语法题</h4><h4 id="解析-2">解析:</h4><p> n表示人数,接下来n个数a1-an。</p><p>用数组存a1-an,用ma,mi存最大值和最小值。然后每个数进行一遍得分的运算+输出就行了。</p><h4 id="代码-2">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100010</span>;</span><br><span class="line"><span class="keyword">double</span> a[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;cin>>n;</span><br><span class="line"> <span class="keyword">double</span> ma=<span class="number">0</span>,mi=<span class="number">10000</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> cin>>a[i];</span><br><span class="line"> ma=<span class="built_in">max</span>(ma,a[i]);</span><br><span class="line"> mi=<span class="built_in">min</span>(mi,a[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> a[i]=<span class="number">100</span>*(a[i]-mi)/(ma-mi);</span><br><span class="line"> <span class="keyword">int</span> sb=a[i];<span class="comment">//直接整数输出double的话会因为小数部分四舍五入而出错</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,sb);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>D.<a href="https://www.luogu.com.cn/problem/T216909?contestId=58542">小卡与质数2</a></p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191049742.png" alt="D" /><figcaption aria-hidden="true">D</figcaption></figure><h4 id="等级算法题">等级:算法题</h4><h4 id="解析二进制规律优化">解析:二进制规律优化</h4><p> 我的解法是一种简单的暴力写法。首先筛质数板子把1e6的所有质数筛出来,然后让小于x的非负整数与其按位异或,最后利用二分查找异或后的数是否是质数,是sum++。时间复杂度O(n<em>m</em>logcnt),铁过不了但是交的代码会在#1-#16之间TLE,且每次基本不相同。</p><p> 正解后补。</p><h4 id="代码-3">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我知道我过不了但是TLE数据一直在变的代码</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1e6</span>+<span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> primes[N],cnt;</span><br><span class="line"><span class="keyword">bool</span> st[N];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!st[i])primes[cnt++]=i;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;primes[j]*i<=n;j++)</span><br><span class="line"> {</span><br><span class="line"> st[primes[j]*i]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(i%primes[j]==<span class="number">0</span>)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> cin>>n;</span><br><span class="line"> <span class="built_in">init</span>(<span class="number">1000010</span>);</span><br><span class="line"> <span class="keyword">while</span>(n--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> x;cin>>x;</span><br><span class="line"> <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<x;i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> t1=x^i;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> t=<span class="built_in">lower_bound</span>(primes,primes+cnt,t1)-primes;</span><br><span class="line"> <span class="keyword">if</span>(primes[t]==t1)sum++;</span><br><span class="line"> }</span><br><span class="line"> cout<<sum<<endl;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line">}</span><br><span class="line"><span class="comment">//周围巨佬的AC代码</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">2e6</span>+<span class="number">10</span>;</span><br><span class="line"><span class="keyword">int</span> pr[N],idx,len=<span class="number">0</span>,res=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">bool</span> st[N];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>;i<=n;i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!st[i]) pr[idx++]=i;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;pr[j]<=n/i;j++)</span><br><span class="line">{</span><br><span class="line">st[pr[j]*i]=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">if</span>(i%pr[j]==<span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> num=<span class="number">1</span>,len=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>(x)</span><br><span class="line">{</span><br><span class="line"> num=num*<span class="number">2</span>; </span><br><span class="line"> <span class="keyword">if</span>(x&<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> t=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=len;i++) t=t*<span class="number">2</span>;</span><br><span class="line"> <span class="comment">//cout<<num<<' '<<t<<' ';</span></span><br><span class="line"> <span class="keyword">int</span> r=<span class="built_in">upper_bound</span>(pr,pr+idx,num<span class="number">-1</span>)-pr;</span><br><span class="line"> <span class="keyword">int</span> l=<span class="built_in">lower_bound</span>(pr,pr+idx,t)-pr;</span><br><span class="line"> <span class="comment">//cout<<l<<' '<<r<<endl;</span></span><br><span class="line"> res+=r-l;</span><br><span class="line"> }</span><br><span class="line"> len++;</span><br><span class="line"> x>>=<span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> num;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="comment">//freopen("1.txt","r",stdin);</span></span><br><span class="line"> <span class="built_in">init</span>(N<span class="number">-10</span>);</span><br><span class="line"> <span class="keyword">int</span> T;</span><br><span class="line"> cin>>T;</span><br><span class="line"> <span class="comment">//cout<<idx<<' ';</span></span><br><span class="line"> <span class="keyword">while</span>(T--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> x;</span><br><span class="line"> cin>>x;</span><br><span class="line"> len=<span class="number">0</span>;res=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> l=<span class="number">1</span>,r;</span><br><span class="line"> r=<span class="built_in">find</span>(x);</span><br><span class="line"> cout<<res<<endl;</span><br><span class="line"> }</span><br><span class="line">} </span><br></pre></td></tr></table></figure><p>E.<a href="https://www.luogu.com.cn/problem/T216910?contestId=58542">萝卜数据库</a></p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191050511.png" alt="E" /><figcaption aria-hidden="true">E</figcaption></figure><h4 id="等级语法题-算法题">等级:语法题-算法题</h4><h4 id="解析-3">解析:</h4><p> 插入时:用二维数组第一维存字段x,第二维存值y,数组值存出现次数。</p><p> 查找时:ma[x] [ymi-yma]之和。</p><h4 id="代码-4">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//二维数组</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">1010</span>;</span><br><span class="line"><span class="keyword">int</span> a[N][N],sum[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,k;</span><br><span class="line"> cin>>n>>k;</span><br><span class="line"> <span class="keyword">int</span> b,p,x,y;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> cin>>b;</span><br><span class="line"> <span class="keyword">if</span>(b==<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> cin>>p;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=p;i++)</span><br><span class="line"> {</span><br><span class="line"> cin>>x>>y;</span><br><span class="line"> a[x][y]++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> yma,ymi;</span><br><span class="line"> cin>>x>>ymi>>yma;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=ymi;i<=yma;i++)</span><br><span class="line"> {</span><br><span class="line"> </span><br><span class="line"> sum+=a[x][i];</span><br><span class="line"> <span class="comment">//cout<<endl<<x<<i<<endl;</span></span><br><span class="line"> }</span><br><span class="line"> cout<<sum<<endl;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">//哈希写法</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">1010</span>;</span><br><span class="line"><span class="keyword">int</span> a[N][N],sum[N];</span><br><span class="line">unordered_map<<span class="keyword">int</span>,<span class="keyword">int</span>> ma[N];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> n,k;</span><br><span class="line"> cin>>n>>k;</span><br><span class="line"> <span class="keyword">int</span> b,p,x,y;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line"> cin>>b;</span><br><span class="line"> <span class="keyword">if</span>(b==<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> cin>>p;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=p;i++)</span><br><span class="line"> {</span><br><span class="line"> cin>>x>>y;</span><br><span class="line"> ma[x][y]++;</span><br><span class="line"> <span class="comment">//cout<<' '<<x<<' '<<y<<ma[x][y]<<endl;</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">int</span> yma,ymi;</span><br><span class="line"> cin>>x>>ymi>>yma;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=ymi;i<=yma;i++)</span><br><span class="line"> {</span><br><span class="line"> </span><br><span class="line"> sum+=ma[x][i];</span><br><span class="line"> <span class="comment">//cout<<endl<<x<<i<<endl;</span></span><br><span class="line"> }</span><br><span class="line"> cout<<sum<<endl;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 题解 </tag>
<tag> 传智杯 </tag>
</tags>
</entry>
<entry>
<title>校赛题解</title>
<link href="/2021/12/14/%E6%A0%A1%E8%B5%9B%E9%A2%98%E8%A7%A3/"/>
<url>/2021/12/14/%E6%A0%A1%E8%B5%9B%E9%A2%98%E8%A7%A3/</url>
<content type="html"><![CDATA[<h1 id="校赛">2021校赛:</h1><h3 id="感受">感受:</h3><p> 校赛算是选拔蓝桥杯省赛,但是这次题目给了1道签到题+两道语法题+2道算法题(可以保证基本没人写出来的那种)。所以导致只要语法有基础+手速够快就能得奖并且获得蓝桥杯参赛资格。</p><p> 然而,因为最后一题因为数据问题(过于氵),导致只要稍微有点思路就能AC,但其实正解没有人写出来,所以这次的校赛其实真的氵。</p><h3 id="题解">题解:</h3><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191050255.png" alt="A" /><figcaption aria-hidden="true">A</figcaption></figure><h4 id="等级语法题">等级:语法题</h4><h4 id="解析">解析:</h4><p> 从最后一天剩一个开始向前推,每向前一天+1然后*2,总共n-1天。</p><p> 一个for循环就行。</p><h4 id="代码">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//c</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"><span class="keyword">int</span> sum=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<n;i++)</span><br><span class="line">{</span><br><span class="line">sum=(sum+<span class="number">1</span>)*<span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d"</span>sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//c++</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> n;cin>>n;</span><br><span class="line"><span class="keyword">int</span> sum=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<n;i++)</span><br><span class="line">{</span><br><span class="line">sum=(sum+<span class="number">1</span>)*<span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line">cout<<sum;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191050582.png" alt="B" /><figcaption aria-hidden="true">B</figcaption></figure><h4 id="等级语法题-1">等级:语法题</h4><h4 id="解析-1">解析:</h4><p> 将头左斜45°,它就变成了一个金字塔形状的数列,然后这样斜着进行更新(指填数)。</p><p> 最后直接两重for控制输出即可。</p><h4 id="代码-1">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//c</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="keyword">int</span> a[<span class="number">110</span>][<span class="number">110</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"><span class="keyword">int</span> num=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=i;j++)</span><br><span class="line">{</span><br><span class="line">a[i-j+<span class="number">1</span>][j]=num++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n+<span class="number">1</span>;i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=n-i+<span class="number">1</span>;j++)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d "</span>a[i][j]);</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"><span class="comment">//c++</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">110</span>][<span class="number">110</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> n;cin>>n;</span><br><span class="line"><span class="keyword">int</span> num=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=i;j++)</span><br><span class="line">{</span><br><span class="line">a[i-j+<span class="number">1</span>][j]=num++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="comment">//cout<<num<<endl;</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n+<span class="number">1</span>;i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=n-i+<span class="number">1</span>;j++)</span><br><span class="line">{</span><br><span class="line">cout<<a[i][j]<<<span class="string">' '</span>;</span><br><span class="line">}</span><br><span class="line">cout<<endl;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191051905.png" alt="C" /><figcaption aria-hidden="true">C</figcaption></figure><h4 id="等级算法题图的dfs">等级:算法题(图的dfs)</h4><h4 id="解析-2">解析:</h4><p> 首先要会图的存储,然后每个点dfs,并每次成立的时候情况++就行了。</p><p> (新生不建议写[doge])</p><h4 id="代码-2">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//c++</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">100010</span>;</span><br><span class="line"><span class="keyword">int</span> h[N],e[N*<span class="number">2</span>],ne[N*<span class="number">2</span>],idx;</span><br><span class="line"><span class="keyword">int</span> n,m,d[N],num=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> st[N];</span><br><span class="line"><span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a,<span class="keyword">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">e[idx]=b;ne[idx]=h[a];h[a]=idx++;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs2</span><span class="params">(<span class="keyword">int</span> u)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> num++;st[u]=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">if</span>(num==<span class="number">4</span>)</span><br><span class="line">{</span><br><span class="line">sum++;</span><br><span class="line"><span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=h[u];i!=<span class="number">-1</span>;i=ne[i])</span><br><span class="line">{</span><br><span class="line"><span class="keyword">int</span> j=e[i];</span><br><span class="line"><span class="keyword">if</span>(num==<span class="number">3</span> && st[j]==<span class="number">2</span>)</span><br><span class="line">{</span><br><span class="line"> sum++;</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(!st[j])</span><br><span class="line">{</span><br><span class="line"><span class="built_in">dfs2</span>(j);</span><br><span class="line">st[j]=<span class="number">0</span>;</span><br><span class="line">num--;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">st[u]=<span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> u)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">num=<span class="number">1</span>;st[u]=<span class="number">2</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=h[u];i!=<span class="number">-1</span>;i=ne[i])</span><br><span class="line">{</span><br><span class="line"><span class="keyword">int</span> j=e[i];</span><br><span class="line"><span class="built_in">dfs2</span>(j);</span><br><span class="line">num--;</span><br><span class="line">}</span><br><span class="line">st[u]=<span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="built_in">memset</span>(h,<span class="number">-1</span>,<span class="keyword">sizeof</span> h);</span><br><span class="line">cin>>n>>m;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=m;i++)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">int</span> a,b;</span><br><span class="line">cin>>a>>b;</span><br><span class="line"><span class="built_in">add</span>(a,b);<span class="built_in">add</span>(b,a);</span><br><span class="line">d[a]++,d[b]++;</span><br><span class="line">}</span><br><span class="line"><span class="comment">// if(n<=3 || m<=2)//比赛时写的特判情况之一</span></span><br><span class="line"><span class="comment">// {</span></span><br><span class="line"><span class="comment">// cout<<0;</span></span><br><span class="line"><span class="comment">// return 0;</span></span><br><span class="line"><span class="comment">// }</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">dfs</span>(i);</span><br><span class="line">}</span><br><span class="line">cout<<sum;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191051245.png" alt="D" /><figcaption aria-hidden="true">D</figcaption></figure><h4 id="等级语法题-2">等级:语法题</h4><h4 id="解析-3">解析:</h4><p> 大水题,我上课讲的都比这难。</p><p> 时分秒表示出来然后输出就行,我上课讲的个位前补零的情况也没有。</p><p>时:a=t/3600</p><p>分:b=t%3600/60</p><p>秒:c=t%60</p><h4 id="代码-3">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//c</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> t;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>,&t);</span><br><span class="line"><span class="keyword">int</span> a=t/<span class="number">3600</span>,b=t%<span class="number">3600</span>/<span class="number">60</span>,c=t%<span class="number">60</span>;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d:%d:%d"</span>,a,b,c);</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"><span class="comment">//c++</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> t;cin>>t;</span><br><span class="line"><span class="keyword">int</span> a=t/<span class="number">3600</span>,b=t%<span class="number">3600</span>/<span class="number">60</span>,c=t%<span class="number">60</span>;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d:%d:%d"</span>,a,b,c);</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191051587.png" alt="E" /><figcaption aria-hidden="true">E</figcaption></figure><h4 id="等级语法题暴力算法题线性dp">等级:语法题(暴力)~算法题(线性DP)</h4><h4 id="解析-4">解析:</h4><p> 因为数据特别氵,所以只要思路还好的的暴力都可以过,但是暴力过了其实不代表个人实力。</p><p>然而正解是一个思路比较清奇(分组)的线性dp。</p><p> 暴力可以问你们杨琳涵学姐、唐鑫学长、赵普学长(他说直接给你们讲线性dp的解法[doge])和我。</p><p> 线性dp版可以找你们赵普学长。(我懒得讲)</p><p> 新生不建议写(暴力建议练其他题,dp算法还没学)</p><h4 id="代码-4">代码:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//暴力</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">200010</span>;</span><br><span class="line"><span class="keyword">int</span> b[N],a[N],idx;</span><br><span class="line"><span class="keyword">bool</span> st[N];</span><br><span class="line"><span class="keyword">int</span> n,k;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">cin>>n>>k;</span><br><span class="line"><span class="keyword">int</span> t;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line">{</span><br><span class="line">cin>>t;</span><br><span class="line"><span class="keyword">if</span>(!b[t])a[idx++]=t;</span><br><span class="line">b[t]++;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(k==<span class="number">0</span>)</span><br><span class="line">{</span><br><span class="line">cout<<idx;</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">sort</span>(a,a+idx<span class="number">-1</span>);</span><br><span class="line"><span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=k;i++)</span><br><span class="line">{</span><br><span class="line">sum+=b[i];</span><br><span class="line">st[i]=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=i+k;j<=<span class="number">100010</span>;j+=k)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(st[j-k]==<span class="number">1</span> && b[j-k]<b[j])</span><br><span class="line">{</span><br><span class="line">sum+=b[j]-b[j-k];</span><br><span class="line">st[j]=<span class="number">1</span>;st[j-k]=<span class="number">0</span>;</span><br><span class="line">}<span class="keyword">else</span> <span class="keyword">if</span>(st[j-k]==<span class="number">0</span>)</span><br><span class="line">{</span><br><span class="line">sum+=b[j];</span><br><span class="line">st[j]=<span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">cout<<sum;</span><br><span class="line">}</span><br><span class="line"><span class="comment">//dp</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">200010</span>;</span><br><span class="line"><span class="keyword">int</span> b[N],a[N],idx;</span><br><span class="line"><span class="keyword">int</span> f[N],st[N];</span><br><span class="line"><span class="keyword">int</span> n,k;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">cin>>n>>k;</span><br><span class="line"><span class="keyword">int</span> t;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line">{</span><br><span class="line">cin>>t;</span><br><span class="line"><span class="keyword">if</span>(!b[t])a[idx++]=t;</span><br><span class="line">b[t]++;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(k==<span class="number">0</span>)</span><br><span class="line">{</span><br><span class="line">cout<<idx;</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">sort</span>(a,a+idx<span class="number">-1</span>);</span><br><span class="line"><span class="keyword">int</span> sum=<span class="number">0</span>,j,i;</span><br><span class="line"><span class="keyword">for</span>(i=<span class="number">1</span>;i<=k;i++)</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">int</span> cnt=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=i;j<=<span class="number">100000</span>;j+=k,cnt++)</span><br><span class="line"> {</span><br><span class="line"> f[cnt]=f[cnt<span class="number">-1</span>];</span><br><span class="line"> <span class="keyword">if</span>(j>=<span class="number">2</span>)f[cnt]=<span class="built_in">max</span>(f[cnt<span class="number">-2</span>]+b[j],f[cnt]);</span><br><span class="line"> }</span><br><span class="line"> sum+=f[cnt<span class="number">-1</span>];</span><br><span class="line">}</span><br><span class="line">cout<<sum;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 题解 </tag>
</tags>
</entry>
<entry>
<title>ACgame</title>
<link href="/2021/12/13/ACgame/"/>
<url>/2021/12/13/ACgame/</url>
<content type="html"><![CDATA[<p>一个简单的小游戏</p><p><a href="https://app279.acapp.acwing.com.cn/">网址</a></p><p>https://app279.acapp.acwing.com.cn/</p>]]></content>
<tags>
<tag> AcWing </tag>
<tag> game </tag>
</tags>
</entry>
<entry>
<title>差分</title>
<link href="/2021/09/12/%E5%B7%AE%E5%88%86/"/>
<url>/2021/09/12/%E5%B7%AE%E5%88%86/</url>
<content type="html"><![CDATA[<p>差分</p><p>差分相当于前缀和的逆运算。</p><p>目的:数组中一段数+x时间复杂度降低</p><p>具体做法:假设一个数组的前缀和为原数组</p><p>一维差分:</p><p>预处理:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> a[N],b[N];</span><br><span class="line"><span class="comment">/*(1)*/</span><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)b[i]=a[i]-a[i<span class="number">-1</span>];</span><br><span class="line"><span class="comment">/*(2)*/</span><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line">{</span><br><span class="line">b[i]+=a[i];</span><br><span class="line"> b[i+<span class="number">1</span>]-=a[i];</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>添加数:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> l,r,c;</span><br><span class="line"><span class="keyword">while</span>(m--)</span><br><span class="line">{</span><br><span class="line">cin>>l>>r>>c;</span><br><span class="line"> b[l]+=c;</span><br><span class="line"> b[r+<span class="number">1</span>]-=c;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>二维差分:</p><p>差分函数:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> x1,<span class="keyword">int</span> y1,<span class="keyword">int</span> x2,<span class="keyword">int</span> y2,<span class="keyword">int</span> c)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> a[x1][y1]+=c;</span><br><span class="line"> a[x2+<span class="number">1</span>][y1]-=c;</span><br><span class="line"> a[x1][y2+<span class="number">1</span>]-=c;</span><br><span class="line"> a[x2+<span class="number">1</span>][y2+<span class="number">1</span>]+=c;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>预处理:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++)<span class="built_in">insert</span>(i,j,i,j,c);</span><br></pre></td></tr></table></figure><p>添加数:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">insert</span>(x1,y1,x2,y2,c);</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
</tags>
</entry>
<entry>
<title>前缀和</title>
<link href="/2021/09/12/%E5%89%8D%E7%BC%80%E5%92%8C/"/>
<url>/2021/09/12/%E5%89%8D%E7%BC%80%E5%92%8C/</url>
<content type="html"><![CDATA[<p>前缀和</p><p>目的:求原数组中某段数的和(O(1))</p><p>具体做法:做一个预处理,定义一个新数组储存原数组前i个数的和</p><p>一维前缀和:</p><p>预处理:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> sum[N],a[N];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)sum[i]=sum[i<span class="number">-1</span>]+a[i];</span><br></pre></td></tr></table></figure><p>查找:(求l~r之间的数)</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">num=sum[r]-sum[l<span class="number">-1</span>];</span><br></pre></td></tr></table></figure><p>二维前缀和:(当成方格形)</p><p>预处理:</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> sum[N][M],a[N][M];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=m;j++)</span><br><span class="line"> sum[i][j]=sum[i<span class="number">-1</span>][j<span class="number">-1</span>]-sum[i<span class="number">-1</span>][j]-sum[i][j<span class="number">-1</span>]+a[i][j];</span><br></pre></td></tr></table></figure><p>查找:(求(l1,r1)~(l2,r2)之间的数)</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">num=sum[l2][r2]-sum[l2][r1<span class="number">-1</span>]-sum[l1<span class="number">-1</span>][r2]+sum[l1][r1];</span><br></pre></td></tr></table></figure><p>高维前缀和:(类比)</p>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
</tags>
</entry>
<entry>
<title>STL</title>
<link href="/2021/09/11/STL/"/>
<url>/2021/09/11/STL/</url>
<content type="html"><![CDATA[<h3 id="stl简记">STL简记</h3>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
</tags>
</entry>
<entry>
<title>hash</title>
<link href="/2021/09/11/hash/"/>
<url>/2021/09/11/hash/</url>
<content type="html"><![CDATA[<h2 id="哈希表">哈希表</h2><p>目的:将大范围的多个数映射到一个0-N的集合中</p><h3 id="常见哈希表"><strong>常见哈希表:</strong></h3><h4 id="普通哈希表">1.普通哈希表</h4><h5 id="处理冲突方式"><strong>处理冲突方式:</strong></h5><p> 1.开放寻址法</p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191052335.png" alt="image-20210911164017945" /><figcaption aria-hidden="true">image-20210911164017945</figcaption></figure><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">2e5</span>+<span class="number">3</span>,num=<span class="number">0x3f3f3f3f</span>;</span><br><span class="line"><span class="keyword">int</span> h[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> k=(x%N+N)%N;</span><br><span class="line"> <span class="keyword">while</span>(h[k]!=num&&h[k]!=x)</span><br><span class="line"> {</span><br><span class="line">k++;</span><br><span class="line"> <span class="keyword">if</span>(k==N)k=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">//添加</span></span><br><span class="line"><span class="keyword">int</span> k=<span class="built_in">find</span>(x);</span><br><span class="line">h[k]=x;</span><br><span class="line"><span class="comment">//查找</span></span><br><span class="line"><span class="keyword">int</span> k=<span class="built_in">find</span>(x);</span><br><span class="line"><span class="keyword">if</span>(h[k]!=num)<span class="comment">//存在</span></span><br><span class="line"> <span class="keyword">else</span> <span class="comment">//不存在</span></span><br></pre></td></tr></table></figure><p> 2.拉链法:一个数组存储所有哈希值</p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191052097.png" alt="image-20210911155856034" /><figcaption aria-hidden="true">image-20210911155856034</figcaption></figure><p>常用操作:1.添加2.查找</p><p>3.删除(额外数组进行标记,不常用)</p><p>代码</p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">1e5</span>+<span class="number">3</span>;<span class="comment">//减少冲突经常取模100003(大于最大集合范围的最小的质数)</span></span><br><span class="line"><span class="keyword">int</span> h[N],e[N],ne[N],idx;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> x)</span><span class="comment">//添加</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">int</span> k=(x%N+N)%N;</span><br><span class="line"> e[idx]=x;</span><br><span class="line"> ne[idx]=h[k];</span><br><span class="line"> h[k]=idx++;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span><span class="comment">//查找</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> k=(x%N+N)%N;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=h[k];i!=<span class="number">-1</span>;i=ne[i])</span><br><span class="line"> <span class="keyword">if</span>(e[i]==x)<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="字符串哈希">2.字符串哈希</h4><h5 id="字符串前缀哈希法">字符串前缀哈希法:</h5><p>预处理所有前缀的哈希</p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191052729.png" alt="1" /><figcaption aria-hidden="true">1</figcaption></figure><p> 1.如何定义前缀的哈希值:将其转化为p进制数</p><p> 注:*1.不能将任何数映射为0</p><p> *2.不存在冲突时,p=131或13331、Q取2^64时,不存在冲突</p><figure><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191052345.png" alt="image-2" /><figcaption aria-hidden="true">image-2</figcaption></figure><p>好处:可以依照公式求出前缀的哈希值求出一段的哈希值</p><p><img src="https://sealphotobed.oss-cn-hangzhou.aliyuncs.com/seal/202303191053444.png" alt="image-20210911171102237" />公式: <span class="math display">\[h[R]-h[L-1]*p^(R-L+1)\]</span></p><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><bits/stdc++.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">long</span> ull;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">1e5</span>+<span class="number">10</span>,P=<span class="number">131</span>;</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">char</span> str[N];</span><br><span class="line">ull h[N],p[N];</span><br><span class="line"><span class="function">ull <span class="title">get</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">return</span> h[r]-h[l<span class="number">-1</span>]*p[r-l+<span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%s"</span>,&n,&m,str+<span class="number">1</span>);</span><br><span class="line"> p[<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<=n;i++)</span><br><span class="line"> {</span><br><span class="line">p[i]=p[i<span class="number">-1</span>]*P;</span><br><span class="line"> h[i]=h[i<span class="number">-1</span>]*P+str[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(m--)</span><br><span class="line"> {</span><br><span class="line"><span class="keyword">int</span> l1,l2,r1,r2;</span><br><span class="line"> cin>>l1>>r1>>l2>>r2;</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">get</span>(l1,r1)==<span class="built_in">get</span>(l2,r2))cout<<<span class="string">"Yes"</span><<endl;</span><br><span class="line"> <span class="keyword">else</span> cout<<<span class="string">"No"</span><<endl;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> AcWing </tag>
</tags>
</entry>
<entry>
<title>Introduction</title>
<link href="/2021/09/10/Introduction/"/>
<url>/2021/09/10/Introduction/</url>
<content type="html"><![CDATA[<p> 学着搭了个简单的博客,用来简单记录自己的算法学习和游戏相关。</p>]]></content>
</entry>
</search>