-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
815 lines (476 loc) · 601 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Java入门反思</title>
<link href="/2020/02/23/%E6%96%B0%E7%94%9F/"/>
<url>/2020/02/23/%E6%96%B0%E7%94%9F/</url>
<content type="html"><![CDATA[<p>这几天被疫情困在家里</p><p>于是乎看了一下Java</p><p>感觉Java最不同于我之前学OI的时候的学的编程的地方就是面向对象的这种方式</p><p>也需要花更多的时间来想怎么能提高代码的可维护性,比如避免多个类用到的变量冲突、文件目录等等</p><p>但纸上得来终觉浅,还是要多实际操作一下,不然总是会觉得某些实际上挺有用的东西十分冗余,同时多看一些优秀的开源项目,学习他们优秀的地方</p>]]></content>
<tags>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>【JLOI2011】飞行路线</title>
<link href="/2018/11/08/%5BJLOI2011%5D%E9%A3%9E%E8%A1%8C%E8%B7%AF%E7%BA%BF/"/>
<url>/2018/11/08/%5BJLOI2011%5D%E9%A3%9E%E8%A1%8C%E8%B7%AF%E7%BA%BF/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P4568" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>dp+dijkstra(大概吧,但是感觉就只是堆优化spfa,但是都说这也是dijkstra,而且想了一下这样好像还能处理负权图,但是不知道时间复杂度是不是能优化?)</p><p>dis[i]/[j]表示到i点用了j次免费机票的最短距离</p><p>然后堆的元素是这样的</p><figure class="highlight cpp"><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="class"><span class="keyword">struct</span> <span class="title">Node</span>{</span></span><br><span class="line"> <span class="keyword">int</span> num,k;</span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> Node&b)<span class="keyword">const</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">return</span> dis[num][k]>dis[b.num][b.k];</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure><p>num是点的序号,k是用了多少次免费机票</p><p>其实好像可以拆成多个点。。。一样的。。。反正最后跑下来挺快的。。。其实还是一道比较简单的省选题。。。luogu评分真的是不敢恭维啊。。。</p><p>大概是最后一次这样写题解了吧,oi生涯还真是短暂啊</p><p>回忆录就等考完再写吧(虽然我这么菜也没什么好回忆的</p><h2>Code</h2><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 10010</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXM 100010</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[MAXM];</span><br><span class="line"><span class="keyword">int</span> last[MAXN],dis[MAXN][<span class="number">11</span>],cnt,n,m,k,s,t;</span><br><span class="line"><span class="keyword">bool</span> inq[MAXN][<span class="number">11</span>];</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span>{</span></span><br><span class="line"> <span class="keyword">int</span> num,k;</span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> Node&b)<span class="keyword">const</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">return</span> dis[num][k]>dis[b.num][b.k];</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">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dijkstra</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> Node u;</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> ans=<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">int</span> tk,temp;</span><br><span class="line"> <span class="keyword">bool</span> tag;</span><br><span class="line"> priority_queue<Node> q;</span><br><span class="line"> rep(i,<span class="number">0</span>,n)</span><br><span class="line"> rep(j,<span class="number">0</span>,k)</span><br><span class="line"> dis[i][j]=<span class="number">-1</span>;</span><br><span class="line"> dis[s][<span class="number">0</span>]=<span class="number">0</span>;</span><br><span class="line"> q.push((Node){s,<span class="number">0</span>});</span><br><span class="line"> inq[s][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> u=q.top();q.pop();</span><br><span class="line"> temp=last[u.num];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[u.num][u.k]+e[temp].v<dis[e[temp].to][u.k]||dis[e[temp].to][u.k]<<span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to][u.k]=dis[u.num][u.k]+e[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to][u.k])</span><br><span class="line"> {</span><br><span class="line"> q.push((Node){e[temp].to,u.k});</span><br><span class="line"> inq[e[temp].to][u.k]=<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>(u.k+<span class="number">1</span><=k&&(dis[u.num][u.k]<dis[e[temp].to][u.k+<span class="number">1</span>]||dis[e[temp].to][u.k+<span class="number">1</span>]<<span class="number">0</span>))</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to][u.k+<span class="number">1</span>]=dis[u.num][u.k];</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to][u.k+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> q.push((Node){e[temp].to,u.k+<span class="number">1</span>});</span><br><span class="line"> inq[e[temp].to][u.k+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> inq[u.num][u.k]=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">0</span>,k)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[t][i]<<span class="number">0</span>)</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">if</span>(dis[t][i]<ans||ans<<span class="number">0</span>)</span><br><span class="line"> ans=dis[t][i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,ans);</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> tx,ty,tz;</span><br><span class="line"> n=read();m=read();k=read();</span><br><span class="line"> s=read();t=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);add(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> dijkstra();</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>
<tags>
<tag> 动态规划 </tag>
<tag> 最短路 </tag>
</tags>
</entry>
<entry>
<title>纪念品分组</title>
<link href="/2018/11/08/%E7%BA%AA%E5%BF%B5%E5%93%81%E5%88%86%E7%BB%84/"/>
<url>/2018/11/08/%E7%BA%AA%E5%BF%B5%E5%93%81%E5%88%86%E7%BB%84/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1094" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>尽量把大的和小的放一起,先放大的后放小的,两个指针向中间移动</p><h2>Code</h2><figure class="highlight cpp"><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"><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stack></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> w,n,p[<span class="number">30010</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> l,r,temp,ans=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>,&w,&n);</span><br><span class="line"> l=<span class="number">1</span>,r=n;</span><br><span class="line"> rep(i,<span class="number">1</span>,n)<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&p[i]);</span><br><span class="line"> sort(p+<span class="number">1</span>,p+n+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">while</span>(l<=r)</span><br><span class="line"> {</span><br><span class="line"> ++ans;</span><br><span class="line"> temp=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(r>=l&&temp+p[r]<=w)</span><br><span class="line"> temp+=p[r--];</span><br><span class="line"> <span class="keyword">while</span>(l<=r&&temp+p[l]<=w)</span><br><span class="line"> temp+=p[l++];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<ans;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 贪心 </tag>
</tags>
</entry>
<entry>
<title>转圈游戏</title>
<link href="/2018/11/08/%E8%BD%AC%E5%9C%88%E6%B8%B8%E6%88%8F/"/>
<url>/2018/11/08/%E8%BD%AC%E5%9C%88%E6%B8%B8%E6%88%8F/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1965" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>直接快速幂+取模。。。没什么好说的</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stack></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> n,m,k,x;</span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">f_pow</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x,<span class="keyword">long</span> <span class="keyword">long</span> k)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> ans=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(k)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(k&<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> ans*=x;</span><br><span class="line"> ans%=n;</span><br><span class="line"> }</span><br><span class="line"> x*=x;</span><br><span class="line"> x%=n;</span><br><span class="line"> k>>=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</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%d%d"</span>,&n,&m,&k,&x);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,(x+<span class="number">1L</span>L*f_pow(<span class="number">10</span>,k)*m)%n);</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>
<tags>
<tag> 快速幂 </tag>
</tags>
</entry>
<entry>
<title>【USACO08JAN】电话线Telephone Lines</title>
<link href="/2018/11/08/%5BUSACO08JAN%5D%E7%94%B5%E8%AF%9D%E7%BA%BFTelephone%20Lines/"/>
<url>/2018/11/08/%5BUSACO08JAN%5D%E7%94%B5%E8%AF%9D%E7%BA%BFTelephone%20Lines/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1948" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>一开始看错题了。。。消费为路径上的最大值。。。所以没有想二分答案。。。</p><h3>1.</h3><p>免费连k条相当于把k条权值变成0</p><p>显然每个点的dis可以这样记录:</p><p>dis[i]/[j]表示到点i消掉了j条边</p><p>然后跑最短路,dis按题目要求来就好了</p><p>应该是动态规划吧。。。</p><p>其实我这个松弛还可以优化一下,具体可以看飞行路线那题,每个点没必要每次把所有的0~k都松弛一遍,只需要松弛被松弛的就行了</p><h3>2.</h3><p>二分答案+spfa</p><p>因为消耗是路径上的边权最大值,所以是可以二分答案的</p><p>judge的时候转换一下,把所有边权大于maxx的边权值设为1,不大于maxx的边权值设为0</p><p>然后跑一边spfa,看dis[n]是不是小于等于k即可</p><p>一开始的左边界设错了。。。二分的左边界应该一直是0,我设置为了最小边权,然后就在bzoj上一直wa,但是luogu上可a。。。数据水?</p><h2>Code</h2><h2>Code</h2><h3>1.</h3><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[<span class="number">20001</span>];</span><br><span class="line"><span class="built_in">queue</span><<span class="keyword">int</span>> q;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> dis[<span class="number">1001</span>][<span class="number">10001</span>],ans=<span class="number">-1</span>;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">1001</span>],n,p,k,cnt;</span><br><span class="line"><span class="keyword">bool</span> inq[<span class="number">1001</span>];</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">long</span> <span class="keyword">long</span> <span class="title">max</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &a,<span class="keyword">const</span> <span class="keyword">int</span> &b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> a>b?a:b;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</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> tx,ty,tz,temp,u;</span><br><span class="line"> <span class="keyword">bool</span> tag;</span><br><span class="line"> n=read();p=read();k=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,p)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> add(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,<span class="number">0</span>,k)</span><br><span class="line"> dis[i][j]=<span class="number">-1</span>;</span><br><span class="line"> dis[<span class="number">1</span>][<span class="number">0</span>]=<span class="number">0</span>;</span><br><span class="line"> inq[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> q.push(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())<span class="comment">//这里可以优化,但是空间消耗会大一点,具体可见飞行路线</span></span><br><span class="line"> {</span><br><span class="line"> u=q.front();q.pop();</span><br><span class="line"> rep(i,<span class="number">0</span>,k)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[u][i]<<span class="number">0</span>)</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> temp=last[u];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> tag=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(dis[e[temp].to][i]<<span class="number">0</span>||max(dis[u][i],e[temp].v)<dis[e[temp].to][i])</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to][i]=max(dis[u][i],e[temp].v);</span><br><span class="line"> tag=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(i+<span class="number">1</span><=k&&(dis[e[temp].to][i+<span class="number">1</span>]<<span class="number">0</span>||dis[u][i]<dis[e[temp].to][i+<span class="number">1</span>]))</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to][i+<span class="number">1</span>]=dis[u][i];</span><br><span class="line"> tag=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(tag&&!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> inq[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> q.push(e[temp].to);</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> inq[u]=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">0</span>,k)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[n][i]<<span class="number">0</span>)</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">if</span>(ans<<span class="number">0</span>)</span><br><span class="line"> ans=dis[n][i];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> ans=min(ans,dis[n][i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,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><h3>2.</h3><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v,v_sb;</span><br><span class="line">}e[<span class="number">20001</span>];</span><br><span class="line"><span class="keyword">int</span> n,p,k,last[<span class="number">1001</span>],cnt,dis[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">bool</span> inq[<span class="number">1001</span>];</span><br><span class="line"><span class="built_in">queue</span><<span class="keyword">int</span>> q;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v_sb=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="keyword">int</span> maxx)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> temp,u;</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> dis[i]=<span class="number">-1</span>;</span><br><span class="line"> rep(i,<span class="number">1</span>,cnt)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(e[i].v_sb>maxx)</span><br><span class="line"> e[i].v=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> e[i].v=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> dis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> inq[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> q.push(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> u=q.front();q.pop();</span><br><span class="line"> <span class="comment">/*if(dis[u]>k)</span></span><br><span class="line"><span class="comment"> {</span></span><br><span class="line"><span class="comment"> inq[u]=0;</span></span><br><span class="line"><span class="comment"> continue;</span></span><br><span class="line"><span class="comment"> }*/</span><span class="comment">//sb的负优化,耗时还多了4ms</span></span><br><span class="line"> temp=last[u];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[e[temp].to]<<span class="number">0</span>||dis[u]+e[temp].v<dis[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to]=dis[u]+e[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> inq[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> q.push(e[temp].to);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> inq[u]=<span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(dis[n]>=<span class="number">0</span>&&dis[n]<=k)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</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="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> tx,ty,tz,l=<span class="number">0</span>,r=<span class="number">1</span>,mid,ans;</span><br><span class="line"> n=read();p=read();k=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,p)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> r=max(r,tz);</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> add(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(!judge(r))</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"-1"</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">while</span>(l<=r)</span><br><span class="line"> {</span><br><span class="line"> mid=l+((r-l)>><span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(judge(mid))</span><br><span class="line"> {</span><br><span class="line"> ans=mid;</span><br><span class="line"> r=mid<span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> l=mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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>]]></content>
<tags>
<tag> 动态规划 </tag>
<tag> 二分答案 </tag>
<tag> 最短路 </tag>
</tags>
</entry>
<entry>
<title>银河英雄传说</title>
<link href="/2018/11/08/%E9%93%B6%E6%B2%B3%E8%8B%B1%E9%9B%84%E4%BC%A0%E8%AF%B4/"/>
<url>/2018/11/08/%E9%93%B6%E6%B2%B3%E8%8B%B1%E9%9B%84%E4%BC%A0%E8%AF%B4/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1196" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>跟叠积木差不多,就是询问的时候输出两个权值相减而已</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 30010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> a[MAXN],b[MAXN],v[MAXN];<span class="comment">//top , bottom , value</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">finda</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">if</span>(!a[x])</span><br><span class="line"> <span class="keyword">return</span> x;</span><br><span class="line"> finda(a[x]);</span><br><span class="line"> v[x]+=v[a[x]];</span><br><span class="line"> <span class="keyword">return</span> a[x]=finda(a[x]);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">findb</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">return</span> b[x]?b[x]=findb(b[x]):x;</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">char</span> c;</span><br><span class="line"> <span class="keyword">int</span> tx,ty;</span><br><span class="line"> <span class="keyword">int</span> T=read();</span><br><span class="line"> <span class="keyword">while</span>(T--)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> tx=read();ty=read();</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'M'</span>)<span class="comment">//merge</span></span><br><span class="line"> {</span><br><span class="line"> tx=finda(tx);ty=findb(ty);</span><br><span class="line"> v[tx]=<span class="number">1</span>;</span><br><span class="line"> a[tx]=ty;</span><br><span class="line"> b[ty]=findb(tx);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'C'</span>)<span class="comment">//query</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(finda(tx)!=finda(ty))</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"-1\n"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,<span class="built_in">abs</span>(v[tx]-v[ty])<span class="number">-1</span>);</span><br><span class="line"> }</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>
<tags>
<tag> 并查集 </tag>
<tag> 带权并查集 </tag>
</tags>
</entry>
<entry>
<title>路径统计</title>
<link href="/2018/11/07/%E8%B7%AF%E5%BE%84%E7%BB%9F%E8%AE%A1/"/>
<url>/2018/11/07/%E8%B7%AF%E5%BE%84%E7%BB%9F%E8%AE%A1/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1608" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>有点类似最短路计数那道题。。。</p><p>主要问题有两个:去重边,统计最短路条数</p><p>前一个比较好搞,后一个在spfa的时候改一下松弛操作就行了</p><p>但是要在每一个元素出队时将其计数器清零,否则会被以下数据×掉</p><figure class="highlight plain"><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></pre></td><td class="code"><pre><span class="line">5 5</span><br><span class="line">1 2 1</span><br><span class="line">2 3 1</span><br><span class="line">3 4 1</span><br><span class="line">4 5 1</span><br><span class="line">1 4 3</span><br></pre></td></tr></table></figure><p>因为这组数据如果不清零4会入队两次, 第一次是1->4 (v=3,dis=3,path=1)</p><p>然后4->5 (v=1,dis=4,path=1)</p><p>第二次是3->4(v=1,dis=3,path=1+1)</p><p>然后4->5(v=1,dis=4,path=1+2)</p><p>然后就会输出</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">4 3</span><br></pre></td></tr></table></figure><p>实际是</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">4 2</span><br></pre></td></tr></table></figure><h2>Code</h2><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[<span class="number">4000001</span>];</span><br><span class="line"><span class="built_in">queue</span><<span class="keyword">int</span>> q;</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">2001</span>],cnt,dis[<span class="number">2001</span>],ee[<span class="number">2001</span>][<span class="number">2001</span>],cnt_path[<span class="number">2001</span>];</span><br><span class="line"><span class="keyword">bool</span> inq[<span class="number">2001</span>];</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v,<span class="keyword">int</span> &num)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(!ee[from][to])</span><br><span class="line"> {</span><br><span class="line"> ee[from][to]=v;</span><br><span class="line"> e[num].next=last[from];</span><br><span class="line"> last[from]=num;</span><br><span class="line"> e[num].to=to;</span><br><span class="line"> e[num].v=v;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(v<ee[from][to])</span><br><span class="line"> {</span><br><span class="line"> ee[from][to]=v;</span><br><span class="line"> e[num].next=last[from];</span><br><span class="line"> last[from]=num;</span><br><span class="line"> e[num].to=to;</span><br><span class="line"> e[num].v=v;</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"> --num;</span><br><span class="line"> --m;</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> tx,ty,tz,temp;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)dis[i]=<span class="number">-1</span>;</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz,i);</span><br><span class="line"> }</span><br><span class="line"> cnt_path[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> dis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> q.push(<span class="number">1</span>);</span><br><span class="line"> inq[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> temp=last[q.front()];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[e[temp].to]<<span class="number">0</span>||dis[q.front()]+e[temp].v<dis[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to]=dis[q.front()]+e[temp].v;</span><br><span class="line"> cnt_path[e[temp].to]=cnt_path[q.front()];</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> q.push(e[temp].to);</span><br><span class="line"> inq[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(dis[q.front()]+e[temp].v==dis[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> cnt_path[e[temp].to]+=cnt_path[q.front()];</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> q.push(e[temp].to);</span><br><span class="line"> inq[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(q.front()!=n)</span><br><span class="line"> cnt_path[q.front()]=<span class="number">0</span>;</span><br><span class="line"> inq[q.front()]=<span class="number">0</span>;</span><br><span class="line"> q.pop();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(dis[n]<=<span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"No answer"</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="built_in">printf</span>(<span class="string">"%d %d"</span>,dis[n],cnt_path[n]);</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>
<tags>
<tag> 最短路 </tag>
</tags>
</entry>
<entry>
<title>TJOI2007路标设置</title>
<link href="/2018/11/07/TJOI2007%E8%B7%AF%E6%A0%87%E8%AE%BE%E7%BD%AE/"/>
<url>/2018/11/07/TJOI2007%E8%B7%AF%E6%A0%87%E8%AE%BE%E7%BD%AE/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P3853" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>一道比较简单的二分答案题。。。</p><p>好像和跳石头区别不大。。。直接上代码就好了。。。</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> l,n,k,pos[<span class="number">100001</span>],t_pos[<span class="number">100001</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="keyword">int</span> maxx)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="built_in">memcpy</span>(t_pos,pos,<span class="keyword">sizeof</span> pos);</span><br><span class="line"> <span class="keyword">int</span> temp=<span class="number">0</span>;</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">while</span>(t_pos[i]-t_pos[i<span class="number">-1</span>]>maxx)</span><br><span class="line"> {</span><br><span class="line"> ++temp;</span><br><span class="line"> t_pos[i<span class="number">-1</span>]+=maxx;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(temp>k)</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">return</span> <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"> l=read();n=read();k=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> pos[i]=read();</span><br><span class="line"> <span class="keyword">int</span> ll=<span class="number">1</span>,rr=l,mid,ans;</span><br><span class="line"> <span class="keyword">while</span>(ll<=rr)</span><br><span class="line"> {</span><br><span class="line"> mid=ll+((rr-ll)>><span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(judge(mid))</span><br><span class="line"> {</span><br><span class="line"> ans=mid;</span><br><span class="line"> rr=mid<span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> ll=mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<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>]]></content>
<tags>
<tag> 二分答案 </tag>
</tags>
</entry>
<entry>
<title>叠积木</title>
<link href="/2018/11/07/%E5%8F%A0%E7%A7%AF%E6%9C%A8/"/>
<url>/2018/11/07/%E5%8F%A0%E7%A7%AF%E6%9C%A8/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P2342" target="_blank" rel="noopener">题目链接</a></p><p>一道不错的带权并查集入门题</p><a id="more"></a><h2>sol</h2><p>用两个并查集来做</p><p>a[]以积木底部的积木为根,带权,用于统计操作</p><p>b[]以积木顶部党的积木为根,不带权,用于移动操作</p><p>移动的时候,a并查集把x底部的接到y顶部的,也就是把a[x]的根接到b[y]的根上,b并查集把y顶部的接到x底部的,也就是把b[y]的根接到a[x]上</p><p>权值相应的搞一搞就行了,好像这个并不是重点?</p><p>但是权值在询问的时候应该要先finda()一下把它路径压缩一下,否则我认为可能它并没有直接连到根上,从而使得其权值并非到根的权值而只是一个到其父节点(非根)的权值</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 30001</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> a[MAXN],b[MAXN],v[MAXN];<span class="comment">//bottom , top , value</span></span><br><span class="line"><span class="keyword">int</span> ans;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">finda</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">if</span>(a[x])</span><br><span class="line"> {</span><br><span class="line"> a[x]=finda(a[x]);</span><br><span class="line"> ans+=v[x];</span><br><span class="line"> v[x]=ans;</span><br><span class="line"> <span class="keyword">return</span> a[x];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> x;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">findb</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">return</span> b[x]?b[x]=findb(b[x]):x;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">merge</span><span class="params">(<span class="keyword">int</span> from,<span class="keyword">int</span> to)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> to=findb(to);</span><br><span class="line"> from=finda(from);</span><br><span class="line"> v[from]=<span class="number">1</span>;</span><br><span class="line"> a[from]=to;</span><br><span class="line"> b[to]=from;</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">char</span> c;</span><br><span class="line"> <span class="keyword">int</span> p,tx,ty;</span><br><span class="line"> p=read();</span><br><span class="line"> rep(k,<span class="number">1</span>,p)</span><br><span class="line"> {</span><br><span class="line"> ans=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'C'</span>)<span class="comment">//query</span></span><br><span class="line"> {</span><br><span class="line"> tx=read();</span><br><span class="line"> finda(tx);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,ans);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span><span class="comment">//move</span></span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();</span><br><span class="line"> merge(tx,ty);</span><br><span class="line"> }</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>
<tags>
<tag> 并查集 </tag>
<tag> 带权并查集 </tag>
</tags>
</entry>
<entry>
<title>关押罪犯</title>
<link href="/2018/11/06/%E5%85%B3%E6%8A%BC%E7%BD%AA%E7%8A%AF/"/>
<url>/2018/11/06/%E5%85%B3%E6%8A%BC%E7%BD%AA%E7%8A%AF/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1525" target="_blank" rel="noopener">题目链接</a></p><p>一道不错的题</p><a id="more"></a><h2>sol</h2><h3>1.二分答案+二分图判定</h3><p>我们要让冲突事件影响力最大值最小,然后就容易想到二分答案</p><p>但是怎么判定?</p><p>其实两个监狱类似于两个集合,每个犯人类似于一个点,冲突关系类似于边</p><p>如果冲突最大值为maxx,就意味着对于权值大于maxx的边及其两端的点构成的子图构成了一个二分图</p><p>所以判定只是判定是否为二分图。。。直接染色判定就好了</p><p>复杂度大概是O((n+m) log max{c_i})的?</p><h3>2.并查集(虚点并查集或带权并查集)</h3><p>只会虚点并查集的做法。。。不过好像带权并查集的题有一部分可以用虚点并查集来做?还是说所有?并不怎么会带权并查集啊。。。</p><p>好像NOI食物链那道题也可以用虚点并查集来做?</p><p>用i+n表示与i敌对的点的集合,类似于一个补集</p><p>然后用类似kruskal最大生成树的方式,先处理掉影响力大的冲突关系,最后出现无法解决的矛盾时直接跳出,否则merge(u+n,v) , merge(u,v+n)</p><p>但是不知道为什么带上按秩合并一直MLE,不带按秩合并就A了。。。玄学吗?</p><p>说起来,代码长度和复杂度上好像都是并查集要更加优秀。。。但是思维难度上貌似是二分答案+二分图判定更加简单?我自己应该是想不出来虚点并查集这种操作。。。</p><h2>Code</h2><h3>1.</h3><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstdlib></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> from,to,next,v;</span><br><span class="line">}e[<span class="number">200001</span>];</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">20001</span>],col[<span class="number">20001</span>],<span class="keyword">ans_t</span>;</span><br><span class="line"><span class="keyword">int</span> vv[<span class="number">100001</span>];</span><br><span class="line"><span class="keyword">int</span> b[<span class="number">3</span>]={<span class="number">0</span>,<span class="number">2</span>,<span class="number">1</span>};</span><br><span class="line"><span class="keyword">int</span> n,m,cnt;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> from,<span class="keyword">int</span> to,<span class="keyword">int</span> v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> <span class="keyword">col_t</span>)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> temp;</span><br><span class="line"> col[x]=<span class="keyword">col_t</span>;</span><br><span class="line"> temp=last[x];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(e[temp].v<=<span class="keyword">ans_t</span>)</span><br><span class="line"> {</span><br><span class="line"> temp=e[temp].next;</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>(!col[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!dfs(e[temp].to,b[<span class="keyword">col_t</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">else</span> <span class="keyword">if</span>(b[<span class="keyword">col_t</span>]!=col[e[temp].to])</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</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>(col,<span class="number">0</span>,<span class="keyword">sizeof</span> col);</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="keyword">if</span>(!col[i])</span><br><span class="line"> <span class="keyword">if</span>(!dfs(i,<span class="number">1</span>))</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> <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="keyword">int</span> l,r,mid,ans;</span><br><span class="line"> <span class="keyword">int</span> tx,ty,tz;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> l=<span class="number">0</span>;r=m;</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> add(ty,tx,tz);</span><br><span class="line"> vv[i]=tz;</span><br><span class="line"> }</span><br><span class="line"> sort(vv+<span class="number">1</span>,vv+m+<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">while</span>(l<=r)</span><br><span class="line"> {</span><br><span class="line"> mid=((l+r)>><span class="number">1</span>);</span><br><span class="line"> <span class="keyword">ans_t</span>=vv[mid];</span><br><span class="line"> <span class="keyword">if</span>(judge())</span><br><span class="line"> {</span><br><span class="line"> r=mid<span class="number">-1</span>;</span><br><span class="line"> ans=vv[mid];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> l=mid+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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><h3>2.</h3><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> from,to,v;</span><br><span class="line">}e[<span class="number">100001</span>];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">int</span> fa[<span class="number">40001</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(Edge a,Edge b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> a.v>b.v;</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">return</span> fa[x]==x?x:fa[x]=find(fa[x]);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">merge</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"> x=find(x);y=find(y);</span><br><span class="line"> fa[x]=y;</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> ans=<span class="number">0</span>;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n+n)fa[i]=i;</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> e[i].from=read();e[i].to=read();e[i].v=read();</span><br><span class="line"> }</span><br><span class="line"> sort(e+<span class="number">1</span>,e+m+<span class="number">1</span>,cmp);</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(find(e[i].from)==find(e[i].to))</span><br><span class="line"> {</span><br><span class="line"> ans=e[i].v;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> merge(e[i].from+n,e[i].to);</span><br><span class="line"> merge(e[i].from,e[i].to+n);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 二分答案 </tag>
<tag> 并查集 </tag>
<tag> 二分图 </tag>
<tag> 虚点并查集 </tag>
<tag> 带权并查集 </tag>
</tags>
</entry>
<entry>
<title>HAOI2006受欢迎的牛</title>
<link href="/2018/11/06/HAOI2006%E5%8F%97%E6%AC%A2%E8%BF%8E%E7%9A%84%E7%89%9B/"/>
<url>/2018/11/06/HAOI2006%E5%8F%97%E6%AC%A2%E8%BF%8E%E7%9A%84%E7%89%9B/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P2341" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>基本操作,先把强联通分量缩成点</p><p>然后我们观察一下发现,如果一个强连通分量里的牛为明星牛,那么这个强联通分量在缩点之后的图上的出度一定为0,否则这个强连通分量不是强连通分量,而是可以与指向的那个合并,因为互相喜欢</p><p>但同时,这个明星强联通分量只能有一个,否则说明强联通分量不被其他所有强联通分量喜欢,也就是ans=0</p><h2>Code</h2><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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"><vector></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stack></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 10010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="built_in">vector</span><<span class="keyword">int</span>> G[MAXN],G2[MAXN];</span><br><span class="line"><span class="built_in">stack</span><<span class="keyword">int</span>> s;</span><br><span class="line"><span class="keyword">int</span> dfn[MAXN],low[MAXN],colcnt,col[MAXN],colnum[MAXN],ncnt,indeg[MAXN];</span><br><span class="line"><span class="keyword">bool</span> ins[MAXN],ned[MAXN];</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">paint</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"> s.pop();</span><br><span class="line"> ins[x]=<span class="number">0</span>;</span><br><span class="line"> col[x]=colcnt;</span><br><span class="line"> colnum[colcnt]++;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">tarjan</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> temp;</span><br><span class="line"> dfn[x]=low[x]=++ncnt;</span><br><span class="line"> s.push(x);</span><br><span class="line"> ins[x]=<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<G[x].size();++i)</span><br><span class="line"> {</span><br><span class="line"> temp=G[x][i];</span><br><span class="line"> <span class="keyword">if</span>(!dfn[temp])</span><br><span class="line"> {</span><br><span class="line"> tarjan(temp);</span><br><span class="line"> low[x]=min(low[x],low[temp]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(ins[temp])</span><br><span class="line"> low[x]=min(low[x],low[temp]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(low[x]==dfn[x])</span><br><span class="line"> {</span><br><span class="line"> ++colcnt;</span><br><span class="line"> <span class="keyword">while</span>((temp=s.top())!=x)</span><br><span class="line"> paint(temp);</span><br><span class="line"> paint(temp);</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> tx,ty,ans=<span class="number">0</span>;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();</span><br><span class="line"> G[tx].push_back(ty);</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="keyword">if</span>(!dfn[i])</span><br><span class="line"> tarjan(i);</span><br><span class="line"> <span class="keyword">if</span>(colcnt==<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,n);</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"> rep(i,<span class="number">1</span>,n)<span class="comment">//缩点 </span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<G[i].size();++j)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(col[G[i][j]]!=col[i])</span><br><span class="line"> {</span><br><span class="line"> G2[col[i]].push_back(col[G[i][j]]);</span><br><span class="line"> ned[col[i]]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,colcnt)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(ans&&!ned[i])</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"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>(!ned[i])</span><br><span class="line"> ans+=colnum[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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>]]></content>
<tags>
<tag> 图论 </tag>
<tag> tarjan </tag>
<tag> 缩点 </tag>
<tag> 强连通分量 </tag>
</tags>
</entry>
<entry>
<title>序列合并</title>
<link href="/2018/11/06/%E5%BA%8F%E5%88%97%E5%90%88%E5%B9%B6/"/>
<url>/2018/11/06/%E5%BA%8F%E5%88%97%E5%90%88%E5%B9%B6/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1631" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>基本相当于《最小函数值》的弱化版</p><p>先把a,b排序,然后把a[1]和b[1~n]的加到堆里</p><p>然后枚举,并且在a[i]+b[j]>=堆顶元素时切换到下一个</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 100010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line">priority_queue<<span class="keyword">int</span>> q;</span><br><span class="line"><span class="keyword">int</span> a[MAXN],b[MAXN],ans[MAXN],n,cnt;</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"> n=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)a[i]=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)b[i]=read();</span><br><span class="line"> sort(a+<span class="number">1</span>,a+n+<span class="number">1</span>);</span><br><span class="line"> sort(b+<span class="number">1</span>,b+n+<span class="number">1</span>);</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> q.push(a[<span class="number">1</span>]+b[i]);</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> {</span><br><span class="line"> rep(j,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i]+b[j]<q.top())</span><br><span class="line"> {</span><br><span class="line"> q.pop();</span><br><span class="line"> q.push(a[i]+b[j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</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 class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> ans[++cnt]=q.top();</span><br><span class="line"> q.pop();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(cnt)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,ans[cnt--]);</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>
<tags>
<tag> 贪心 </tag>
<tag> 堆 </tag>
</tags>
</entry>
<entry>
<title>最小函数值</title>
<link href="/2018/11/06/%E6%9C%80%E5%B0%8F%E5%87%BD%E6%95%B0%E5%80%BC/"/>
<url>/2018/11/06/%E6%9C%80%E5%B0%8F%E5%87%BD%E6%95%B0%E5%80%BC/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P2085" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>维护一个大根堆,如果当前函数在x=j的值小于堆顶元素,弹掉堆顶,加入当前值</p><p>如果当前函数在x=j的值大于等于堆顶元素并且在x=j的导数值大于上一个函数在x=j的导数值,说明这个函数后面也不会比之前的函数更优,直接换到下一个函数即可</p><p>发现有了这种求前k大值得方法好像快排求前k大就比较鸡肋啊。。。?不知道是不是还有其他用途,毕竟快排是个分治算法。。。就像线性筛不能完全替代埃氏筛?</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 10010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line">priority_queue<<span class="keyword">int</span>> q;</span><br><span class="line"><span class="keyword">int</span> a[MAXN],b[MAXN],c[MAXN],n,m;</span><br><span class="line"><span class="keyword">int</span> ans[MAXN],cnt;</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> temp;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> a[i]=read();b[i]=read();c[i]=read();</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,m)q.push(a[<span class="number">1</span>]*i*i+b[<span class="number">1</span>]*i+c[<span class="number">1</span>]);</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> {</span><br><span class="line"> rep(j,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> temp=a[i]*j*j+b[i]*j+c[i];</span><br><span class="line"> <span class="keyword">if</span>(temp<q.top())</span><br><span class="line"> {</span><br><span class="line"> q.pop();</span><br><span class="line"> q.push(temp);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(<span class="number">2</span>*a[i]*j+b[i]>=<span class="number">2</span>*a[i<span class="number">-1</span>]*j+b[i<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 class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> ans[++cnt]=q.top();</span><br><span class="line"> q.pop();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(cnt)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,ans[cnt--]);</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>
<tags>
<tag> 堆 </tag>
<tag> 导数 </tag>
</tags>
</entry>
<entry>
<title>HAOI2009巧克力&矩形分割</title>
<link href="/2018/11/06/HAOI2009%E5%B7%A7%E5%85%8B%E5%8A%9B&%E7%9F%A9%E5%BD%A2%E5%88%86%E5%89%B2/"/>
<url>/2018/11/06/HAOI2009%E5%B7%A7%E5%85%8B%E5%8A%9B&%E7%9F%A9%E5%BD%A2%E5%88%86%E5%89%B2/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P3173" target="_blank" rel="noopener">HAOI2009巧克力</a></p><p><a href="https://www.luogu.org/problemnew/show/P1324" target="_blank" rel="noopener">矩形分割</a></p><a id="more"></a><h2>sol</h2><p>不难想到一个O(nm)的dp(应该是正确的。。。)</p><p>但是显然复杂度过不去</p><p>然后发现贪心就可以。。。</p><p>其实x和y切割线权值跟线的位置编号是无关的。。。所以可以sort一遍</p><p>然后考虑切的策略,如果先切权值小的,那么权值大的放到后面对切割花费的贡献会更大,花费总和也就会更大,所以我们先切大的即可。。。</p><p>现在做贪心题总感觉贪心不对。。。除非是在知道这个题贪心可做的情况下才敢信心满满的去打贪心。。。以后还是先要考虑一下贪心再去考虑其他dp之类的做法。。。除非有什么不满足贪心性质的地方再去想什么dp之类的。。。</p><h2>Code</h2><figure class="highlight cpp"><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"><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 10010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &a,<span class="keyword">const</span> <span class="keyword">int</span> &b)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">return</span> a>b;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> x[MAXN],y[MAXN],n,m;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans;</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> l=<span class="number">1</span>,r=<span class="number">1</span>;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<n;++i)x[i]=read();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i<m;++i)y[i]=read();</span><br><span class="line"> sort(x+<span class="number">1</span>,x+n,cmp);sort(y+<span class="number">1</span>,y+m,cmp);</span><br><span class="line"> <span class="keyword">while</span>(l<n&&r<m)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(x[l]>y[r])</span><br><span class="line"> {</span><br><span class="line"> ans+=x[l]*r;</span><br><span class="line"> ++l;</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"> ans+=y[r]*l;</span><br><span class="line"> ++r;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(l<n)</span><br><span class="line"> {</span><br><span class="line"> ans+=x[l]*r;++l;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(r<m)</span><br><span class="line"> {</span><br><span class="line"> ans+=y[r]*l;++r;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<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>]]></content>
<tags>
<tag> 贪心 </tag>
</tags>
</entry>
<entry>
<title>相似基因</title>
<link href="/2018/11/06/%E7%9B%B8%E4%BC%BC%E5%9F%BA%E5%9B%A0/"/>
<url>/2018/11/06/%E7%9B%B8%E4%BC%BC%E5%9F%BA%E5%9B%A0/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1140" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>总是想做成一维的,但是发现做不出</p><p>dp[i]/[j]表示第一串到第i位,第二串到第j位的最大相似度</p><p>然后不难发现可以从(i,j-1) / (i-1,j) / (i-1,j-1)转移过来</p><p>//但是不知道为什么从(0,0)开始 往后推不对,就是从(i,j)到(i)</p><p>↑错误的原因是没有(len1,len2)从(len1-1,len2)和(len1,len2-1)转移过来的情况,居然想了一个小时都没想出来为什么错。。。写这篇博客的时候重新写了一下三种转移恍然大悟。。。稍微改动一下就可以ac了。。。</p><p>WA的转移</p><figure class="highlight cpp"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<l1;++i)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<l2;++j)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[i+<span class="number">1</span>][j+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> vis[i+<span class="number">1</span>][j+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> dp[i+<span class="number">1</span>][j+<span class="number">1</span>]=dp[i][j]+mapp[ja[i+<span class="number">1</span>]][jb[j+<span class="number">1</span>]];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i+<span class="number">1</span>][j+<span class="number">1</span>]=max(dp[i+<span class="number">1</span>][j+<span class="number">1</span>],mapp[ja[i+<span class="number">1</span>]][jb[j+<span class="number">1</span>]]+dp[i][j]);</span><br><span class="line"> <span class="keyword">if</span>(!vis[i+<span class="number">1</span>][j])</span><br><span class="line"> {</span><br><span class="line"> vis[i+<span class="number">1</span>][j]=<span class="number">1</span>;</span><br><span class="line"> dp[i+<span class="number">1</span>][j]=dp[i][j]+mapp[ja[i+<span class="number">1</span>]][<span class="number">0</span>]; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i+<span class="number">1</span>][j]=max(dp[i+<span class="number">1</span>][j],mapp[ja[i+<span class="number">1</span>]][<span class="number">0</span>]+dp[i][j]);</span><br><span class="line"> <span class="keyword">if</span>(!vis[i][j+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> vis[i][j+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> dp[i][j+<span class="number">1</span>]=dp[i][j]+mapp[<span class="number">0</span>][jb[j+<span class="number">1</span>]];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i][j+<span class="number">1</span>]=max(dp[i][j+<span class="number">1</span>],dp[i][j]+mapp[<span class="number">0</span>][jb[j+<span class="number">1</span>]]);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>正确的转移</p><figure class="highlight cpp"><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="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<=l1;++i)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<=l2;++j)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(i+<span class="number">1</span><=l1&&j+<span class="number">1</span><=l2)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[i+<span class="number">1</span>][j+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> vis[i+<span class="number">1</span>][j+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> dp[i+<span class="number">1</span>][j+<span class="number">1</span>]=dp[i][j]+mapp[ja[i+<span class="number">1</span>]][jb[j+<span class="number">1</span>]];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i+<span class="number">1</span>][j+<span class="number">1</span>]=max(dp[i+<span class="number">1</span>][j+<span class="number">1</span>],mapp[ja[i+<span class="number">1</span>]][jb[j+<span class="number">1</span>]]+dp[i][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(i+<span class="number">1</span><=l1)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[i+<span class="number">1</span>][j])</span><br><span class="line"> {</span><br><span class="line"> vis[i+<span class="number">1</span>][j]=<span class="number">1</span>;</span><br><span class="line"> dp[i+<span class="number">1</span>][j]=dp[i][j]+mapp[ja[i+<span class="number">1</span>]][<span class="number">4</span>]; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i+<span class="number">1</span>][j]=max(dp[i+<span class="number">1</span>][j],mapp[ja[i+<span class="number">1</span>]][<span class="number">4</span>]+dp[i][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(j+<span class="number">1</span><=l2)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[i][j+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> vis[i][j+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> dp[i][j+<span class="number">1</span>]=dp[i][j]+mapp[<span class="number">4</span>][jb[j+<span class="number">1</span>]];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i][j+<span class="number">1</span>]=max(dp[i][j+<span class="number">1</span>],dp[i][j]+mapp[<span class="number">4</span>][jb[j+<span class="number">1</span>]]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>并不喜欢题解里的从当前的减去那种转移方式。。。感觉初始化不好想,而且在这里感觉没那么直观显然。。。</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mapp[<span class="number">5</span>][<span class="number">5</span>]=</span><br><span class="line">{</span><br><span class="line"> {<span class="number">5</span>,<span class="number">-1</span>,<span class="number">-2</span>,<span class="number">-1</span>,<span class="number">-3</span>},</span><br><span class="line"> {<span class="number">-1</span>,<span class="number">5</span>,<span class="number">-3</span>,<span class="number">-2</span>,<span class="number">-4</span>},</span><br><span class="line"> {<span class="number">-2</span>,<span class="number">-3</span>,<span class="number">5</span>,<span class="number">-2</span>,<span class="number">-2</span>},</span><br><span class="line"> {<span class="number">-1</span>,<span class="number">-2</span>,<span class="number">-2</span>,<span class="number">5</span>,<span class="number">-1</span>},</span><br><span class="line"> {<span class="number">-3</span>,<span class="number">-4</span>,<span class="number">-2</span>,<span class="number">-1</span>,<span class="number">0</span>}</span><br><span class="line">};</span><br><span class="line"><span class="keyword">int</span> dp[<span class="number">101</span>][<span class="number">101</span>];</span><br><span class="line"><span class="keyword">int</span> l1,l2,ja[<span class="number">101</span>],jb[<span class="number">101</span>];</span><br><span class="line"><span class="keyword">bool</span> vis[<span class="number">101</span>][<span class="number">101</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">char</span> c;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&l1);</span><br><span class="line"> rep(i,<span class="number">1</span>,l1)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'A'</span>)ja[i]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'C'</span>)ja[i]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'G'</span>)ja[i]=<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'T'</span>)ja[i]=<span class="number">3</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&l2);</span><br><span class="line"> rep(i,<span class="number">1</span>,l2)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'A'</span>)jb[i]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'C'</span>)jb[i]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'G'</span>)jb[i]=<span class="number">2</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'T'</span>)jb[i]=<span class="number">3</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">0</span>;i<=l1;++i)</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<=l2;++j)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(i+<span class="number">1</span><=l1&&j+<span class="number">1</span><=l2)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[i+<span class="number">1</span>][j+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> vis[i+<span class="number">1</span>][j+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> dp[i+<span class="number">1</span>][j+<span class="number">1</span>]=dp[i][j]+mapp[ja[i+<span class="number">1</span>]][jb[j+<span class="number">1</span>]];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i+<span class="number">1</span>][j+<span class="number">1</span>]=max(dp[i+<span class="number">1</span>][j+<span class="number">1</span>],mapp[ja[i+<span class="number">1</span>]][jb[j+<span class="number">1</span>]]+dp[i][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(i+<span class="number">1</span><=l1)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[i+<span class="number">1</span>][j])</span><br><span class="line"> {</span><br><span class="line"> vis[i+<span class="number">1</span>][j]=<span class="number">1</span>;</span><br><span class="line"> dp[i+<span class="number">1</span>][j]=dp[i][j]+mapp[ja[i+<span class="number">1</span>]][<span class="number">4</span>]; </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i+<span class="number">1</span>][j]=max(dp[i+<span class="number">1</span>][j],mapp[ja[i+<span class="number">1</span>]][<span class="number">4</span>]+dp[i][j]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(j+<span class="number">1</span><=l2)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[i][j+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> vis[i][j+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> dp[i][j+<span class="number">1</span>]=dp[i][j]+mapp[<span class="number">4</span>][jb[j+<span class="number">1</span>]];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> dp[i][j+<span class="number">1</span>]=max(dp[i][j+<span class="number">1</span>],dp[i][j]+mapp[<span class="number">4</span>][jb[j+<span class="number">1</span>]]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<dp[l1][l2];</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>
<tags>
<tag> 动态规划 </tag>
</tags>
</entry>
<entry>
<title>【USACO07JAN】区间统计Tallest Cow</title>
<link href="/2018/11/06/%5BUSACO07JAN%5D%E5%8C%BA%E9%97%B4%E7%BB%9F%E8%AE%A1Tallest%20Cow/"/>
<url>/2018/11/06/%5BUSACO07JAN%5D%E5%8C%BA%E9%97%B4%E7%BB%9F%E8%AE%A1Tallest%20Cow/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P2879" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>一开始以为是个线段树或者分块之类的数据结构,但是对于最后的输出并不是很好</p><p>然后发现可以差分</p><p>一开始所有的身高都是可能的最大身高,每当出现一对(a,b)相望时,[a+1,b-1]中的身高-1</p><p>注意到会有重复数据,所以要判重</p><p>直接用了个map做掉了。。。</p><p>不判重就只有五十分。。。以后还是要考虑一下数据的问题。。。</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><map></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> cf[<span class="number">10010</span>];</span><br><span class="line"><span class="built_in">map</span><<span class="keyword">int</span>,<span class="keyword">int</span>> sb;</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> tx,ty;</span><br><span class="line"> <span class="keyword">int</span> n=read(),I=read(),Mh=read(),r=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,r)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();</span><br><span class="line"> <span class="keyword">if</span>(tx>ty)swap(tx,ty);</span><br><span class="line"> <span class="keyword">if</span>(ty-tx<=<span class="number">1</span>)<span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">if</span>(sb[tx*n+ty])<span class="keyword">continue</span>;</span><br><span class="line"> sb[tx*n+ty]=<span class="number">1</span>;</span><br><span class="line"> cf[tx]-=<span class="number">1</span>;</span><br><span class="line"> cf[ty<span class="number">-1</span>]+=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,Mh);</span><br><span class="line"> Mh+=cf[i];</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>
<tags>
<tag> 差分 </tag>
<tag> 哈希判重 </tag>
</tags>
</entry>
<entry>
<title>JLOI2012树</title>
<link href="/2018/11/06/JLOI2012%E6%A0%91/"/>
<url>/2018/11/06/JLOI2012%E6%A0%91/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P3252" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>一开始以为树形dp什么的。。。但是是多叉树就不会了。。。</p><p>然后想的是: dfs,每搜到一个叶子节点就开始在栈内节点上尺取(好像也叫two-pointers)?但是并不会打尺取/two-pointers</p><p>所以就每次搜到叶子做一个前缀和,然后对于每一个找</p><p>是否存在summ[j],使得summ[now]-summ[j]==S</p><p>然后注意到点权都是正整数,所以可以二分查找</p><p>但这个复杂度好像并不对,最坏应该是O( (n^2) * (log n -log 4) / 4) ,是n^2 log n级别的了,但是题目好像并没有卡</p><p>如果尺取的话复杂度的log应该就没了,也就n^2级别,只是常数比下面的搜索小一点?</p><p>其实从每个点开始dfs的最坏复杂度也就O(n^2 / 2) ,就是n^2级别的了</p><p>所以感觉这两种并不是正解?</p><p>但是并不会其他做法。。。我好菜啊。。。</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 100010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next;</span><br><span class="line">}e[MAXN];</span><br><span class="line"><span class="keyword">int</span> last[MAXN],dp[MAXN],v[MAXN];</span><br><span class="line"><span class="keyword">int</span> n,s,myStack[MAXN],tail,ans;</span><br><span class="line"><span class="keyword">int</span> summ[MAXN];</span><br><span class="line"><span class="keyword">bool</span> vis[MAXN],notRoot[MAXN];</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> x)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> myStack[++tail]=x;</span><br><span class="line"> <span class="keyword">if</span>(!last[x])</span><br><span class="line"> {</span><br><span class="line"> rep(i,<span class="number">1</span>,tail)</span><br><span class="line"> summ[i]=summ[i<span class="number">-1</span>]+v[myStack[i]];</span><br><span class="line"> rep(i,<span class="number">1</span>,tail)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(vis[myStack[i]])</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> vis[myStack[i]]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>((*lower_bound(summ,summ+i+<span class="number">1</span>,summ[i]-s))==summ[i]-s)</span><br><span class="line"> ++ans;</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> temp=last[x];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> dfs(e[temp].to);</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> --tail;</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> tx,ty;</span><br><span class="line"> n=read();s=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> v[i]=read();</span><br><span class="line"> <span class="keyword">if</span>(v[i]==s)</span><br><span class="line"> {</span><br><span class="line"> ++ans;</span><br><span class="line"> vis[i]=<span class="number">1</span>;</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;++i)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();</span><br><span class="line"> e[i].next=last[tx];</span><br><span class="line"> last[tx]=i;</span><br><span class="line"> e[i].to=ty;</span><br><span class="line"> notRoot[ty]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="keyword">if</span>(!notRoot[i])</span><br><span class="line"> {</span><br><span class="line"> dfs(i);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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>]]></content>
<tags>
<tag> DFS </tag>
<tag> 二分查找 </tag>
<tag> 前缀和 </tag>
</tags>
</entry>
<entry>
<title>树的重量(构造答案)</title>
<link href="/2018/11/06/%E6%A0%91%E7%9A%84%E9%87%8D%E9%87%8F(%E6%9E%84%E9%80%A0%E7%AD%94%E6%A1%88)/"/>
<url>/2018/11/06/%E6%A0%91%E7%9A%84%E9%87%8D%E9%87%8F(%E6%9E%84%E9%80%A0%E7%AD%94%E6%A1%88)/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1268" target="_blank" rel="noopener">题目链接</a></p><p>一道很不错的题</p><a id="more"></a><h2>sol</h2><p>一开始想到了最小生成树,但是后来发现k可以在图上加点,边可以重叠</p><p>然后就不会了。。。</p><p>先从小的数据开始考虑,如果有两个点,显然ans=dis[1]/[2]</p><p>在考虑3个点,有可能到3的边是从1->2上延伸出来的一条“叉边”</p><p>叉边其实是这种情况:</p><p><img src="https://cdn.luogu.org/upload/pic/42347.png" alt=""></p><p>然后1和2直接连边,枚举i=3~n找从1~i-1最小的延伸出的叉边</p><p>结合上图等价关系不难发现:叉边长度=(dis[1]/[i]+dis[j]/[i]-dis[i]/[j])/2</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line">{</span><br><span class="line">a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> dis[<span class="number">31</span>][<span class="number">31</span>],n;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans;</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> minn;</span><br><span class="line"><span class="keyword">while</span>(n=read())</span><br><span class="line">{</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,i+<span class="number">1</span>,n)</span><br><span class="line"> dis[i][j]=read();</span><br><span class="line">ans=dis[<span class="number">1</span>][<span class="number">2</span>];</span><br><span class="line">rep(i,<span class="number">3</span>,n)</span><br><span class="line">{</span><br><span class="line">minn=<span class="number">2147483647</span>;</span><br><span class="line">rep(j,<span class="number">2</span>,i<span class="number">-1</span>)</span><br><span class="line"> minn=min(minn,(dis[<span class="number">1</span>][i]+dis[j][i]-dis[<span class="number">1</span>][j])/<span class="number">2</span>);</span><br><span class="line">ans+=minn;</span><br><span class="line"><span class="comment">//cout<<ans<<"test\n";</span></span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</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>
<tags>
<tag> 图论 </tag>
<tag> 构造 </tag>
</tags>
</entry>
<entry>
<title>请柬/邮递员送信(反图spfa)</title>
<link href="/2018/10/28/%E8%AF%B7%E6%9F%AC%EF%BC%8C%E9%82%AE%E9%80%92%E5%91%98%E9%80%81%E4%BF%A1/"/>
<url>/2018/10/28/%E8%AF%B7%E6%9F%AC%EF%BC%8C%E9%82%AE%E9%80%92%E5%91%98%E9%80%81%E4%BF%A1/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1342" target="_blank" rel="noopener">请柬</a></p><p><a href="https://www.luogu.org/problemnew/show/P1629" target="_blank" rel="noopener">邮递员送信</a></p><a id="more"></a><h2>sol</h2><p>这两道题好像基本上是完全一样的啊。。。不知道为什么一道黄一道蓝。。。</p><p>如果是无向图要来回时间直接*2</p><p>有向图的话,显然的做法是从每个点跑一遍spfa找到起点的最短路。。。但是显然过不了</p><p>其实上面等价于在反图上跑一遍spfa</p><p>所以我们正图跑一边spfa,反图跑一边spfa,这样正图上的是1点到其他点的最短路,反图上是其他点到1点的最短路,不得不说图论真的是神奇。。。</p><h2>Code</h2><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</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"><cstring></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="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[<span class="number">100010</span>],ve[<span class="number">100010</span>];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">1001</span>],vlast[<span class="number">1001</span>],cnt,vcnt;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> dis[<span class="number">1001</span>],vdis[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">bool</span> vis[<span class="number">1001</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">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">vadd</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ve[++vcnt].next=vlast[from];</span><br><span class="line"> vlast[from]=vcnt;</span><br><span class="line"> ve[vcnt].to=to;</span><br><span class="line"> ve[vcnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</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>(vis,<span class="number">0</span>,<span class="keyword">sizeof</span> vis);</span><br><span class="line"> <span class="keyword">int</span> temp;</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> dis[i]=<span class="number">-1</span>;</span><br><span class="line"> dis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">queue</span><<span class="keyword">int</span>> q;</span><br><span class="line"> q.push(<span class="number">1</span>);</span><br><span class="line"> vis[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> temp=last[q.front()];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[e[temp].to]<<span class="number">0</span>||dis[e[temp].to]>dis[q.front()]+e[temp].v)</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to]=dis[q.front()]+e[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!vis[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> vis[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> q.push(e[temp].to);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> vis[q.front()]=<span class="number">0</span>;</span><br><span class="line"> q.pop();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">vspfa</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>(vis,<span class="number">0</span>,<span class="keyword">sizeof</span> vis);</span><br><span class="line"> <span class="keyword">int</span> temp;</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> vdis[i]=<span class="number">-1</span>;</span><br><span class="line"> vdis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">queue</span><<span class="keyword">int</span>> q;</span><br><span class="line"> q.push(<span class="number">1</span>);</span><br><span class="line"> vis[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> temp=vlast[q.front()];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(vdis[ve[temp].to]<<span class="number">0</span>||vdis[ve[temp].to]>vdis[q.front()]+ve[temp].v)</span><br><span class="line"> {</span><br><span class="line"> vdis[ve[temp].to]=vdis[q.front()]+ve[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!vis[ve[temp].to])</span><br><span class="line"> {</span><br><span class="line"> vis[ve[temp].to]=<span class="number">1</span>;</span><br><span class="line"> q.push(ve[temp].to);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=ve[temp].next;</span><br><span class="line"> }</span><br><span class="line"> vis[q.front()]=<span class="number">0</span>;</span><br><span class="line"> q.pop();</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><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> ans=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> tx,ty,tz;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> vadd(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> spfa();</span><br><span class="line"> vspfa();</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> {</span><br><span class="line"> ans+=(dis[i]+vdis[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<ans;</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">//请柬</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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[<span class="number">1000010</span>],ve[<span class="number">1000010</span>];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">int</span> vlast[<span class="number">1000001</span>],last[<span class="number">1000001</span>],cnt;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> dis[<span class="number">1000001</span>],ans;</span><br><span class="line"><span class="keyword">bool</span> inq[<span class="number">1000001</span>];</span><br><span class="line"><span class="built_in">queue</span><<span class="keyword">int</span>> q;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">vadd</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> ve[cnt].next=vlast[from];</span><br><span class="line"> vlast[from]=cnt;</span><br><span class="line"> ve[cnt].to=to;</span><br><span class="line"> ve[cnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">spfa1</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> temp;</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> dis[i]=<span class="number">2147483647</span>;</span><br><span class="line"> dis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> inq[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> q.push(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> temp=last[q.front()];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[e[temp].to]>dis[q.front()]+e[temp].v)</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to]=dis[q.front()]+e[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> q.push(e[temp].to);</span><br><span class="line"> inq[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> inq[q.front()]=<span class="number">0</span>;</span><br><span class="line"> q.pop();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">spfa2</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> temp;</span><br><span class="line"> rep(i,<span class="number">2</span>,n)</span><br><span class="line"> dis[i]=<span class="number">2147483647</span>;</span><br><span class="line"> dis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> inq[<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> q.push(<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">while</span>(!q.empty())</span><br><span class="line"> {</span><br><span class="line"> temp=vlast[q.front()];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[ve[temp].to]>dis[q.front()]+ve[temp].v)</span><br><span class="line"> {</span><br><span class="line"> dis[ve[temp].to]=dis[q.front()]+ve[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!inq[ve[temp].to])</span><br><span class="line"> {</span><br><span class="line"> q.push(ve[temp].to);</span><br><span class="line"> inq[ve[temp].to]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=ve[temp].next;</span><br><span class="line"> }</span><br><span class="line"> inq[q.front()]=<span class="number">0</span>;</span><br><span class="line"> q.pop();</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> tx,ty,tz;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> vadd(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> spfa1();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[i]!=<span class="number">2147483647</span>)</span><br><span class="line"> ans+=dis[i];</span><br><span class="line"> }</span><br><span class="line"> spfa2();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[i]!=<span class="number">2147483647</span>)</span><br><span class="line"> ans+=dis[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,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><h2>总结</h2>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 最短路 </tag>
</tags>
</entry>
<entry>
<title>跑路</title>
<link href="/2018/10/28/%E8%B7%91%E8%B7%AF/"/>
<url>/2018/10/28/%E8%B7%91%E8%B7%AF/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1613" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>这道题是最短路径和倍增思想的结合题。</p><p>观察到每条边长度均为1km,也就是2^0</p><p>再观察跑路器,每秒钟可以跑2^k千米</p><p>所以用倍增,如果u,v之间存在一条路径长度为2^k千米,那么可以转化为它们之间存在一条消耗时间为1的边</p><p>如果u,v之间存在一个点w, u,w之间存在一条路径长度为2^k,w,v之间存在一条路径长度为2^k,那么u,v之间存在一条长度为2^{k+1}的路径,当然也是消耗时间为1</p><p>按这个方法跑一边floyd来建图,然后再跑一遍Floyd找最短路径即可</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">bool</span> dp[<span class="number">70</span>][<span class="number">51</span>][<span class="number">51</span>];</span><br><span class="line"><span class="keyword">int</span> n,m,dis[<span class="number">51</span>][<span class="number">51</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> tx,ty;</span><br><span class="line"> <span class="built_in">memset</span>(dis,<span class="number">0x3f3f3f3f</span>,<span class="keyword">sizeof</span> dis);</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();</span><br><span class="line"> dp[<span class="number">0</span>][tx][ty]=<span class="number">1</span>;</span><br><span class="line"> dis[tx][ty]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> rep(w,<span class="number">1</span>,<span class="number">64</span>)</span><br><span class="line"> rep(k,<span class="number">1</span>,n)</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!dp[w][i][j])</span><br><span class="line"> dp[w][i][j]=(dp[w<span class="number">-1</span>][i][k]&dp[w<span class="number">-1</span>][k][j]);</span><br><span class="line"> <span class="keyword">if</span>(dp[w][i][j])</span><br><span class="line"> dis[i][j]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> rep(k,<span class="number">1</span>,n)</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,<span class="number">1</span>,n)</span><br><span class="line"> dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);</span><br><span class="line"> <span class="built_in">cout</span><<dis[<span class="number">1</span>][n];</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>总结</h2><p>神奇的操作。。。</p>]]></content>
<tags>
<tag> 最短路 </tag>
<tag> Floyd </tag>
<tag> 倍增 </tag>
</tags>
</entry>
<entry>
<title>三角形牧场</title>
<link href="/2018/10/28/%E4%B8%89%E8%A7%92%E5%BD%A2%E7%89%A7%E5%9C%BA/"/>
<url>/2018/10/28/%E4%B8%89%E8%A7%92%E5%BD%A2%E7%89%A7%E5%9C%BA/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1284" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>这题加深了我对背包的一些理解。。。</p><p>可以用来求哪些可以表示到,砝码称重好像也是这个思想?</p><p>一开始想的是:</p><p>b+c>a , b<a , c<a, a=sum-b-c</p><p>然后就有sum/2<b+c<2*sum/3</p><p>然后背包表示b+c能取到的,但是b+c还能再分,所以不行</p><p>所以就是一个二维背包,状态转移可以看代码,其实还是比较好想的</p><p>但是从来没想过背包还能这样。。。神奇。。。</p><p>说到底还是我太弱了。。。</p><p>用一个二维表示能不能取到两个边长i,j,如果dp_{i,j}==true说明可以取到,然后就能求出另一条边长</p><p>然后判一下合法性+海伦公式就行了</p><h2>Code</h2><figure class="highlight cpp"><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"><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> n,len[<span class="number">41</span>];</span><br><span class="line"><span class="keyword">int</span> ll,rr,sum;</span><br><span class="line"><span class="keyword">int</span> ans=<span class="number">-1</span>;</span><br><span class="line"><span class="keyword">double</span> p;</span><br><span class="line"><span class="keyword">bool</span> dp[<span class="number">801</span>][<span class="number">801</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</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">int</span> a,b,c;</span><br><span class="line"> a=max(x,y);a=max(a,sum-x-y);</span><br><span class="line"> c=min(x,y);c=min(c,sum-x-y);</span><br><span class="line"> b=sum-a-c;</span><br><span class="line"> <span class="keyword">if</span>(b+c>a)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</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="function"><span class="keyword">int</span> <span class="title">calc</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"> <span class="keyword">return</span> <span class="built_in">sqrt</span>(p*(p-a)*(p-b)*(p-c))*<span class="number">100</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"> dp[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&len[i]);</span><br><span class="line"> sum+=len[i];</span><br><span class="line"> }</span><br><span class="line"> p=(<span class="keyword">double</span>)(<span class="number">1.0</span>*sum/<span class="number">2.0</span>);</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> per(j,<span class="number">800</span>,len[i])</span><br><span class="line"> per(k,j,<span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dp[j-len[i]][k])</span><br><span class="line"> dp[j][k]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(k-len[i]>=<span class="number">0</span>&&dp[j][k-len[i]])</span><br><span class="line"> dp[j][k]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,<span class="number">800</span>)</span><br><span class="line"> rep(j,<span class="number">1</span>,i)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dp[i][j]&&judge(i,j))</span><br><span class="line"> ans=max(ans,calc(i,j,sum-i-j));</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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><h2>总结</h2><p>第一次接触这样的背包以及用背包来求是否可以组合出这样的数,算是二维背包?</p><p>对dp还是理解不够深啊。。。</p>]]></content>
<tags>
<tag> 动态规划 </tag>
<tag> 背包 </tag>
</tags>
</entry>
<entry>
<title>逃离僵尸岛(图论操作-建立虚点)</title>
<link href="/2018/10/26/%E9%80%83%E7%A6%BB%E5%83%B5%E5%B0%B8%E5%B2%9B/"/>
<url>/2018/10/26/%E9%80%83%E7%A6%BB%E5%83%B5%E5%B0%B8%E5%B2%9B/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P3393" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>一个比较显然的想法是从每个被感染的点开始bfs处理出所有危险的点</p><p>但是更优(大概)的做法是建立一个0号虚点与所有被感染的点建立有向边后dfs,与0号点的距离<=s+1的点都是危险的点</p><p>然后就是注意细节了。。。在1号城市和N城市,不需要住店。。。</p><h2>Code</h2><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line">{</span><br><span class="line">t=<span class="number">1</span>;c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line">{</span><br><span class="line">a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"><span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[<span class="number">800010</span>];</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span>{</span></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> dis;</span><br><span class="line"><span class="keyword">int</span> last;</span><br><span class="line"><span class="keyword">bool</span> gr,dg,inq;</span><br><span class="line">}node[<span class="number">100001</span>];</span><br><span class="line"><span class="keyword">int</span> n,m,k,s,p,qq,cnt;</span><br><span class="line"><span class="built_in">queue</span><<span class="keyword">int</span>> q;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> from,<span class="keyword">int</span> to)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">e[++cnt].next=node[from].last;</span><br><span class="line">node[from].last=cnt;</span><br><span class="line">e[cnt].to=to;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">value</span><span class="params">(<span class="keyword">const</span> <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">return</span> node[x].dg?qq:p;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">spfa1</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> temp;</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> node[i].dis=<span class="number">-1</span>;</span><br><span class="line">q.push(<span class="number">0</span>);</span><br><span class="line">node[<span class="number">0</span>].inq=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">while</span>(!q.empty())</span><br><span class="line">{</span><br><span class="line">temp=node[q.front()].last;</span><br><span class="line"><span class="keyword">while</span>(temp)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(node[e[temp].to].dis<<span class="number">0</span>||node[e[temp].to].dis>node[q.front()].dis+<span class="number">1</span>)</span><br><span class="line">{</span><br><span class="line">node[e[temp].to].dis=node[q.front()].dis+<span class="number">1</span>;</span><br><span class="line"><span class="keyword">if</span>(node[e[temp].to].dis<s&&!node[e[temp].to].inq)</span><br><span class="line">{</span><br><span class="line">q.push(e[temp].to);</span><br><span class="line">node[e[temp].to].inq=<span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">temp=e[temp].next;</span><br><span class="line">}</span><br><span class="line">node[q.front()].inq=<span class="number">0</span>;</span><br><span class="line">q.pop();</span><br><span class="line">}</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">void</span> <span class="title">spfa2</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> temp;</span><br><span class="line">rep(i,<span class="number">2</span>,n)</span><br><span class="line"> node[i].dis=<span class="number">-1</span>;</span><br><span class="line">node[<span class="number">1</span>].dis=<span class="number">0</span>;</span><br><span class="line">q.push(<span class="number">1</span>);</span><br><span class="line">node[<span class="number">1</span>].inq=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">while</span>(!q.empty())</span><br><span class="line">{</span><br><span class="line">temp=node[q.front()].last;</span><br><span class="line"><span class="keyword">while</span>(temp)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(node[e[temp].to].gr)<span class="comment">//该城市被感染 ->不能经过 </span></span><br><span class="line">{</span><br><span class="line">temp=e[temp].next; </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>(node[e[temp].to].dis<<span class="number">0</span>||node[e[temp].to].dis>node[q.front()].dis+value(e[temp].to))</span><br><span class="line">{</span><br><span class="line">node[e[temp].to].dis=node[q.front()].dis+value(e[temp].to);</span><br><span class="line"><span class="keyword">if</span>(!node[e[temp].to].inq)</span><br><span class="line">{</span><br><span class="line">node[e[temp].to].inq=<span class="number">1</span>;</span><br><span class="line">q.push(e[temp].to);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">temp=e[temp].next;</span><br><span class="line">}</span><br><span class="line">node[q.front()].inq=<span class="number">0</span>;</span><br><span class="line">q.pop();</span><br><span class="line">}</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"><span class="keyword">int</span> tx,ty;</span><br><span class="line">n=read();m=read();k=read();s=read();p=read();qq=read();</span><br><span class="line">rep(i,<span class="number">1</span>,k)</span><br><span class="line">{</span><br><span class="line">tx=read();</span><br><span class="line">node[tx].gr=<span class="number">1</span>;</span><br><span class="line">add(<span class="number">0</span>,tx);</span><br><span class="line">}</span><br><span class="line">rep(i,<span class="number">1</span>,m)</span><br><span class="line">{</span><br><span class="line">tx=read();ty=read();</span><br><span class="line">add(tx,ty);add(ty,tx);</span><br><span class="line">}</span><br><span class="line">++s;</span><br><span class="line">spfa1();<span class="comment">//以0号为虚点,有向边连向所有被感染点,距离0点小于等于s+1的都是危险点 </span></span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="keyword">if</span>(node[i].dis><span class="number">0</span>&&node[i].dis<=s)</span><br><span class="line"> node[i].dg=<span class="number">1</span>;</span><br><span class="line">spfa2();</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%lld"</span>,node[n].dis-value(n));</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>
<tags>
<tag> 图论 </tag>
<tag> 最短路 </tag>
</tags>
</entry>
<entry>
<title>NOIP模拟赛 期望</title>
<link href="/2018/10/24/NOIP%E6%A8%A1%E6%8B%9F%E8%B5%9B%20%E6%9C%9F%E6%9C%9B/"/>
<url>/2018/10/24/NOIP%E6%A8%A1%E6%8B%9F%E8%B5%9B%20%E6%9C%9F%E6%9C%9B/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/U47078" target="_blank" rel="noopener">题目链接</a></p><a id="more"></a><h2>sol</h2><p>二进制下计算每一位出现0/1的概率</p><p>观察搜索树可知,显然可以dp</p><p>从上一个数每一位出现0/1的概率很容易转移到当前数每一位出现0/1的概率</p><p>复杂度O(n)</p><p>主要是细节问题,1个错误是第25行:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;b[i];++j,b[i]>>=<span class="number">1</span>)</span><br></pre></td></tr></table></figure><p>正确的写法是:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j<=<span class="number">31</span>;++j,b[i]>>=<span class="number">1</span>)</span><br></pre></td></tr></table></figure><p>因为如果前面有一个高位可以or出1,后面有个and,高位是0,第一种写法算不到那里,所以期望算出来应该是偏大?</p><p>其实是个很沙雕的错误,但是调了一上午orz</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> n,a[<span class="number">100001</span>],b[<span class="number">100001</span>];</span><br><span class="line"><span class="keyword">double</span> c[<span class="number">100001</span>],ans,dp[<span class="number">40</span>][<span class="number">2</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="comment">//freopen("exp.in","r",stdin);</span></span><br><span class="line"><span class="comment">//freopen("exp.out","w",stdout);</span></span><br><span class="line"><span class="keyword">double</span> x,y;</span><br><span class="line"><span class="keyword">bool</span> temp;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line">rep(i,<span class="number">1</span>,n)<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&a[i]);</span><br><span class="line">rep(i,<span class="number">1</span>,n)<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&b[i]);</span><br><span class="line">rep(i,<span class="number">1</span>,n)<span class="built_in">scanf</span>(<span class="string">"%lf"</span>,&c[i]);</span><br><span class="line">rep(i,<span class="number">1</span>,<span class="number">31</span>)</span><br><span class="line"> dp[i][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line">rep(i,<span class="number">1</span>,n)</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<=<span class="number">31</span>;++j,b[i]>>=<span class="number">1</span>)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(b[i]&<span class="number">1</span>)</span><br><span class="line"> temp=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> temp=<span class="number">0</span>;</span><br><span class="line">x=dp[j][<span class="number">0</span>];y=dp[j][<span class="number">1</span>];</span><br><span class="line"><span class="comment">//dp[j][0]=0;dp[j][1]=0;</span></span><br><span class="line"><span class="keyword">if</span>(a[i]==<span class="number">0</span>)<span class="comment">//and</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(temp)</span><br><span class="line">{</span><br><span class="line">dp[j][<span class="number">1</span>]=y;</span><br><span class="line">dp[j][<span class="number">0</span>]=<span class="number">1</span>-dp[j][<span class="number">1</span>];</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">dp[j][<span class="number">1</span>]=y*c[i];</span><br><span class="line">dp[j][<span class="number">0</span>]=<span class="number">1</span>-dp[j][<span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(a[i]==<span class="number">1</span>)<span class="comment">//or</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(temp)</span><br><span class="line">{</span><br><span class="line">dp[j][<span class="number">0</span>]=x*c[i];</span><br><span class="line">dp[j][<span class="number">1</span>]=<span class="number">1</span>-dp[j][<span class="number">0</span>];</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">dp[j][<span class="number">1</span>]=y;</span><br><span class="line">dp[j][<span class="number">0</span>]=<span class="number">1</span>-dp[j][<span class="number">1</span>];</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">if</span>(a[i]==<span class="number">2</span>)<span class="comment">//xor</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(temp)</span><br><span class="line">{</span><br><span class="line">dp[j][<span class="number">0</span>]=x*c[i]+y*(<span class="number">1</span>-c[i]);</span><br><span class="line">dp[j][<span class="number">1</span>]=<span class="number">1</span>-dp[j][<span class="number">0</span>];</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">dp[j][<span class="number">0</span>]=x;</span><br><span class="line">dp[j][<span class="number">1</span>]=<span class="number">1</span>-dp[j][<span class="number">0</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">rep(i,<span class="number">1</span>,<span class="number">31</span>)</span><br><span class="line"> ans+=(<span class="number">1</span><<(i<span class="number">-1</span>))*dp[i][<span class="number">1</span>];</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%.1f"</span>,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>]]></content>
<tags>
<tag> 期望 </tag>
<tag> 动态规划 </tag>
</tags>
</entry>
<entry>
<title>欧拉定理及扩展欧拉定理</title>
<link href="/2018/10/18/%E6%AC%A7%E6%8B%89%E5%AE%9A%E7%90%86%E5%8F%8A%E6%89%A9%E5%B1%95%E6%AC%A7%E6%8B%89%E5%AE%9A%E7%90%86/"/>
<url>/2018/10/18/%E6%AC%A7%E6%8B%89%E5%AE%9A%E7%90%86%E5%8F%8A%E6%89%A9%E5%B1%95%E6%AC%A7%E6%8B%89%E5%AE%9A%E7%90%86/</url>
<content type="html"><![CDATA[<p>待填坑</p><a id="more"></a><p><a href="https://www.luogu.org/problemnew/show/P1397" target="_blank" rel="noopener">例题</a></p>]]></content>
<tags>
<tag> 数论 </tag>
</tags>
</entry>
<entry>
<title>初涉差分约束</title>
<link href="/2018/10/18/%E5%B7%AE%E5%88%86%E7%BA%A6%E6%9D%9F%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<url>/2018/10/18/%E5%B7%AE%E5%88%86%E7%BA%A6%E6%9D%9F%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</url>
<content type="html"><![CDATA[<p>待填坑</p><a id="more"></a>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 最短路 </tag>
<tag> 差分约束 </tag>
</tags>
</entry>
<entry>
<title>初探欧拉路径/欧拉回路(无序字母对)</title>
<link href="/2018/10/17/%E5%88%9D%E6%8E%A2%E6%AC%A7%E6%8B%89%E8%B7%AF%E5%BE%84/"/>
<url>/2018/10/17/%E5%88%9D%E6%8E%A2%E6%AC%A7%E6%8B%89%E8%B7%AF%E5%BE%84/</url>
<content type="html"><![CDATA[<p>初学欧拉路径</p><a id="more"></a><h3>欧拉路径和欧拉回路的概念</h3><p><a herf="[https://www.cnblogs.com/acxblog/">@ACX</a>:</p><figure class="highlight plain"><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><br><span class="line"></span><br><span class="line">如果这条路径的起点和终点重合,那么就是欧拉回路。</span><br></pre></td></tr></table></figure><p>欧拉回路就是一笔画最后又走回去了,欧拉路径就是走不回去但是可以一笔画。。。</p><p>所以要怎么确定一个图中是否存在欧拉路径/欧拉回路?</p><h3>无向图中</h3><p>欧拉路径:只有两个点的度数为奇数,其余均为偶数</p><p>欧拉回路:不存在度数为奇数的点</p><p><a href="https://www.cnblogs.com/acxblog/" target="_blank" rel="noopener">@ACX</a>:</p><figure class="highlight plain"><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><br><span class="line"></span><br><span class="line">如果是欧拉回路,奇点的个数应该为0。</span><br></pre></td></tr></table></figure><h3>有向图中</h3><p>欧拉路径:入度和出度不相等的点数不大于2(不一定是起点和终点,比如1->2->3->2,只有一个点入度不等于出度)</p><p>欧拉回路:所有点的入度均等于出度</p><p><a href="https://www.cnblogs.com/acxblog/" target="_blank" rel="noopener">@ACX</a>:</p><figure class="highlight plain"><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">有向图:欧拉路径中,最多只有两个点的入度不等于出度。起点出度比入度大1,终点入度比出度大1。</span><br><span class="line"></span><br><span class="line">如果是欧拉回路,所有点的 入度=出度 。</span><br></pre></td></tr></table></figure><h3>欧拉路径/欧拉回路的求法</h3><p>先找出起点和终点,然后dfs+回溯就ok</p><p><a herf="https://www.cnblogs.com/acxblog/p/7390301.html">这里说的比较好</a></p><hr><p>例题</p><p><a href="https://www.luogu.org/problemnew/show/P2731" target="_blank" rel="noopener">luoguP1341无序字母对</a></p><h2>sol</h2><p>给出一对字母对我们就可以看成是加了边,能生成n+1个字母的串包含这n个字母对的条件就是这个图包含欧拉路径/欧拉回路</p><p>也就是题目等价于:</p><p>给出n条边,要求求出一个字典序最小的方案,可以把每条边都走一遍,输出路径上的点</p><p>所以基本上就是一道欧拉路径/回路的模板题</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="keyword">bool</span> e[<span class="number">1001</span>][<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">int</span> deg[<span class="number">1001</span>],p_deg_cnt,s=<span class="number">1001</span>;</span><br><span class="line"><span class="keyword">char</span> o_seq[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">bool</span> sxy,solved;</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> now,<span class="keyword">int</span> depth)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">o_seq[depth]=now;</span><br><span class="line"><span class="keyword">if</span>(depth==n+<span class="number">1</span>)</span><br><span class="line">{</span><br><span class="line">solved=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line">rep(i,(<span class="keyword">int</span>)<span class="string">'A'</span>,(<span class="keyword">int</span>)<span class="string">'z'</span>)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(e[now][i])</span><br><span class="line">{</span><br><span class="line">e[now][i]=<span class="number">0</span>;e[i][now]=<span class="number">0</span>;</span><br><span class="line">dfs(i,depth+<span class="number">1</span>);</span><br><span class="line"><span class="keyword">if</span>(solved)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">e[now][i]=<span class="number">1</span>;e[i][now]=<span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</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"><span class="keyword">char</span> tx,ty;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">while</span>((tx=getchar())==<span class="string">' '</span>||tx==<span class="string">'\n'</span>||tx==<span class="string">'\r'</span>);</span><br><span class="line">ty=getchar();</span><br><span class="line">e[(<span class="keyword">int</span>)tx][(<span class="keyword">int</span>)ty]=<span class="number">1</span>;e[(<span class="keyword">int</span>)ty][(<span class="keyword">int</span>)tx]=<span class="number">1</span>;</span><br><span class="line">deg[(<span class="keyword">int</span>)tx]++;deg[(<span class="keyword">int</span>)ty]++;</span><br><span class="line">s=min(s,(<span class="keyword">int</span>)tx);s=min(s,(<span class="keyword">int</span>)ty);</span><br><span class="line">}</span><br><span class="line">rep(i,(<span class="keyword">int</span>)<span class="string">'A'</span>,(<span class="keyword">int</span>)<span class="string">'z'</span>)</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">if</span>(deg[i]&<span class="number">1</span>)<span class="comment">//judge degree</span></span><br><span class="line"> {</span><br><span class="line"> ++p_deg_cnt;</span><br><span class="line"> <span class="keyword">if</span>(!sxy)</span><br><span class="line"> {</span><br><span class="line"> sxy=<span class="number">1</span>;s=i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> s=min(s,i);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(p_deg_cnt>=<span class="number">3</span>)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"No Solution"</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">dfs(s,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">if</span>(solved)</span><br><span class="line"> rep(i,<span class="number">1</span>,n+<span class="number">1</span>)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%c"</span>,o_seq[i]);</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"No Solution"</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>]]></content>
<tags>
<tag> DFS </tag>
<tag> 图论 </tag>
<tag> 欧拉路径/欧拉回路 </tag>
</tags>
</entry>
<entry>
<title>旅行家的预算</title>
<link href="/2018/10/17/%E6%97%85%E8%A1%8C%E5%AE%B6%E7%9A%84%E9%A2%84%E7%AE%97/"/>
<url>/2018/10/17/%E6%97%85%E8%A1%8C%E5%AE%B6%E7%9A%84%E9%A2%84%E7%AE%97/</url>
<content type="html"><![CDATA[<p>NOIP 1999 TG <a href="https://www.luogu.org/problemnew/show/P1016" target="_blank" rel="noopener">旅行家的预算</a></p><a id="more"></a><h2>sol</h2><p>先判断No Sol的情况:存在(d[i]-d[i-1])/d2>c</p><p>然后对每个点贪心,先加恰好到的,然后把这个点加进堆里,后面可以用这个点加油</p><p>注意到油箱容量,用到这个点的时候它后面的到当前点之前的点的剩余油量增加,也就是这些点能加的油少了,维护一个这个东西,再用priority_queue按价格排序并记录序号就好了</p><p>写的时候犯了不少zz错误,忘了除以d2最甚</p><hr><h2>Code</h2><figure class="highlight cpp"><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="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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">TEMP</span>{</span></span><br><span class="line"> <span class="keyword">double</span> p;</span><br><span class="line"> <span class="keyword">int</span> num;</span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> TEMP &b)<span class="keyword">const</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">return</span> p>b.p;</span><br><span class="line"> }</span><br><span class="line">}tem;</span><br><span class="line"><span class="keyword">double</span> d1,c,d2;</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="keyword">double</span> d[<span class="number">8</span>],p[<span class="number">7</span>];</span><br><span class="line"><span class="keyword">double</span> fuel[<span class="number">7</span>],ans=<span class="number">0</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">double</span> wzy;</span><br><span class="line"> priority_queue<TEMP> sb;</span><br><span class="line"> <span class="built_in">cin</span>>>d1>>c>>d2>>p[<span class="number">0</span>]>>n;</span><br><span class="line"> d[n+<span class="number">1</span>]=d1;</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="built_in">cin</span>>>d[i]>>p[i];</span><br><span class="line"> sort(d+<span class="number">1</span>,d+n+<span class="number">1</span>);</span><br><span class="line"> rep(i,<span class="number">1</span>,n+<span class="number">1</span>)</span><br><span class="line"> <span class="keyword">if</span>(d[i]-d[i<span class="number">-1</span>]>c*d2)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"No Solution"</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"> tem.p=p[<span class="number">0</span>];tem.num=<span class="number">0</span>;</span><br><span class="line"> sb.push(tem);</span><br><span class="line"> rep(i,<span class="number">1</span>,n+<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> wzy=d[i]-d[i<span class="number">-1</span>];</span><br><span class="line"> <span class="keyword">while</span>(wzy><span class="number">0</span>)</span><br><span class="line"> {</span><br><span class="line"> tem=sb.top();</span><br><span class="line"> <span class="keyword">while</span>(fuel[tem.num]>=c)<span class="comment">//already full</span></span><br><span class="line"> {</span><br><span class="line"> sb.pop();</span><br><span class="line"> tem=sb.top();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>((c-fuel[tem.num])*d2>wzy)<span class="comment">//able to fill to arrive</span></span><br><span class="line"> {</span><br><span class="line"> rep(t,tem.num,i<span class="number">-1</span>)</span><br><span class="line"> fuel[t]+=wzy/d2;</span><br><span class="line"> ans+=tem.p*(wzy/d2);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span><span class="comment">//unable</span></span><br><span class="line"> {</span><br><span class="line"> rep(t,tem.num+<span class="number">1</span>,i<span class="number">-1</span>)</span><br><span class="line"> fuel[t]+=(c-fuel[tem.num]);</span><br><span class="line"> wzy-=(c-fuel[tem.num])*d2;</span><br><span class="line"> ans+=tem.p*(c-fuel[tem.num]);</span><br><span class="line"> sb.pop();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> tem.p=p[i];tem.num=i;</span><br><span class="line"> sb.push(tem);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%.2f"</span>,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>]]></content>
<tags>
<tag> 贪心 </tag>
<tag> 堆 </tag>
</tags>
</entry>
<entry>
<title>传纸条</title>
<link href="/2018/10/16/%E4%BC%A0%E7%BA%B8%E6%9D%A1/"/>
<url>/2018/10/16/%E4%BC%A0%E7%BA%B8%E6%9D%A1/</url>
<content type="html"><![CDATA[<p>NOIP2008 T3 <a href="https://www.luogu.org/problemnew/show/P1006" target="_blank" rel="noopener">传纸条</a></p><a id="more"></a><h2>sol</h2><p>显然可以dp,推了二十分钟感觉传纸条的路径大概是让一个多边形的轮廓覆盖的权值最大。。。</p><p>其实就是找不重叠的两条路能覆盖的最大权值这意思吧。。。</p><p>单条路推进想了好久怎么不让路径重叠。。。然后回家发现直接两条路一并推进就会很好做。。。</p><p>因为显然两条路在推进相同步数的条件下它们的横、纵坐标肯定都不相等,否则就是交叉或者重叠的情况了,基于这点来做就可以了</p><p>递推不会写。。。于是写了记忆化搜索。。。</p><hr><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> dp[<span class="number">51</span>][<span class="number">51</span>][<span class="number">51</span>][<span class="number">51</span>],mapp[<span class="number">51</span>][<span class="number">51</span>];</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">bool</span> vis[<span class="number">51</span>][<span class="number">51</span>];</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> i,<span class="keyword">int</span> j,<span class="keyword">int</span> k,<span class="keyword">int</span> w)</span></span></span><br><span class="line"><span class="function"></span>{ </span><br><span class="line"> <span class="keyword">if</span>(dp[i][j][k][w])</span><br><span class="line"> <span class="keyword">return</span> dp[i][j][k][w];</span><br><span class="line"> <span class="keyword">if</span>(i+<span class="number">1</span><=n&&k+<span class="number">1</span><=n)</span><br><span class="line"> dp[i][j][k][w]=max(dp[i][j][k][w],dfs(i+<span class="number">1</span>,j,k+<span class="number">1</span>,w));</span><br><span class="line"> <span class="keyword">if</span>(i+<span class="number">1</span><=n&&w+<span class="number">1</span><=m)</span><br><span class="line"> dp[i][j][k][w]=max(dp[i][j][k][w],dfs(i+<span class="number">1</span>,j,k,w+<span class="number">1</span>));</span><br><span class="line"> <span class="keyword">if</span>(j+<span class="number">1</span><w&&k+<span class="number">1</span><=n)</span><br><span class="line"> dp[i][j][k][w]=max(dp[i][j][k][w],dfs(i,j+<span class="number">1</span>,k+<span class="number">1</span>,w));</span><br><span class="line"> <span class="keyword">if</span>(j+<span class="number">1</span><=w&&w+<span class="number">1</span><=m)</span><br><span class="line"> dp[i][j][k][w]=max(dp[i][j][k][w],dfs(i,j+<span class="number">1</span>,k,w+<span class="number">1</span>));</span><br><span class="line"> dp[i][j][k][w]+=(mapp[i][j]+mapp[k][w]);</span><br><span class="line"> <span class="keyword">return</span> dp[i][j][k][w];</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"</span>,&n,&m);</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,<span class="number">1</span>,m)</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&mapp[i][j]);</span><br><span class="line"> <span class="built_in">cout</span><<dfs(<span class="number">2</span>,<span class="number">1</span>,<span class="number">1</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></pre></td></tr></table></figure><hr><h2>others</h2><p>从这篇开始有点不想复制题面了 。。。看心情吧</p>]]></content>
<tags>
<tag> 动态规划 </tag>
<tag> 记忆化搜索 </tag>
</tags>
</entry>
<entry>
<title>luogu P1119 灾后重建</title>
<link href="/2018/10/16/%E7%81%BE%E5%90%8E%E9%87%8D%E5%BB%BA/"/>
<url>/2018/10/16/%E7%81%BE%E5%90%8E%E9%87%8D%E5%BB%BA/</url>
<content type="html"><![CDATA[<p>做了这题才发现自己对floyd有多不了解。。。</p><a id="more"></a><p><div class="am-u-md-8 lg-right" name="problemleft"></div></p><p><div class="lg-content-left"></div></p><p><div class="lg-article am-g"></div></p><h2>题目背景</h2><br><p>$B$地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响。但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车。换句话说,只有连接着两个重建完成的村庄的公路才能通车,只能到达重建完成的村庄。</p><br><h2>题目描述</h2><br><p>给出$B$地区的村庄数$N$,村庄编号从$0$到$N-1$,和所有$M$条公路的长度,公路是双向的。并给出第$i$个村庄重建完成的时间$t_i$,你可以认为是同时开始重建并在第$t_i$天重建完成,并且在当天即可通车。若$t_i$为$0$则说明地震未对此地区造成损坏,一开始就可以通车。之后有$Q$个询问$(x, y, t)$,对于每个询问你要回答在第$t$天,从村庄$x$到村庄y的最短路径长度为多少。如果无法找到从$x$村庄到$y$村庄的路径,经过若干个已重建完成的村庄,或者村庄$x$或村庄$y$在第t天仍未重建完成 ,则需要返回$-1$。</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>第一行包含两个正整数$N,M$,表示了村庄的数目与公路的数量。</p><br><p>第二行包含$N$个非负整数$t_0, t_1,…, t_{N-1}$,表示了每个村庄重建完成的时间,数据保证了$t_0 ≤ t_1 ≤ … ≤ t_{N-1}$。</p><br><p>接下来$M$行,每行$3$个非负整数$i, j, w$,$w$为不超过$10000$的正整数,表示了有一条连接村庄$i$与村庄$j$的道路,长度为$w$,保证$i≠j$,且对于任意一对村庄只会存在一条道路。</p><br><p>接下来一行也就是$M+3$行包含一个正整数$Q$,表示$Q$个询问。</p><br><p>接下来$Q$行,每行$3$个非负整数$x, y, t$,询问在第$t$天,从村庄$x$到村庄$y$的最短路径长度为多少,数据保证了$t$是不下降的。</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>共$Q$行,对每一个询问$(x, y, t)$输出对应的答案,即在第$t$天,从村庄$x$到村庄$y$的最短路径长度为多少。如果在第t天无法找到从$x$村庄到$y$村庄的路径,经过若干个已重建完成的村庄,或者村庄x或村庄$y$在第$t$天仍未修复完成,则输出$-1$。</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>4 5<br>1 2 3 4<br>0 2 1<br>2 3 1<br>3 1 2<br>2 1 4<br>0 3 5<br>4<br>2 0 2<br>0 1 2<br>0 1 3<br>0 1 4</pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>-1<br>-1<br>5<br>4</pre><br></div><br></div><br><h2>说明</h2><br><p>对于$30\%$的数据,有$N≤50$;</p><br><p>对于$30\%$的数据,有$t_i= 0$,其中有$20\%$的数据有$t_i = 0$且$N>50$;</p><br><p>对于$50\%$的数据,有$Q≤100$;</p><br><p>对于$100\%$的数据,有$N≤200$,$M≤N \times (N-1)/2$,$Q≤50000$,所有输入数据涉及整数均不超过$100000$。</p><br><br><div class="lg-article-sub am-g" id="sub"></div><br><br><br>—<br><br><h2>sol</h2><p>这题很大程度上加深了我对Floyd的理解</p><p>先想到的是每次对每个查询点跑一边spfa,只走时间可行的边,但是算了下复杂度不太对</p><p>然后就想多源最短路,记录一个当前的最外层循环跑到了哪里,从前往后时间上慢慢递推松弛</p><p>总的思想对了,但是中间错了,我加边的时候以为双向边不太对,然后就按单向边加,从时间晚的连向时间早的,并不对,而且最外层循环到达当前时间的点之间松弛并不会用到当前时间的点</p><p>因为原生的floyd是f[k]/[i]/[j]表示经过前k个点i到j的最小距离,然后写的时候一般就把第一维去掉了,</p><p>然后改过来就对了,但是并没想到是那里错了,看了题解才知道,一直在考虑i,j的问题</p><hr><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;</span><br><span class="line"> a+=(c-<span class="string">'0'</span>);</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> n,m,t[<span class="number">201</span>],q;</span><br><span class="line"><span class="keyword">int</span> dis[<span class="number">201</span>][<span class="number">201</span>];</span><br><span class="line"><span class="keyword">bool</span> used[<span class="number">201</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> cnt=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> tx,ty,tz;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> <span class="built_in">memset</span>(dis,<span class="number">0x3f3f3f3f</span>,<span class="keyword">sizeof</span> dis);</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> dis[i][i]=<span class="number">0</span>;</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> t[i]=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> dis[ty+<span class="number">1</span>][tx+<span class="number">1</span>]=tz;</span><br><span class="line"> dis[tx+<span class="number">1</span>][ty+<span class="number">1</span>]=tz;</span><br><span class="line"> }</span><br><span class="line"> q=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,q)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> <span class="keyword">while</span>(t[cnt]<=tz&&cnt<=n)</span><br><span class="line"> {</span><br><span class="line"> rep(u,<span class="number">1</span>,n)</span><br><span class="line"> rep(v,<span class="number">1</span>,n)</span><br><span class="line"> dis[u][v]=min(dis[u][v],dis[u][cnt]+dis[cnt][v]);</span><br><span class="line"> ++cnt;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(t[tx+<span class="number">1</span>]>tz||t[ty+<span class="number">1</span>]>tz||dis[tx+<span class="number">1</span>][ty+<span class="number">1</span>]==<span class="number">0x3f3f3f3f</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"-1\n"</span>);</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,dis[tx+<span class="number">1</span>][ty+<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></pre></td></tr></table></figure><hr><h2>参考</h2><p><a href="http://www.360doc.com/userhome/14357424" target="_blank" rel="noopener">@Rainboy913</a>:</p><figure class="highlight plain"><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">为了加深对floyd算法的认识,特地搜了一下floyd算法的证明。原来floyd算法的本质是一个动态规划的过程。</span><br><span class="line">状态转移方程:</span><br><span class="line"></span><br><span class="line">f[k][i][j]=min(f[k-1][i][j],f[k-1][i][k]+f[k-1][k][j])</span><br><span class="line"></span><br><span class="line">f[k][i][j]表示只经过前k个点(包括k),从i到j的最小值。当k从1到n时,就是从i到j的最小值。我们熟悉的用二维数组的写法实际上是对空间的一种压缩。</span><br><span class="line"></span><br><span class="line">解释一下:</span><br><span class="line"></span><br><span class="line">计算只经过前k个点,从i到j的最小值时,有两种情况需要考虑:经过第k个点和不经过第k个点。经过第k个点则距离应是从i到k的最小值和从k到j的最小值,两个最小值的路径都必须只经过前k-1个点(为什么是k-1而不是k,事实上他们两数值相同,因为起点和终点已经有第k个点,只是在dp的过程中先产生k-1,f[k][i][k]和f[k][k][j]有可能比f[k][i][j]的值晚计算出,就不能在计算f[k][i][j]时用到这两个值)。不经过k的点则距离与只经过前k-1个点时一样。</span><br></pre></td></tr></table></figure><p> <a href="https://www.luogu.org/space/show?uid=48036" target="_blank" rel="noopener">@秋日私语</a>:</p><figure class="highlight plain"><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">这道题加深了我对Floyed算法的理解。</span><br><span class="line"></span><br><span class="line">首先谈谈Floyed算法:Floyd算法的本质是动态规划,其转移方程 为:f[k][i][j] = min( f[k-1][i][j], f([k-1][i] [k])+f[k-1][k][j] )。</span><br><span class="line"></span><br><span class="line">f[k][i][j]表示路径除开起点i与终点j,只经过前k个点中的某些 点,从i到j的最小值。计算这个值只需要考虑两种情况:最短路经 过k,和最短路不经过k(那么就经过前k-1个点中的某些点)。由于 k要从k-1转移而来,自然k为最外层的循环。而经过滚动(类似于背 包问题)后,就变成了我们熟悉的f[i][j]=min(f[i][j],f[i] [k]+f[k][j])了。</span><br></pre></td></tr></table></figure><p><a href="https://www.luogu.org/space/show?uid=17667" target="_blank" rel="noopener">@feilongz</a>:</p><figure class="highlight plain"><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></pre></td><td class="code"><pre><span class="line">一开始想到许多玄学的思路。</span><br><span class="line"></span><br><span class="line">最早的思路——30数据可以不考虑修理,一遍floyd就行。</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><br><span class="line">还有个更严肃的问题是,并不是边修理的时间,而是点。</span><br><span class="line"></span><br><span class="line">最后还是靠理解floyd。</span><br><span class="line"></span><br><span class="line">首先N很小,一般来说N小的时候我都随便跑循环。可以想到floyd就算稍微加点循环也不会有问题。</span><br><span class="line"></span><br><span class="line">考虑一下floyd的实现,其实就是DP。枚举一个断点k,用k去更新其他最短路,最后得出最优解。这个k就是关键——正常的算法k只是单纯枚举,这次只要稍加考虑就行了。考虑k点的时间是否符合当前要求。</span><br><span class="line"></span><br><span class="line">显然时间和要求需要排序,不过出题人和善的都排好了。</span><br><span class="line"></span><br><span class="line">于是我枚举每个问题,在问题里枚举k,看k点重建时间是否小于当前问题,是的话就用k去更新其他点。这里要注意任何一个k都只更新一遍,我用一个vis数组记录,这样才能保证效率不会退化为(n^3q),因为只执行一遍floyd,剩下的问题只要用处理好的图即可,可知效率应当是(n^3+q),这完全可以接受了。</span><br><span class="line"></span><br><span class="line">如果不记录,我T了7个点。</span><br><span class="line"></span><br><span class="line">不过这个算法就算A了效率还是奇低。。</span><br><span class="line"></span><br><span class="line">P.S.:更新的时候不要下意识的把i,j的时间考虑上,完全没关系。输出-1 -1 5 5的可能是这个问题。</span><br><span class="line"></span><br><span class="line">最后上代码。</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> 最短路 </tag>
<tag> Floyd </tag>
</tags>
</entry>
<entry>
<title>双栈排序</title>
<link href="/2018/10/15/%E5%8F%8C%E6%A0%88%E6%8E%92%E5%BA%8F/"/>
<url>/2018/10/15/%E5%8F%8C%E6%A0%88%E6%8E%92%E5%BA%8F/</url>
<content type="html"><![CDATA[<p> NOIP 2008 TG </p><a id="more"></a><p><div class="am-u-md-8 lg-right" name="problemleft"></div></p><p><div class="lg-content-left"></div></p><p><div class="lg-article am-g"></div></p><p></p><h2>题目描述</h2><p></p><p></p><p><code>Tom</code>最近在研究一个有趣的排序问题。如图所示,通过$2$个栈$S_1$和$S_2$,<code>Tom</code>希望借助以下$4$种操作实现将输入序列升序排序。</p><p></p><p></p><p><img src="https://cdn.luogu.org/upload/pic/51.png" alt=""> </p><p></p><p></p><p>操作$a$</p><p></p><p></p><p>如果输入序列不为空,将第一个元素压入栈$S_1$</p><p></p><p></p><p>操作$b$</p><p></p><p></p><p>如果栈$S_1$不为空,将$S_1$栈顶元素弹出至输出序列</p><p></p><p></p><p>操作$c$</p><p></p><p></p><p>如果输入序列不为空,将第一个元素压入栈$S_2$</p><p></p><p></p><p>操作$d$</p><p></p><p></p><p>如果栈$S_2$不为空,将$S_2$栈顶元素弹出至输出序列</p><p></p><p></p><p>如果一个$1-n$的排列P可以通过一系列操作使得输出序列为$1,2,…,(n-1),n$,<code>Tom</code>就称$P$是一个“可双栈排序排列”。例如$(1,3,2,4)$就是一个“可双栈排序序列”,而$(2,3,4,1)$不是。下图描述了一个将$(1,3,2,4)$排序的操作序列:$<a,c,c,b,a,d,d,b>$</p><p></p><p></p><p><img src="https://cdn.luogu.org/upload/pic/52.png" alt=""> </p><p></p><p></p><p>当然,这样的操作序列有可能有几个,对于上例$(1,3,2,4)$,$<a,c,c,b,a,d,d,b>$是另外一个可行的操作序列。<code>Tom</code>希望知道其中字典序最小的操作序列是什么。</p><p></p><p></p><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><p></p><p></p><p></p><p>第一行是一个整数$n$。</p><p></p><p></p><p>第二行有$n$个用空格隔开的正整数,构成一个$1-n$的排列。</p><p></p><br><strong>输出格式:</strong><br><p></p><p></p><p></p><p>共一行,如果输入的排列不是“可双栈排序排列”,输出数字$0$;否则输出字典序最小的操作序列,每两个操作之间用空格隔开,行尾没有空格。</p><p></p><p></p><p></p><h2>输入输出样例</h2><p></p><p><div class="am-g"></div></p><p><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>4<br>1 3 2 4</pre><br></p><p><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>a b a a b b a b</pre><br><br></p><p><div class="am-g"></div></p><p><div class="am-u-md-6 copy-region"><br><strong>输入样例#2:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>4<br>2 3 4 1</pre><br></p><p><div class="am-u-md-6 copy-region"><br><strong>输出样例#2:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>0</pre><br><br></p><p><div class="am-g"></div></p><p><div class="am-u-md-6 copy-region"><br><strong>输入样例#3:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>3<br>2 3 1</pre><br></p><p><div class="am-u-md-6 copy-region"><br><strong>输出样例#3:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>a c a b b d</pre><br><br></p><p></p><h2>说明</h2><p></p><p></p><p>$30\%$的数据满足: $n \le 10$</p><p></p><p></p><p>$50\%$的数据满足:$ n \le 50$</p><p></p><p></p><p>$100\%$的数据满足: $n \le 1000$</p><br><p></p><p><div class="lg-article-sub am-g" id="sub"></div><br></p><h2 id=""><a href="#" class="headerlink" title=""></a></h2><h1>sol</h1><p>双栈,先考虑一比较简单的问题,化简问题,双->单</p><p>单栈用来干什么?逆序对输出的缓存空间。</p><p>单栈排序什么时候无法进行?</p><p>错误结论:</p><p>存在a[i]<a[j]<a[k],j<i<k时,不能单栈排序</p><p>得出原因:</p><p>3 2 4</p><p>起决定性作用的并不是3 2 4,而是1的相对位置</p><p>譬如1 3 2 4,3 1 2 4,3 2 1 4就可以,3 2 4 1就不可以</p><p>再看1的相对位置,更简化的一组数据即为</p><p>2 3 1</p><p>可见上面那组其决定性的是2 4 1</p><p>可得出正确结论:</p><p>存在i<j<k ,="" a[j]="">a[i]>a[k]时不能进行单栈排序</k></p><p>有峰?1 3 2 4也有</p><p>单峰?并不是</p><p>不知道怎么定义这个东西,大概就是一个峰,左降慢右降快(好像不是一直)</p><p>要验证不能进行单栈排序,显然可以暴力,O(n^3)</p><p>可以预处理前后缀最小值,然后枚举j来O(n)做吧?貌似并不能,与a[i]具体取值也有关,不一定min</p><p>但是可以O(n^2),a[k]最小,就从k入手好了</p><p>维护后缀最小值f[i]=min{a[i],a[i+1],…,a[n]}</p><p>然后条件转成了a[j]>a[i]>f[j+1],应该是O(n^2)了</p><p>然后再一想,我们有两个栈!!!!</p><p>那这些不能放在一个栈里的有部分就可以搞掉了,但并不能解决掉所有</p><p>考虑 2 3 1</p><p>我们可以把2放进另一个栈,也可以把3放进另一个栈,但显然把3放进另一个字典序会更小,</p><p>也就是说,出现单栈解决不了的之后我们就把那个“峰”放进另一个栈</p><p>这就像是二分图了,分成了两部分,对吧</p><p>那我们在考虑什么时候两个栈都解决不了:放入另一个栈的之间会冲突</p><p>那就完全是个二分图的判定了</p><p>每出现一个那样的“峰”,我们就把两个“脊”和峰之间连一条边,最后判定一下是不是二分图,不是就输出0,是就模拟输出过程,就ok了,第一个染的点当然是放进第一个栈,没边没染的也是</p><p>(其实没有必要给右边的“脊”加边,因为f[i]是后缀最小值,所以无法构造出一个能成为峰的右“脊”(可以用反证法),所以没有必要)</p><p>emmm不是很懂为什么输出的时候一弹就要弹到不能再弹,大概跟前面的某些性质有关?</p><p>所以前面思考单栈当然是有用的,单栈的n^3显然对1000是过不了的,而n^2就可以了</p><p>然后其实也可以考虑用并查集,二分图和并查集好像还是比较相似的?</p><p>那就是把峰弄到一个集合,脊弄到另一个?</p><p>但是好像二分图染色比较更好弄?</p><p>不过貌似并查集可以在线边处理边判断是否有解?</p><p>这个题乍一看真的不像图论题。。。感觉跟图论一点关系都没有。。。然后贪心模拟30分orzco’l</p><p>大概相当于搜索分(O(4^n))?</p><p><a href="https://www.luogu.org/space/show?uid=21596" target="_blank" rel="noopener">@Forever丶CIL</a>:</p><figure class="highlight plain"><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></pre></td><td class="code"><pre><span class="line">一看这道题,感觉好难啊,感觉这道题爆捜都写不出来,</span><br><span class="line"></span><br><span class="line">但是看他是“双栈”,我就联想到了P1654;一个思路</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><br><span class="line">而它的能力有限,对于->2 3 1这样的数据是无能为力的</span><br><span class="line"></span><br><span class="line">我们看看像“2 3 1”,这种数据有什么性质</span><br><span class="line"></span><br><span class="line">->给2 3 1分别记为a1 a2 a3我们发现a3<a1<a2;再找其他几组满足</span><br><span class="line"></span><br><span class="line">a3<a1<a2的数据,我们会发现都不可以用单栈排序;</span><br><span class="line"></span><br><span class="line">所以我们就发现了本题的重要性质之一,这种情况下,我们</span><br><span class="line"></span><br><span class="line">就可以将a1压进1号栈,a2压进2号栈,a3压进1号栈,再分别</span><br><span class="line"></span><br><span class="line">弹出a3,a1,a2,就完成了排序。</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><br><span class="line">我们考虑当a4<a1<a2<a3时,是不是双栈就GG了?</span><br><span class="line"></span><br><span class="line">所以说,我们要判断可不可以用双栈排序,本质上就是要判断数列中存不存在</span><br><span class="line"></span><br><span class="line">a4<a1<a2<a3。当然我们可以暴力判O(n^4) 2333,也可以优化一下先预处理出</span><br><span class="line"></span><br><span class="line">后缀最小值,然后O(n^3)判断,当然都会TLE。。或许其他大佬有更快的做法</span><br><span class="line"></span><br><span class="line">我这里写的是二分图染色的做法,具体解释在代码中。</span><br></pre></td></tr></table></figure><hr><h1>Code</h1><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</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"><cstring></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="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;<span class="keyword">bool</span> t=<span class="number">0</span>;<span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">if</span>(c==<span class="string">'-'</span>){t=<span class="number">1</span>;c=getchar();}</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>){a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"><span class="keyword">int</span> to,next;</span><br><span class="line">}e[<span class="number">100001</span>];</span><br><span class="line"><span class="keyword">int</span> n,a[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">int</span> f[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">int</span> s1[<span class="number">1001</span>],s1_top,s2[<span class="number">1001</span>],s2_top;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">1001</span>],cnt;</span><br><span class="line"><span class="keyword">short</span> <span class="keyword">int</span> col[<span class="number">1001</span>],tt_col[<span class="number">2</span>]={<span class="number">1</span>,<span class="number">0</span>};</span><br><span class="line"><span class="keyword">bool</span> noSol=<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">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">e[++cnt].next=last[from];</span><br><span class="line">last[from]=cnt;</span><br><span class="line">e[cnt].to=to;</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> now,<span class="keyword">int</span> t_col)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">col[now]=t_col;</span><br><span class="line"><span class="keyword">int</span> temp=last[now];</span><br><span class="line"><span class="keyword">while</span>(temp)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(col[e[temp].to]<<span class="number">0</span>)</span><br><span class="line"> dfs(e[temp].to,tt_col[t_col]);</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(col[e[temp].to]!=tt_col[t_col])</span><br><span class="line">{</span><br><span class="line">noSol=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(noSol)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">temp=e[temp].next;</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">n=read();</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> col[i]=<span class="number">-1</span>;</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> a[i]=read();</span><br><span class="line">f[n]=a[n];</span><br><span class="line">per(i,n<span class="number">-1</span>,<span class="number">1</span>)</span><br><span class="line"> f[i]=min(f[i+<span class="number">1</span>],a[i]);</span><br><span class="line">rep(i,<span class="number">1</span>,n<span class="number">-2</span>)</span><br><span class="line"> rep(j,i+<span class="number">1</span>,n<span class="number">-1</span>)</span><br><span class="line"> <span class="keyword">if</span>(a[j]>a[i]&&a[i]>f[j+<span class="number">1</span>])</span><br><span class="line"> {</span><br><span class="line"> add(i,j);add(j,i);</span><br><span class="line"> }</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(col[i]<<span class="number">0</span>)</span><br><span class="line"> dfs(i,<span class="number">0</span>);</span><br><span class="line"><span class="keyword">if</span>(noSol)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"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><br><span class="line">cnt=<span class="number">1</span>;</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(col[i])</span><br><span class="line">{</span><br><span class="line">s2[++s2_top]=a[i];</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"c "</span>);</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">s1[++s1_top]=a[i];</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"a "</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>((s1[s1_top]==cnt&&s1_top)||(s2[s2_top]==cnt&&s2_top))</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(s1[s1_top]==cnt)</span><br><span class="line">{</span><br><span class="line">--s1_top;</span><br><span class="line">++cnt;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"b "</span>);</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">--s2_top;</span><br><span class="line">++cnt;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"d "</span>);</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> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> DFS </tag>
<tag> 图论 </tag>
<tag> 并查集 </tag>
<tag> 二分图 </tag>
</tags>
</entry>
<entry>
<title>树网的核</title>
<link href="/2018/10/15/%E6%A0%91%E7%BD%91%E7%9A%84%E6%A0%B8/"/>
<url>/2018/10/15/%E6%A0%91%E7%BD%91%E7%9A%84%E6%A0%B8/</url>
<content type="html"><![CDATA[<p>NOIP 2007 TG T4</p><a id="more"></a><p><div class="am-u-md-8 lg-right" name="problemleft"></div></p><p><div class="lg-content-left"></div></p><p><div class="lg-article am-g"></div></p><p></p><h2>题目描述</h2><p></p><p></p><p>设$T=(V,E,W)$是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称$T$为树网(<code>treebetwork</code>),其中$V$,$E$分别表示结点与边的集合,$W$表示各边长度的集合,并设$T$有$n$个结点。</p><p></p><p></p><p>路径:树网中任何两结点$a$,$b$都存在唯一的一条简单路径,用$d(a, b)$表示以$a, b$为端点的路径的长度,它是该路径上各边长度之和。我们称$d(a, b)$为$a, b$两结点间的距离。</p><p></p><p></p><p>$D(v, P)=\min{d(v, u)}$, $u$为路径$P$上的结点。</p><p></p><p></p><p>树网的直径:树网中最长的路径成为树网的直径。对于给定的树网$T$,直径不一定是唯一的,但可以证明:各直径的中点(不一定恰好是某个结点,可能在某条边的内部)是唯一的,我们称该点为树网的中心。</p><p></p><p></p><p>偏心距$\mathrm{ECC}(F)$:树网T中距路径F最远的结点到路径$F$的距离,即</p><p></p><p></p><p>$\mathrm{ECC}(F)=\max{d(v, F),v \in V}$</p><p></p><p></p><p>任务:对于给定的树网$T=(V, E, W)$和非负整数$s$,求一个路径$F$,他是某直径上的一段路径(该路径两端均为树网中的结点),其长度不超过$s$(可以等于s),使偏心距$ECC(F)$最小。我们称这个路径为树网$T=(V, E, W)$的核(<code>Core</code>)。必要时,$F$可以退化为某个结点。一般来说,在上述定义下,核不一定只有一个,但最小偏心距是唯一的。</p><p></p><p></p><p>下面的图给出了树网的一个实例。图中,$A-B$与$A-C$是两条直径,长度均为$20$。点$W$是树网的中心,$EF$边的长度为$5$。如果指定$s=11$,则树网的核为路径<code>DEFG</code>(也可以取为路径<code>DEF</code>),偏心距为$8$。如果指定$s=0$(或$s=1$、$s=2$),则树网的核为结点$F$,偏心距为$12$。</p><p></p><p></p><p><img src="https://cdn.luogu.org/upload/pic/20270.png" alt=""></p><p></p><p></p><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><p></p><p></p><p></p><p>共$n$行。</p><p></p><p></p><p>第$1$行,两个正整数$n$和$s$,中间用一个空格隔开。其中$n$为树网结点的个数,$s$为树网的核的长度的上界。设结点编号以此为$1,2,…,n$。</p><p></p><p></p><p>从第$2$行到第$n$行,每行给出$3$个用空格隔开的正整数,依次表示每一条边的两个端点编号和长度。例如,“$2 4 7$”表示连接结点$2$与$4$的边的长度为$7$。</p><p></p><br><strong>输出格式:</strong><br><p></p><p></p><p></p><p>一个非负整数,为指定意义下的最小偏心距。</p><p></p><p></p><p></p><h2>输入输出样例</h2><p></p><p><div class="am-g"></div></p><p><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>5 2<br>1 2 5<br>2 3 2<br>2 4 4<br>2 5 3</pre></p><p><br></p><p><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>5<br></pre><br><br></p><p><div class="am-g"></div></p><p><div class="am-u-md-6 copy-region"><br><strong>输入样例#2:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>8 6<br>1 3 2<br>2 3 2<br>3 4 6<br>4 5 3<br>4 6 4<br>4 7 2<br>7 8 3<br></pre><br></p><p><div class="am-u-md-6 copy-region"><br><strong>输出样例#2:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><p><pre>5</pre><br><br></p><p></p><h2>说明</h2><p></p><p></p><p>$40\%$的数据满足:$5 \le n \le 15$<br><br>$70\%$的数据满足:$5 \le n \le 80$<br><br>$100\%$的数据满足:$5 \le n \le 300,0 \le s \le 1000$。边长度为不超过$1000$的正整数</p><p></p><h2 id="NOIP-2007-提高第四题"><a href="#NOIP-2007-提高第四题" class="headerlink" title="NOIP 2007 提高第四题"></a><p>NOIP 2007 提高第四题</p></h2><h1>sol</h1><p>看第一眼并想不出什么做法,感觉直径会有多条,然后就感觉很迷</p><p>然后就有种感觉,貌似任意挑出一条直径答案都是一样的?</p><p>然后抱着大胆猜想不用证明的心态,过了。。。</p><p>我的做法就是先跑一遍floyd,再两遍dfs找直径,然后再dfs找直径上的点,最后再在直径上找核,差不多就是枚举核的左右端点,可以尺取,因为一条路径的子路径一定不会比这条路径总的更优,也算是一个比较233的优化?</p><p>但是尺取并不会写,所以写了个类似的东西来代替</p><p>然后就在找到的可能为核的路径上一个一个去搞到每个点的距离,其实就是取个min</p><p>复杂度大概是O(n^3),又或许是O(n^4)?看了下数据范围能过</p><p>但是好像可以O(n)来做。。。</p><p>思想是比较简单的,但是代码却比较麻烦。。。</p><p>犯的一些sb错误:</p><p> ++j和算temp顺序反了</p><p> 直接用的直径上的点的相对编号。。。</p><p> 忘了给temp加上。。。</p><p>这个数据范围基本上是个模拟题,当练代码能力好了。。。</p><hr><h1>Code</h1><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;</span><br><span class="line"> a+=(c-<span class="string">'0'</span>);</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[<span class="number">300</span>*<span class="number">2</span>];</span><br><span class="line"><span class="keyword">int</span> dis[<span class="number">301</span>][<span class="number">301</span>],n,s;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">301</span>],cnt;</span><br><span class="line"><span class="keyword">int</span> sb,zc=<span class="number">-1</span>,sb2,zc2=<span class="number">-1</span>;</span><br><span class="line"><span class="keyword">bool</span> vis[<span class="number">301</span>],solved;</span><br><span class="line"><span class="keyword">int</span> zj[<span class="number">301</span>],zcnt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &w)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=w;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">floyd</span><span class="params">()</span><span class="comment">//dis</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> rep(k,<span class="number">1</span>,n)</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,<span class="number">1</span>,n)</span><br><span class="line"> dis[i][j]=min(dis[i][k]+dis[k][j],dis[i][j]);</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> now,<span class="keyword">int</span> len)</span><span class="comment">//zhi jing</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(len>zc)</span><br><span class="line"> {</span><br><span class="line"> sb=now;</span><br><span class="line"> zc=len;</span><br><span class="line"> }</span><br><span class="line"> vis[now]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> temp=last[now];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[e[temp].to])</span><br><span class="line"> dfs(e[temp].to,len+e[temp].v);</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> vis[now]=<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">dfs2</span><span class="params">(<span class="keyword">int</span> now,<span class="keyword">int</span> len)</span><span class="comment">//zhi jing</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(len>zc2)</span><br><span class="line"> {</span><br><span class="line"> sb2=now;</span><br><span class="line"> zc2=len;</span><br><span class="line"> }</span><br><span class="line"> vis[now]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> temp=last[now];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[e[temp].to])</span><br><span class="line"> dfs2(e[temp].to,len+e[temp].v);</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> vis[now]=<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_path</span><span class="params">(<span class="keyword">int</span> now)</span><span class="comment">//zhi_jing_path</span></span></span><br><span class="line"><span class="function"></span>{ </span><br><span class="line"> zj[++zcnt]=now;</span><br><span class="line"> vis[now]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(now==sb2)</span><br><span class="line"> {</span><br><span class="line"> solved=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> temp=last[now];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!vis[e[temp].to])</span><br><span class="line"> dfs_path(e[temp].to);</span><br><span class="line"> <span class="keyword">if</span>(solved)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> --zcnt;</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> diss[<span class="number">301</span>];</span><br><span class="line"> <span class="keyword">int</span> sxy,ans=<span class="number">2147483647</span>,temp;</span><br><span class="line"> <span class="built_in">memset</span>(dis,<span class="number">0x3f3f3f3f</span>,<span class="keyword">sizeof</span> dis);</span><br><span class="line"> <span class="keyword">int</span> tx,ty,tz;</span><br><span class="line"> n=read();s=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,n)</span><br><span class="line"> dis[i][i]=<span class="number">0</span>;</span><br><span class="line"> rep(i,<span class="number">1</span>,n<span class="number">-1</span>)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> dis[tx][ty]=tz;</span><br><span class="line"> dis[ty][tx]=tz;</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> add(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> floyd();</span><br><span class="line"> dfs(<span class="number">1</span>,<span class="number">0</span>);</span><br><span class="line"> dfs2(sb,<span class="number">0</span>);</span><br><span class="line"> dfs_path(sb);</span><br><span class="line"> <span class="keyword">int</span> j;</span><br><span class="line"> <span class="built_in">memset</span>(vis,<span class="number">0</span>,<span class="keyword">sizeof</span> vis);</span><br><span class="line"> rep(i,<span class="number">1</span>,zcnt)</span><br><span class="line"> {</span><br><span class="line"> temp=<span class="number">0</span>;sxy=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(vis[zcnt])</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> j=i;</span><br><span class="line"> <span class="keyword">while</span>(j<zcnt&&temp+dis[zj[j]][zj[j+<span class="number">1</span>]]<=s)</span><br><span class="line"> {</span><br><span class="line"> temp+=dis[zj[j]][zj[j+<span class="number">1</span>]];</span><br><span class="line"> ++j;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(vis[j])</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> vis[j]=<span class="number">1</span>;</span><br><span class="line"> <span class="built_in">memset</span>(diss,<span class="number">0x3f3f3f3f</span>,<span class="keyword">sizeof</span> diss);</span><br><span class="line"> rep(w,i,j)</span><br><span class="line"> rep(k,<span class="number">1</span>,n)</span><br><span class="line"> diss[k]=min(diss[k],dis[zj[w]][k]);</span><br><span class="line"> rep(k,<span class="number">1</span>,n)</span><br><span class="line"> sxy=max(sxy,diss[k]);</span><br><span class="line"> ans=min(ans,sxy);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<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>]]></content>
<tags>
<tag> DFS </tag>
<tag> 图论 </tag>
<tag> 模拟 </tag>
<tag> Floyd </tag>
</tags>
</entry>
<entry>
<title>【USACO14FEB】路障Roadblock / luogu P1186 玛丽卡</title>
<link href="/2018/10/12/%5BUSACO14FEB%5D%E8%B7%AF%E9%9A%9CRoadblock,luogu%20P1186%20%E7%8E%9B%E4%B8%BD%E5%8D%A1/"/>
<url>/2018/10/12/%5BUSACO14FEB%5D%E8%B7%AF%E9%9A%9CRoadblock,luogu%20P1186%20%E7%8E%9B%E4%B8%BD%E5%8D%A1/</url>
<content type="html"><![CDATA[<p>两道几乎相同的题目</p><a id="more"></a><h2>[USACO14FEB]路障Roadblock </h2><p><div class="am-u-md-8 lg-right" name="problemleft"></div></p><p><div class="lg-content-left"></div></p><p><div class="lg-article am-g"></div></p><h2>题目描述</h2><br><p>每天早晨,FJ从家中穿过农场走到牛棚。农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度。FJ 的房子在 1 号田,牛棚在 N 号田。没有两块田被多条道路连接,以适当的路径顺序总是能在农场任意一对田间行走。当FJ从一块田走到另一块时,总是以总路长最短的道路顺序来走。</p><br><p>FJ 的牛呢,总是不安好心,决定干扰他每天早晨的计划。它们在 M 条路的某一条上安放一叠稻草堆,使这条路的长度加倍。牛希望选择一条路干扰使得FJ 从家到牛棚的路长增加最多。它们请你设计并告诉它们最大增量是多少。</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>第 1 行:两个整数 N, M。</p><br><p>第 2 到 M+1 行:第 i+1 行包含三个整数 A_i, B_i, L_i,A_i 和 B_i 表示道路 i 连接的田的编号,L_i 表示路长。</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>第 1 行:一个整数,表示通过使某条路加倍而得到的最大增量。</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>5 7<br>2 1 5<br>1 3 1<br>3 2 8<br>3 5 7<br>3 4 3<br>2 4 7<br>4 5 2</pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>2</pre><br></div><br></div><br><h2>说明</h2><br><p>【样例说明】</p><br><p>若使 3 和 4 之间的道路长加倍,最短路将由 1-3-4-5 变为 1-3-5。</p><br><p>【数据规模和约定】</p><br><p>对于 30%的数据,N <= 70,M <= 1,500。</p><br><p>对于 100%的数据,1 <= N <= 100,1 <= M <= 5,000,1 <= L_i <= 1,000,000。</p><br><br><div class="lg-article-sub am-g" id="sub"></div><br><br><br>—<br><br><h2>sol</h2><p>第一次求最短路记录下到n的最短路的总长和前一条边的编号</p><p>然后枚举将上面的边长度扩大二倍之后再求最短路,记录一个其中的到n的最短路的长度的最大值</p><p>最后输出差就好了</p><hr><h2>P1186 玛丽卡 </h2><p><div class="am-u-md-8 lg-right" name="problemleft"></div></p><p><div class="lg-content-left"></div></p><p><div class="lg-article am-g"></div></p><h2>题目描述</h2><br><p>麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复。</p><br><p>因为她和他们不住在同一个城市,因此她开始准备她的长途旅行。</p><br><p>在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城市路上所需花费的时间。</p><br><p>麦克在车中无意中听到有一条路正在维修,并且那儿正堵车,但没听清楚到底是哪一条路。无论哪一条路正在维修,从玛丽卡所在的城市都能到达麦克所在的城市。</p><br><p>玛丽卡将只从不堵车的路上通过,并且她将按最短路线行车。麦克希望知道在最糟糕的情况下玛丽卡到达他所在的城市需要多长时间,这样他就能保证他的女朋友离开该城市足够远。</p><br><p>编写程序,帮助麦克找出玛丽卡按最短路线通过不堵车道路到达他所在城市所需的最长时间(用分钟表示)。</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>第一行有两个用空格隔开的数$N$和$M$,分别表示城市的数量以及城市间道路的数量。$1≤N≤1000,1≤M≤N \times (N-1)/2$。城市用数字$1-N$标识,麦克在城市$1$中,玛丽卡在城市$N$中。 </p><br><p>接下来的$M$行中每行包含三个用空格隔开的数$A,B,V$。其中$1≤A,B≤N,1≤V≤1000$。这些数字表示在$A$和城市$B$中间有一条双行道,并且在$V$分钟内是就能通过。</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>一行,写出用分钟表示的最长时间,在这段时间中,无论哪条路在堵车,玛丽卡应该能够到达麦克处,如果少于这个时间的话,则必定存在一条路,该条路一旦堵车,玛丽卡就不能够赶到麦克处。</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>5 7<br>1 2 8<br>1 4 10<br>2 3 9<br>2 4 10<br>2 5 1<br>3 4 7<br>3 5 10<br></pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>27</pre><br></div><br></div><br>—<br><br><h2>sol</h2><p>思路跟上题是一样的,但是这个是直接删边,然后直接输出ans2</p><p>直接把边权赋值为一个较大而又不至于爆int的数就好了,基本上没怎么改程序233</p><hr><h1>Code 1</h1><figure class="highlight cpp"><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="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"><cmath></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="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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;</span><br><span class="line"> a+=(c-<span class="string">'0'</span>);</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">101</span>],cnt,pre[<span class="number">101</span>];</span><br><span class="line"><span class="keyword">int</span> q[<span class="number">100010</span>],qh,qt,dis[<span class="number">101</span>],ans1,ans2;</span><br><span class="line"><span class="keyword">bool</span> inq[<span class="number">101</span>];</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> from,to,next,v;</span><br><span class="line">}e[<span class="number">10010</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">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].from=from;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">spfa</span><span class="params">(<span class="keyword">bool</span> sb)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> temp;</span><br><span class="line"> <span class="built_in">memset</span>(dis,<span class="number">127</span>,<span class="keyword">sizeof</span> dis);</span><br><span class="line"> <span class="comment">//cout<<dis[n]<<'\n';</span></span><br><span class="line"> qh=<span class="number">0</span>;qt=<span class="number">0</span>;</span><br><span class="line"> dis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> q[qt]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(qh<=qt)</span><br><span class="line"> {</span><br><span class="line"> temp=last[q[qh]];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[q[qh]]+e[temp].v<dis[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to]=dis[q[qh]]+e[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!sb)</span><br><span class="line"> pre[e[temp].to]=temp;</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> q[++qt]=e[temp].to;</span><br><span class="line"> inq[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> inq[q[qh]]=<span class="number">0</span>;</span><br><span class="line"> ++qh;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(!sb)</span><br><span class="line"> ans1=dis[n];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> ans2=max(ans2,dis[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> tx,ty,tz;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> add(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> spfa(<span class="number">0</span>);</span><br><span class="line"> tx=pre[n];</span><br><span class="line"> <span class="keyword">while</span>(tx)</span><br><span class="line"> {</span><br><span class="line"> e[tx].v*=<span class="number">2</span>;</span><br><span class="line"> spfa(<span class="number">1</span>);</span><br><span class="line"> e[tx].v/=<span class="number">2</span>;</span><br><span class="line"> tx=pre[e[tx].from];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<ans2-ans1;</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><h1>Code 2</h1><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><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"><cmath></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="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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;</span><br><span class="line"> a+=(c-<span class="string">'0'</span>);</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">1001</span>],cnt,pre[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">int</span> q[<span class="number">100010</span>],qh,qt,dis[<span class="number">1001</span>],ans1,ans2;</span><br><span class="line"><span class="keyword">bool</span> inq[<span class="number">1001</span>];</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> from,to,next,v;</span><br><span class="line">}e[<span class="number">1000010</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">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].from=from;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">spfa</span><span class="params">(<span class="keyword">bool</span> sb)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> temp;</span><br><span class="line"> <span class="built_in">memset</span>(dis,<span class="number">127</span>,<span class="keyword">sizeof</span> dis);</span><br><span class="line"> <span class="comment">//cout<<dis[n]<<'\n';</span></span><br><span class="line"> qh=<span class="number">0</span>;qt=<span class="number">0</span>;</span><br><span class="line"> dis[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line"> q[qt]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(qh<=qt)</span><br><span class="line"> {</span><br><span class="line"> temp=last[q[qh]];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[q[qh]]+e[temp].v<dis[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to]=dis[q[qh]]+e[temp].v;</span><br><span class="line"> <span class="keyword">if</span>(!sb)</span><br><span class="line"> pre[e[temp].to]=temp;</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> q[++qt]=e[temp].to;</span><br><span class="line"> inq[e[temp].to]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> inq[q[qh]]=<span class="number">0</span>;</span><br><span class="line"> ++qh;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(!sb)</span><br><span class="line"> ans1=dis[n];</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> ans2=max(ans2,dis[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> tx,ty,tz;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> rep(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> add(ty,tx,tz);</span><br><span class="line"> }</span><br><span class="line"> spfa(<span class="number">0</span>);</span><br><span class="line"> tx=pre[n];</span><br><span class="line"> <span class="keyword">while</span>(tx)</span><br><span class="line"> {</span><br><span class="line"> ty=e[tx].v;</span><br><span class="line"> e[tx].v=<span class="number">1147483647</span>;</span><br><span class="line"> spfa(<span class="number">1</span>);</span><br><span class="line"> e[tx].v=ty;</span><br><span class="line"> tx=pre[e[tx].from];</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//cout<<ans2<<' '<<ans1<<'\n';</span></span><br><span class="line"> <span class="comment">//cout<<ans2-ans1;</span></span><br><span class="line"> <span class="built_in">cout</span><<ans2;</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>
<tags>
<tag> 最短路 </tag>
<tag> SPFA </tag>
<tag> 枚举 </tag>
</tags>
</entry>
<entry>
<title>【USACO08OCT】打井Watering Hole</title>
<link href="/2018/10/12/%5BUSACO08OCT%5D%E6%89%93%E4%BA%95Watering%20Hole/"/>
<url>/2018/10/12/%5BUSACO08OCT%5D%E6%89%93%E4%BA%95Watering%20Hole/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1550" target="_blank" rel="noopener">luogu P1550</a></p><a id="more"></a><p><div class="am-u-md-8 lg-right" name="problemleft"></div></p><p><div class="lg-content-left"></div></p><p><div class="lg-article am-g"></div></p><h2>题目背景</h2><br><p>John的农场缺水了!!!</p><br><h2>题目描述</h2><br><p>Farmer John has decided to bring water to his N (1 <= N <= 300) pastures which are conveniently numbered 1..N. He may bring water to a pasture either by building a well in that pasture or connecting the pasture via a pipe to another pasture which already has water.</p><br><p>Digging a well in pasture i costs W_i (1 <= W_i <= 100,000).</p><br><p>Connecting pastures i and j with a pipe costs P_ij (1 <= P_ij <= 100,000; P_ij = P_ji; P_ii=0).</p><br><p>Determine the minimum amount Farmer John will have to pay to water all of his pastures.</p><br><p>POINTS: 400</p><br><p>农民John 决定将水引入到他的n(1<=n<=300)个牧场。他准备通过挖若</p><br><p>干井,并在各块田中修筑水道来连通各块田地以供水。在第i 号田中挖一口井需要花费W_i(1<=W_i<=100,000)元。连接i 号田与j 号田需要P_ij (1 <= P_ij <= 100,000 , P_ji=P_ij)元。</p><br><p>请求出农民John 需要为使所有农场都与有水的农场相连或拥有水井所需要的钱数。</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>第1 行为一个整数n。</p><br><p>第2 到n+1 行每行一个整数,从上到下分别为W_1 到W_n。</p><br><p>第n+2 到2n+1 行为一个矩阵,表示需要的经费(P_ij)。</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>只有一行,为一个整数,表示所需要的钱数。</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>4<br>5<br>4<br>4<br>3<br>0 2 2 2<br>2 0 3 3<br>2 3 0 4<br>2 3 4 0</pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>9</pre><br></div><br></div><br><h2>说明</h2><br><p>John等着用水,你只有1s时间!!!</p><br><br><div class="lg-article-sub am-g" id="sub"></div><br><br><br>—<br><br><h2>sol</h2><p>比较显然的贪心+最小生成树</p><p>并查集维护的同时维护一个联通块里挖井的花费最小值,做最小生成树的时候贪心一下</p><p>如果联通(一条边)的花费加上两个联通块里挖井的最小值的较小值小于在两个联通块里挖井的花费最小值之和,那么我们就把它们联通,否则就让他们分别挖井就好了</p><p>一开始sb了,忘了加上这两个的较小值,居然能能过70%???</p><hr><h2>Code</h2><figure class="highlight cpp"><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><span class="line">99</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line">{</span><br><span class="line">t=<span class="number">1</span>;</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line">{</span><br><span class="line">a*=<span class="number">10</span>;</span><br><span class="line">a+=(c-<span class="string">'0'</span>);</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"><span class="keyword">int</span> from,to,v;</span><br><span class="line"><span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> Edge &b)<span class="keyword">const</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> v<b.v;</span><br><span class="line">}</span><br><span class="line">}e[<span class="number">301</span>*<span class="number">301</span>];</span><br><span class="line"><span class="keyword">int</span> n,w[<span class="number">301</span>],cnt;</span><br><span class="line"><span class="keyword">int</span> fa[<span class="number">301</span>],minn[<span class="number">301</span>];</span><br><span class="line"><span class="keyword">bool</span> water[<span class="number">301</span>];</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">return</span> fa[x]<<span class="number">0</span>?x:(fa[x]=find(fa[x]));</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">merge</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">x=find(x);y=find(y);</span><br><span class="line"><span class="keyword">if</span>(fa[x]>fa[y])</span><br><span class="line"> swap(x,y);</span><br><span class="line">fa[x]+=fa[y];</span><br><span class="line">minn[x]=min(minn[x],minn[y]);</span><br><span class="line">fa[y]=x;</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">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">e[++cnt].from=from;</span><br><span class="line">e[cnt].to=to;</span><br><span class="line">e[cnt].v=v;</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> tx,ty;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans=<span class="number">0</span>;</span><br><span class="line">n=read();</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> fa[i]=<span class="number">-1</span>;</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line">{</span><br><span class="line">w[i]=read();</span><br><span class="line">minn[i]=w[i];</span><br><span class="line">}</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line">tx=read();</span><br><span class="line"><span class="keyword">if</span>(i<j)<span class="comment">//not have to add an edge twice</span></span><br><span class="line"> add(i,j,tx);</span><br><span class="line"> }</span><br><span class="line">sort(e+<span class="number">1</span>,e+cnt+<span class="number">1</span>);</span><br><span class="line">rep(i,<span class="number">1</span>,cnt)</span><br><span class="line">{</span><br><span class="line">tx=find(e[i].from);ty=find(e[i].to);</span><br><span class="line"><span class="keyword">if</span>(tx==ty)</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"><span class="keyword">if</span>(min(minn[tx],minn[ty])+e[i].v<=minn[tx]+minn[ty])<span class="comment">//e[i].v<=minn[tx]+minn[ty])</span></span><br><span class="line">{</span><br><span class="line">ans+=e[i].v;</span><br><span class="line">merge(tx,ty);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line">{</span><br><span class="line">tx=find(i);</span><br><span class="line"><span class="keyword">if</span>(!water[tx])</span><br><span class="line">{</span><br><span class="line">ans+=minn[tx];</span><br><span class="line">water[tx]=<span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="built_in">cout</span><<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>]]></content>
<tags>
<tag> 贪心 </tag>
<tag> 图论 </tag>
<tag> 最小生成树 </tag>
</tags>
</entry>
<entry>
<title>【USACO09NOV】找工就业Job Hunt</title>
<link href="/2018/10/12/%5BUSACO09NOV%5D%E6%89%BE%E5%B7%A5%E5%B0%B1%E4%B8%9AJob%20Hunt/"/>
<url>/2018/10/12/%5BUSACO09NOV%5D%E6%89%BE%E5%B7%A5%E5%B0%B1%E4%B8%9AJob%20Hunt/</url>
<content type="html"><![CDATA[<p><a href="https://www.luogu.org/problemnew/show/P1938" target="_blank" rel="noopener">luogu P1938</a></p><a id="more"></a><p><div class="am-u-md-8 lg-right" name="problemleft"></div></p><p><div class="lg-content-left"></div></p><p><div class="lg-article am-g"></div></p><h2>题目描述</h2><br><p>Bessie is running out of money and is searching for jobs. Farmer John knows this and wants the cows to travel around so he has imposed a rule that his cows can only make D (1 <= D <= 1,000) dollars in a city before they must work in another city. Bessie can, however, return to a city after working elsewhere for a while and again earn the D dollars maximum in that city. There is no limit on the number of times Bessie can do this.</p><br><p>Bessie’s world comprises P (1 <= P <= 150) one-way paths connecting C (2 <= C <= 220) cities conveniently numbered 1..C. Bessie is currently in city S (1 <= S <= C). Path i runs one-way from city A_i to city B_i (1 <= A_i <= C; 1 <= B_i <= C) and costs nothing to traverse.</p><br><p>To help Bessie, Farmer John will give her access to his private jet service. This service features F (1 <= F <= 350) routes, each of which is a one way flight from one city J_i to a another K_i (1 <= J_i <= C; 1 <= K_i <= C) and which costs T_i (1 <= T_i <= 50,000) dollars. Bessie can pay for the tickets from future earnings if she doesn’t have the cash on hand.</p><br><p>Bessie can opt to retire whenever and wherever she wants. Given an unlimited amount of time, what is the most money that Bessie can make presuming she can make the full D dollars in each city she can travel to? Print -1 if there is no limit to this amount.</p><br><p>奶牛们正在找工作。农场主约翰知道后,鼓励奶牛们四处碰碰运气。而且他还加了一条要求:一头牛在一个城市最多只能赚D(1≤D≤1000)美元,然后它必须到另一座城市工作。当然,它可以在别处工作一阵子后又回到原来的城市再最多赚D美元。而且这样的往返次数没有限制。</p><br><p>城市间有P(1≤P≤150)条单向路径连接,共有C(2≤C≤220)座城市,编号从1到C。奶牛贝茜当前处在城市S(1≤S≤C)。路径i从城市A_i到城市B_i(1≤A_i≤C,1≤B_i≤C),在路径上行走不用任何花费。</p><br><p>为了帮助贝茜,约翰让它使用他的私人飞机服务。这项服务有F条(1≤F≤350)单向航线,每条航线是从城市J_i飞到另一座城市K_i(1≤J_i≤C,1≤K_i≤C),费用是T_i(1≤T_i≤50000)美元。如果贝茜手中没有现钱,可以用以后赚的钱来付机票钱。</p><br><p>贝茜可以选择在任何时候,在任何城市退休。如果在工作时间上不做限制,贝茜总共可以赚多少钱呢?如果赚的钱也不会出现限制,就输出-1。</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>第一行:5个用空格分开的整数D,P,C,F,S。</p><br><p>第2到第P+1行:第i+1行包含2个用空格分开的整数,表示一条从城市A_i到城市B_i的单向路径。</p><br><p>接下来F行,每行3个用空格分开的整数,表示一条从城市J_i到城市K_i的单向航线,费用是T_i。</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>一个整数,在上述规则下最多可以赚到的钱数。</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>100 3 5 2 1<br>1 5<br>2 3<br>1 4<br>5 2 150<br>2 5 120<br></pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>250</pre><br></div><br></div><br><h2>说明</h2><br><p>This world has five cities, three paths and two jet routes. Bessie starts out in city 1, and she can only make 100 dollars in each city before moving on.</p><br><p>Bessie can travel from city 1 to city 5 to city 2 to city 3, and make a total of 4<em>100 - 150 = 250 dollars.</em></p><br><p>Source: USACO 2009 November Silver</p><br><p>这个世界上有五个城市,三条单向路径和两条单向航线。贝茜从一号城市开始她的旅行,她在离开一个城市前最多只能在这个城市赚100美元。</p><br><p>贝茜可以通过从一号城市–>五号城市–>二号城市–>三号城市的旅行赚到4100-150=250美元。</p><br><p>(注:在四个城市各赚100美元,从五号城市飞到二号城市花掉150美元)</p><br><p>来源:USACO 2009 十一月银组</p><br><br><div class="lg-article-sub am-g" id="sub"></div><br><br><br>—<br><br><h2>sol</h2><p>直接建图spfa最长路,再加个判正环(虽然数据里好像并没有输出-1的点)</p><p>好久没打spfa了。。。居然打错了好几次。。。总结一下错误:</p><pre><code>忘了出队出队的时候忘了去掉在队列中的标记忘了切到下一条边(指邻接链表)答案初始化错了orz读入优化居然把&&写成了||</code></pre><hr><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;</span><br><span class="line"> a+=(c-<span class="string">'0'</span>);</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"> <span class="keyword">int</span> to,next,v;</span><br><span class="line">}e[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">int</span> d,p,c,f,s;</span><br><span class="line"><span class="keyword">int</span> last[<span class="number">300</span>],cnt;</span><br><span class="line"><span class="keyword">int</span> incnt[<span class="number">300</span>],q[<span class="number">100000</span>],qh,qt,dis[<span class="number">300</span>];<span class="comment">//in_queue_count , queue , q_head , q_tail , dis</span></span><br><span class="line"><span class="keyword">bool</span> inq[<span class="number">300</span>]; <span class="comment">// judge if it is in queue</span></span><br><span class="line"><span class="keyword">int</span> ans;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to,<span class="keyword">const</span> <span class="keyword">int</span> &v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> e[++cnt].next=last[from];</span><br><span class="line"> last[from]=cnt;</span><br><span class="line"> e[cnt].to=to;</span><br><span class="line"> e[cnt].v=v;</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">bool</span> sb=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> temp;</span><br><span class="line"> <span class="keyword">int</span> tx,ty,tz=<span class="number">0</span>;</span><br><span class="line"> <span class="comment">//cout<<'a';</span></span><br><span class="line"> d=read();p=read();c=read();f=read();s=read();</span><br><span class="line"> <span class="comment">//cout<<d<<' '<<p<<' '<<c<<' '<<f<<' '<<s<<'\n';</span></span><br><span class="line"> ans=d;</span><br><span class="line"> rep(i,<span class="number">1</span>,c)</span><br><span class="line"> dis[i]=<span class="number">-2000000000</span>;</span><br><span class="line"> dis[s]=d;</span><br><span class="line"> rep(i,<span class="number">1</span>,p)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> }</span><br><span class="line"> rep(i,<span class="number">1</span>,f)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tz=read();</span><br><span class="line"> add(tx,ty,tz);</span><br><span class="line"> }</span><br><span class="line"> q[qt]=s;</span><br><span class="line"> <span class="keyword">while</span>(qh<=qt)</span><br><span class="line"> {</span><br><span class="line"> temp=last[q[qh]];</span><br><span class="line"> <span class="keyword">while</span>(temp)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(dis[q[qh]]-e[temp].v+d>dis[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> dis[e[temp].to]=dis[q[qh]]-e[temp].v+d;</span><br><span class="line"> ans=max(ans,dis[e[temp].to]);</span><br><span class="line"> <span class="keyword">if</span>(!inq[e[temp].to])</span><br><span class="line"> {</span><br><span class="line"> incnt[e[temp].to]++;</span><br><span class="line"> q[++qt]=e[temp].to;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(incnt[e[temp].to]>=c+<span class="number">1</span>)</span><br><span class="line"> {</span><br><span class="line"> sb=<span class="number">1</span>;<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> temp=e[temp].next;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(sb)</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> inq[q[qh]]=<span class="number">0</span>;</span><br><span class="line"> ++qh;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(sb)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"-1"</span>);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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>]]></content>
<tags>
<tag> 图论 </tag>
<tag> SPFA </tag>
</tags>
</entry>
<entry>
<title>婆罗门的辣鸡问题解题报告</title>
<link href="/2018/10/12/%E5%A9%86%E7%BD%97%E9%97%A8%E7%9A%84%E8%BE%A3%E9%B8%A1%E9%97%AE%E9%A2%98%E8%A7%A3%E9%A2%98%E6%8A%A5%E5%91%8A/"/>
<url>/2018/10/12/%E5%A9%86%E7%BD%97%E9%97%A8%E7%9A%84%E8%BE%A3%E9%B8%A1%E9%97%AE%E9%A2%98%E8%A7%A3%E9%A2%98%E6%8A%A5%E5%91%8A/</url>
<content type="html"><![CDATA[<p>总体来说还算可以的一套题。。。</p><a id="more"></a><p>难度貌似有点倒序?</p><h2>婆罗门的山区火箭运输</h2><p>这题实际上相当于货车运输的高度弱化版。。。</p><p>看到最大值最小就想到了二分答案。。。但是二分答案的验证复杂度不稳定。。。</p><p>最坏大概是O($2^{nm}$log max{$w_{i,j}$}-min{$w_{i,j}$})</p><p>并不能稳过</p><p>而转化成货车运输的思路,操作比那要简单得多</p><p>先建图,把点权差的绝对值当做边权</p><p>因为只需要求出(1,1)到(n,m)的所有路径中最小值最大的那个最小的最大值</p><p>所以就可以做一颗最小生成树</p><p>而且只需要加边到让(1,1)和(n,m)联通即可,然后不断更新ans</p><p>显然是正确的</p><p>复杂度O(nm log nm) (忽略了并查集、建图和读入。。。)</p><p>但是我空间开小了。。。然后就炸了4个点qwq,开了两倍空间就可以a了</p><p>1.5s就能卡掉了二分答案。。。1s可以卡个常数,需要读入优化+一些简单卡常操作。。。</p><h2>Code</h2><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 1010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line">{</span><br><span class="line">t=<span class="number">1</span>;</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line">{</span><br><span class="line">a*=<span class="number">10</span>;</span><br><span class="line">a+=(c-<span class="string">'0'</span>);</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"><span class="keyword">int</span> from,to,v;</span><br><span class="line"><span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> Edge &b)<span class="keyword">const</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">return</span> v<b.v;</span><br><span class="line">}</span><br><span class="line">}e[MAXN*MAXN*<span class="number">2</span>];<span class="comment">//12MB*2</span></span><br><span class="line"><span class="keyword">int</span> w[MAXN+<span class="number">1</span>][MAXN+<span class="number">1</span>],n,m,cnt,fa[MAXN*MAXN*<span class="number">2</span>];<span class="comment">//less than 12 MB*2</span></span><br><span class="line"><span class="keyword">int</span> ans;</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> from,<span class="keyword">int</span> to,<span class="keyword">int</span> v)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">e[++cnt].from=from;</span><br><span class="line">e[cnt].to=to;</span><br><span class="line">e[cnt].v=v;</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 class="comment">//union -> find</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">return</span> fa[x]?(fa[x]=find(fa[x])):x;<span class="comment">//lu jing ya suo</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">merge</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span><span class="comment">//union -> merge</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">x=find(x);y=find(y);</span><br><span class="line">fa[x]=y;</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">freopen(<span class="string">"mountain.in"</span>,<span class="string">"r"</span>,<span class="built_in">stdin</span>);</span><br><span class="line">freopen(<span class="string">"mountain.out"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line"><span class="built_in">memset</span>(fa,<span class="number">0</span>,<span class="keyword">sizeof</span> fa);</span><br><span class="line"><span class="keyword">int</span> tempa,tempb;</span><br><span class="line">n=read();m=read();</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> rep(j,<span class="number">1</span>,m)</span><br><span class="line"> w[i][j]=read();</span><br><span class="line">rep(i,<span class="number">1</span>,n)<span class="comment">//build the map , add edge one by one</span></span><br><span class="line">{</span><br><span class="line">rep(j,<span class="number">1</span>,m)</span><br><span class="line">{</span><br><span class="line"><span class="comment">//if(i==1&&j==1)</span></span><br><span class="line"><span class="comment">// cout<<i*m+j<<' '<<1*m+1<<'\n';</span></span><br><span class="line"><span class="comment">//if(i==n&&j==m)</span></span><br><span class="line"><span class="comment">// cout<<i*m+j<<' '<<n*m+m<<'\n';</span></span><br><span class="line"><span class="keyword">if</span>(i!=n)<span class="comment">//if(i<n)</span></span><br><span class="line"> add(i*m+j,(i+<span class="number">1</span>)*m+j,<span class="built_in">abs</span>(w[i][j]-w[i+<span class="number">1</span>][j]));</span><br><span class="line"><span class="keyword">if</span>(j!=m)<span class="comment">//if(j<m)</span></span><br><span class="line"> add(i*m+j,i*m+j+<span class="number">1</span>,<span class="built_in">abs</span>(w[i][j]-w[i][j+<span class="number">1</span>]));</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">sort(e+<span class="number">1</span>,e+cnt+<span class="number">1</span>);</span><br><span class="line">ans=<span class="number">0</span>;</span><br><span class="line">rep(i,<span class="number">1</span>,cnt)<span class="comment">//kruskal , only need to let (1,1) and (n,m) are accessable each other</span></span><br><span class="line">{</span><br><span class="line">tempa=find(e[i].from);tempb=find(e[i].to);</span><br><span class="line"><span class="keyword">if</span>(tempa==tempb)</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line">merge(tempa,tempb);</span><br><span class="line">ans=max(ans,e[i].v);</span><br><span class="line"><span class="keyword">if</span>(find(<span class="number">1</span>*m+<span class="number">1</span>)==find(n*m+m))</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d"</span>,ans);</span><br><span class="line">fclose(<span class="built_in">stdin</span>);</span><br><span class="line">fclose(<span class="built_in">stdout</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>婆罗门的井字棋</h2><p>就是模拟。。。没什么好说的。。。</p><p>直接暴力判断,人工能写出来所有判断情况,应该就不会T了。。。</p><p>但是是真的考验耐心。。。</p><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> mapp[<span class="number">4</span>][<span class="number">4</span>];</span><br><span class="line"><span class="keyword">bool</span> usedp[<span class="number">27</span>],usedt[<span class="number">27</span>][<span class="number">27</span>];<span class="comment">//used_person , used_team</span></span><br><span class="line"><span class="keyword">int</span> ansp,anst;<span class="comment">//ans_person , ans_team</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">ansp=<span class="number">0</span>;anst=<span class="number">0</span>;</span><br><span class="line"><span class="built_in">memset</span>(usedp,<span class="number">0</span>,<span class="keyword">sizeof</span> usedp);</span><br><span class="line"><span class="built_in">memset</span>(usedt,<span class="number">0</span>,<span class="keyword">sizeof</span> usedt);</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">freopen(<span class="string">"count.in"</span>,<span class="string">"r"</span>,<span class="built_in">stdin</span>);</span><br><span class="line">freopen(<span class="string">"count.out"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line">init();</span><br><span class="line">ansp=<span class="number">0</span>;anst=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line">rep(i,<span class="number">1</span>,<span class="number">3</span>)</span><br><span class="line"> rep(j,<span class="number">1</span>,<span class="number">3</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> mapp[i][j]=(c-<span class="string">'A'</span>+<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line">rep(i,<span class="number">1</span>,<span class="number">3</span>)<span class="comment">//judge hang</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(mapp[i][<span class="number">1</span>]==mapp[i][<span class="number">2</span>]&&mapp[i][<span class="number">2</span>]==mapp[i][<span class="number">3</span>])<span class="comment">//person win</span></span><br><span class="line"> <span class="keyword">if</span>(!usedp[mapp[i][<span class="number">1</span>]])</span><br><span class="line"> {</span><br><span class="line"> usedp[mapp[i][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line"> ++ansp;</span><br><span class="line"> }</span><br><span class="line"><span class="keyword">if</span>(mapp[i][<span class="number">1</span>]==mapp[i][<span class="number">2</span>]&&mapp[i][<span class="number">2</span>]!=mapp[i][<span class="number">3</span>])<span class="comment">//left two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[i][<span class="number">1</span>]][mapp[i][<span class="number">3</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[i][<span class="number">1</span>]][mapp[i][<span class="number">3</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[i][<span class="number">3</span>]][mapp[i][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[i][<span class="number">1</span>]==mapp[i][<span class="number">3</span>]&&mapp[i][<span class="number">2</span>]!=mapp[i][<span class="number">3</span>])<span class="comment">//two points</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[i][<span class="number">1</span>]][mapp[i][<span class="number">2</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[i][<span class="number">1</span>]][mapp[i][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[i][<span class="number">2</span>]][mapp[i][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[i][<span class="number">2</span>]==mapp[i][<span class="number">3</span>]&&mapp[i][<span class="number">1</span>]!=mapp[i][<span class="number">2</span>])<span class="comment">//right two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[i][<span class="number">2</span>]][mapp[i][<span class="number">1</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[i][<span class="number">2</span>]][mapp[i][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[i][<span class="number">1</span>]][mapp[i][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">rep(j,<span class="number">1</span>,<span class="number">3</span>)<span class="comment">//judge lie</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][j]==mapp[<span class="number">2</span>][j]&&mapp[<span class="number">2</span>][j]==mapp[<span class="number">3</span>][j])<span class="comment">//person win</span></span><br><span class="line"> <span class="keyword">if</span>(!usedp[mapp[<span class="number">1</span>][j]])</span><br><span class="line"> {</span><br><span class="line"> ++ansp;</span><br><span class="line"> usedp[mapp[<span class="number">1</span>][j]]=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][j]==mapp[<span class="number">2</span>][j]&&mapp[<span class="number">2</span>][j]!=mapp[<span class="number">3</span>][j])<span class="comment">//up two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">2</span>][j]][mapp[<span class="number">3</span>][j]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">2</span>][j]][mapp[<span class="number">3</span>][j]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">3</span>][j]][mapp[<span class="number">2</span>][j]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][j]==mapp[<span class="number">3</span>][j]&&mapp[<span class="number">2</span>][j]!=mapp[<span class="number">3</span>][j])<span class="comment">//two points</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">2</span>][j]][mapp[<span class="number">3</span>][j]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">2</span>][j]][mapp[<span class="number">3</span>][j]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">3</span>][j]][mapp[<span class="number">2</span>][j]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">2</span>][j]==mapp[<span class="number">3</span>][j]&&mapp[<span class="number">1</span>][j]!=mapp[<span class="number">2</span>][j])<span class="comment">//down two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">1</span>][j]][mapp[<span class="number">2</span>][j]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">1</span>][j]][mapp[<span class="number">2</span>][j]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">2</span>][j]][mapp[<span class="number">1</span>][j]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="comment">//judge dui jiao xian</span></span><br><span class="line"><span class="comment">//left->right</span></span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][<span class="number">1</span>]==mapp[<span class="number">2</span>][<span class="number">2</span>]&&mapp[<span class="number">2</span>][<span class="number">2</span>]==mapp[<span class="number">3</span>][<span class="number">3</span>])<span class="comment">//person win</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedp[mapp[<span class="number">1</span>][<span class="number">1</span>]])</span><br><span class="line">{</span><br><span class="line">usedp[mapp[<span class="number">1</span>][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line">ansp++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][<span class="number">1</span>]==mapp[<span class="number">2</span>][<span class="number">2</span>]&&mapp[<span class="number">2</span>][<span class="number">2</span>]!=mapp[<span class="number">3</span>][<span class="number">3</span>])<span class="comment">//up two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">3</span>][<span class="number">3</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">3</span>][<span class="number">3</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">3</span>][<span class="number">3</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][<span class="number">1</span>]==mapp[<span class="number">3</span>][<span class="number">3</span>]&&mapp[<span class="number">1</span>][<span class="number">1</span>]!=mapp[<span class="number">2</span>][<span class="number">2</span>])<span class="comment">//two points</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">1</span>][<span class="number">1</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">1</span>][<span class="number">1</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">1</span>][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">2</span>][<span class="number">2</span>]==mapp[<span class="number">3</span>][<span class="number">3</span>]&&mapp[<span class="number">1</span>][<span class="number">1</span>]!=mapp[<span class="number">2</span>][<span class="number">2</span>])<span class="comment">//down two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">1</span>][<span class="number">1</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">1</span>][<span class="number">1</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">1</span>][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="comment">//right->left</span></span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][<span class="number">3</span>]==mapp[<span class="number">2</span>][<span class="number">2</span>]&&mapp[<span class="number">2</span>][<span class="number">2</span>]==mapp[<span class="number">3</span>][<span class="number">1</span>])<span class="comment">//person win</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedp[mapp[<span class="number">1</span>][<span class="number">3</span>]])</span><br><span class="line">{</span><br><span class="line">usedp[mapp[<span class="number">1</span>][<span class="number">3</span>]]=<span class="number">1</span>;</span><br><span class="line">ansp++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][<span class="number">3</span>]==mapp[<span class="number">2</span>][<span class="number">2</span>]&&mapp[<span class="number">2</span>][<span class="number">2</span>]!=mapp[<span class="number">3</span>][<span class="number">1</span>])<span class="comment">//up two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">3</span>][<span class="number">1</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">3</span>][<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">3</span>][<span class="number">1</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">1</span>][<span class="number">3</span>]==mapp[<span class="number">3</span>][<span class="number">1</span>]&&mapp[<span class="number">1</span>][<span class="number">3</span>]!=mapp[<span class="number">2</span>][<span class="number">2</span>])<span class="comment">//two points</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">1</span>][<span class="number">3</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">1</span>][<span class="number">3</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">1</span>][<span class="number">3</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="keyword">if</span>(mapp[<span class="number">2</span>][<span class="number">2</span>]==mapp[<span class="number">3</span>][<span class="number">1</span>]&&mapp[<span class="number">1</span>][<span class="number">3</span>]!=mapp[<span class="number">2</span>][<span class="number">2</span>])<span class="comment">//down two</span></span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(!usedt[mapp[<span class="number">1</span>][<span class="number">3</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]])</span><br><span class="line">{</span><br><span class="line">usedt[mapp[<span class="number">1</span>][<span class="number">3</span>]][mapp[<span class="number">2</span>][<span class="number">2</span>]]=<span class="number">1</span>;</span><br><span class="line">usedt[mapp[<span class="number">2</span>][<span class="number">2</span>]][mapp[<span class="number">1</span>][<span class="number">3</span>]]=<span class="number">1</span>;</span><br><span class="line">anst++;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%d\n%d"</span>,ansp,anst);</span><br><span class="line">fclose(<span class="built_in">stdin</span>);</span><br><span class="line">fclose(<span class="built_in">stdout</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>婆罗门的城市斗鸡</h2><p>这题数据水了,最大的N其实只有不到6000,应该O(N^2)也能过,但是不开long long就没救了</p><p>正解是O(N log N)的排序和O(N)的计算</p><p>考虑一下两个点中间夹着一个点,这两个点的距离其实可以拆分成前面的点和中间的点的距离以及后面的点和中间的点的距离</p><p>然后考虑每个点和它的后一个点的贡献,比如x和x+1,他们的距离包含在了x <em> (n-x)对里,也就是他们的距离在答案中出现了x </em> (n-x)次,直接加上这个数,每个点都这么做</p><p>最后算出来是一半的,然后 * 2就ok了</p><p>记得开long long</p><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 100010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line">{</span><br><span class="line">t=<span class="number">1</span>;</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line">{</span><br><span class="line">a*=<span class="number">10</span>;</span><br><span class="line">a+=(c-<span class="string">'0'</span>);</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> pos[MAXN];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans;</span><br><span class="line"><span class="keyword">int</span> 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">freopen(<span class="string">"chicken.in"</span>,<span class="string">"r"</span>,<span class="built_in">stdin</span>);</span><br><span class="line">freopen(<span class="string">"chicken.out"</span>,<span class="string">"w"</span>,<span class="built_in">stdout</span>);</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> pos[i]=read();</span><br><span class="line">sort(pos+<span class="number">1</span>,pos+n+<span class="number">1</span>);</span><br><span class="line">--n;</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> ans+=(<span class="keyword">long</span> <span class="keyword">long</span>)(((<span class="keyword">long</span> <span class="keyword">long</span>)i)*(<span class="keyword">long</span> <span class="keyword">long</span>)(n+<span class="number">1</span>-i)*(pos[i+<span class="number">1</span>]-pos[i]));</span><br><span class="line">ans=(<span class="keyword">long</span> <span class="keyword">long</span>)(ans*<span class="number">2</span>);</span><br><span class="line"><span class="built_in">cout</span><<ans;</span><br><span class="line">fclose(<span class="built_in">stdin</span>);</span><br><span class="line">fclose(<span class="built_in">stdout</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><hr><p>测试题目:</p><p>链接:<a href="https://pan.baidu.com/s/1fG4u5aqBIp-LBShIUszgfw" target="_blank" rel="noopener">https://pan.baidu.com/s/1fG4u5aqBIp-LBShIUszgfw</a><br>提取码:g2js</p><p>评测环境lemon,已配置好,请放心食用</p>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 模拟 </tag>
<tag> 最小生成树 </tag>
<tag> 数学 </tag>
</tags>
</entry>
<entry>
<title>P3916 图的遍历</title>
<link href="/2018/10/10/P3916%20%E5%9B%BE%E7%9A%84%E9%81%8D%E5%8E%86/"/>
<url>/2018/10/10/P3916%20%E5%9B%BE%E7%9A%84%E9%81%8D%E5%8E%86/</url>
<content type="html"><![CDATA[<p>一道图论题</p><a id="more"></a><div class="am-u-md-8 lg-right" name="problemleft"><br><div class="lg-content-left"><br><div class="lg-article am-g"><br><h2>题目描述</h2><br><p>给出$N$个点,$M$条边的有向图,对于每个点$v$,求$A(v)$表示从点$v$出发,能到达的编号最大的点。</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>第1 行,2 个整数$N,M$。</p><br><p>接下来$M$行,每行2个整数$U_i,V_i$,表示边$(U_i,V_i)$。点用$1, 2,\cdots,N$编号。</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>N 个整数$A(1),A(2),\cdots,A(N)$。</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>4 3<br>1 2<br>2 4<br>4 3</pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>4 4 3 4</pre><br></div><br></div><br><h2>说明</h2><br><p>• 对于60% 的数据,$1 \le N . K \le 10^3$;</p><br><p>• 对于100% 的数据,$1 \le N , M \le 10^5$。</p><br></div><hr><h1>sol</h1><p>看到这题第一眼:</p><p> 记忆化,直接搜</p><p>然后就WA了</p><p>大概能知道某种环可以卡掉,但是不知道怎么构造以及为什么</p><p>其实是因为后搜到的没办法更新了</p><p>比如这组数据(感谢<a href="sxyugao.top">@sxyugao</a>大佬提供的hack数据,原题hack数据太大了。。。)</p><figure class="highlight plain"><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">3 3</span><br><span class="line">1 3</span><br><span class="line">1 2</span><br><span class="line">2 1</span><br></pre></td></tr></table></figure><p>所以我们反着建图,记录大的点能到哪些小的点(也就是原图小的点能到哪些大的点),从大的开始搜索,就不会存在这样的问题</p><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath> </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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line">{</span><br><span class="line">t=<span class="number">1</span>;</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line">{</span><br><span class="line">a*=<span class="number">10</span>;</span><br><span class="line">a+=(c-<span class="string">'0'</span>);</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>{</span></span><br><span class="line"><span class="keyword">int</span> to,next;</span><br><span class="line">}e[<span class="number">100010</span>];</span><br><span class="line"><span class="keyword">int</span> cnt,n,m;</span><br><span class="line"><span class="keyword">int</span> a[<span class="number">100010</span>],last[<span class="number">100010</span>];</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> x,<span class="keyword">const</span> <span class="keyword">int</span> st)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">if</span>(a[x])</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">a[x]=st;</span><br><span class="line"><span class="keyword">int</span> temp=last[x];</span><br><span class="line"><span class="keyword">while</span>(temp)</span><br><span class="line">{</span><br><span class="line">dfs(e[temp].to,st);</span><br><span class="line">temp=e[temp].next;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &from,<span class="keyword">const</span> <span class="keyword">int</span> &to)</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">e[++cnt].to=from;</span><br><span class="line">e[cnt].next=last[to];</span><br><span class="line">last[to]=cnt;</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> tx,ty;</span><br><span class="line">n=read();m=read();</span><br><span class="line">rep(i,<span class="number">1</span>,m)</span><br><span class="line">{</span><br><span class="line">tx=read();ty=read();</span><br><span class="line">add(tx,ty);</span><br><span class="line">}</span><br><span class="line">per(i,n,<span class="number">1</span>)</span><br><span class="line"> dfs(i,i);</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>,a[i]);</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><hr><h3>题外话</h3><p>图论的题还是比较玄学啊orz</p><p>不知道要怎么练习这种东西</p></div></div>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 记忆化搜索 </tag>
</tags>
</entry>
<entry>
<title>bzoj 2456 mode</title>
<link href="/2018/10/10/bzoj%202456%20mode/"/>
<url>/2018/10/10/bzoj%202456%20mode/</url>
<content type="html"><![CDATA[<p>神奇的解法。。。</p><a id="more"></a><p><title>Problem 2456. – mode</title><center><h2>2456: mode</h2><span class="green">Time Limit: </span>1 Sec <span class="green">Memory Limit: </span>1 MB<br><span class="green">Submit: </span>7662 <span class="green">Solved: </span>2932<br>[<a href="submitpage.php?id=2456">Submit</a>][<a href="problemstatus.php?id=2456">Status</a>][<a href="bbs.php?id=2456">Discuss</a>]</center></p><h2>Description</h2><div class="content"><p>给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。</p></div><h2>Input</h2><div class="content"><p>第1行一个正整数n。<br></p><p>第2行n个正整数用空格隔开。</p><h2>Output</h2><p> 一行一个正整数表示那个众数。</p></div><h2>Sample Input</h2><br>5<br><br>3 2 3 1 3<br><br><h2>Sample Output</h2><pre><code><div class=content><span class=sampledata>3<br /></code></pre><p></p><h2>HINT</h2><br> <div class="content"><p></p><p>100%的数据,n<=500000,数列中每个数<=maxlongint。</p><br><p></p><p></p><p></p><br><p></p><hr><h2>sol</h2><p>一开始以为离散化+sort,结果空间只有1MB</p><p>然后看了题解</p><p>先是题里对众数的定义,出现次数超过n/2的数</p><p>也就是说只可能有一个众数</p><p>然后用的这种算法好像是把不是众数的数撞掉</p><p>具体可以看代码,真的短。。。</p><p>不知道为什么现在写代码会犯很多sb错误orz,感觉脑子不是很好使了…</p><hr><h2>Code</h2><figure class="highlight cpp"><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"><cstdio></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</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">long</span> <span class="keyword">long</span> x=<span class="number">-1</span>,sb;</span><br><span class="line"><span class="keyword">int</span> n,tag=<span class="number">1</span>;</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">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">scanf</span>(<span class="string">"%lld"</span>,&sb);</span><br><span class="line"><span class="keyword">if</span>(sb==x)</span><br><span class="line"> tag+=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> tag-=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">if</span>(tag<<span class="number">1</span>)</span><br><span class="line">{</span><br><span class="line">x=sb;</span><br><span class="line">tag=<span class="number">1</span>;</span><br><span class="line">} </span><br><span class="line">}</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%lld"</span>,x);</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></div>]]></content>
<tags>
<tag> 模拟 </tag>
<tag> 数学 </tag>
<tag> 神奇操作 </tag>
</tags>
</entry>
<entry>
<title>CF437C The Child and Toy</title>
<link href="/2018/10/10/CF437C%20The%20Child%20and%20Toy/"/>
<url>/2018/10/10/CF437C%20The%20Child%20and%20Toy/</url>
<content type="html"><![CDATA[<p>贪心,神仙图论</p><a id="more"></a><div class="am-u-md-8 lg-right" name="problemleft"><br><div class="lg-content-left"><br><div class="lg-article am-g"><br><h2>题意翻译</h2><br><p>n个带权点,m条无向边,删除一个点就要付出所有与之有联系且没有被删除的点的点权之和的代价。</p><br><p>求删除所有点的最小代价。</p><br><h2>题目描述</h2><br><p>On Children’s Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can’t wait to destroy the toy.</p><br><p>The toy consists of $ n $ parts and $ m $ ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let’s define an energy value of part $ i $ as $ v_{i} $ . The child spend $ v_{f1}+v_{f2}+…+v_{fk} $ energy for removing part $ i $ where $ f_{1},f_{2},…,f_{k} $ are the parts that are directly connected to the $ i $ -th and haven’t been removed.</p><br><p>Help the child to find out, what is the minimum total energy he should spend to remove all $ n $ parts.</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>The first line contains two integers $ n $ and $ m $ ( $ 1<=n<=1000 $ ; $ 0<=m<=2000 $ ). The second line contains $ n $ integers: $ v_{1},v_{2},…,v_{n} $ ( $ 0<=v_{i}<=10^{5} $ ). Then followed $ m $ lines, each line contains two integers $ x_{i} $ and $ y_{i} $ , representing a rope from part $ x_{i} $ to part $ y_{i} $ ( $ 1<=x_{i},y_{i}<=n; x_{i}≠y_{i} $ ).</p><br><p>Consider all the parts are numbered from $ 1 $ to $ n $ .</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>Output the minimum total energy the child should spend to remove all $ n $ parts of the toy.</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>4 3<br>10 20 30 40<br>1 4<br>1 2<br>2 3<br></pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>40<br></pre><br></div><br></div><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#2:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>4 4<br>100 100 100 100<br>1 2<br>2 3<br>2 4<br>3 4<br></pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#2:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>400<br></pre><br></div><br></div><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#3:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>7 10<br>40 10 20 10 20 80 40<br>1 5<br>4 7<br>4 5<br>5 2<br>5 7<br>6 4<br>1 6<br>1 3<br>4 3<br>1 4<br></pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#3:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>160<br></pre><br></div><br></div><br><h2>说明</h2><br><p>One of the optimal sequence of actions in the first sample is:</p><br><ul><br><li>First, remove part $ 3 $ , cost of the action is $ 20 $ .</li><br><li>Then, remove part $ 2 $ , cost of the action is $ 10 $ .</li><br><li>Next, remove part $ 4 $ , cost of the action is $ 10 $ .</li><br><li>At last, remove part $ 1 $ , cost of the action is $ 0 $ .</li><br></ul><br><p>So the total energy the child paid is $ 20+10+10+0=40 $ , which is the minimum.</p><br><p>In the second sample, the child will spend $ 400 $ no matter in what order he will remove the parts.</p><br></div><br><div class="lg-article-sub am-g" id="sub"></div><br></div><br></div><hr><h1>sol</h1><p>首先有个比较显然的贪心</p><p>每个点再记录与之相连的未被删除的点的点权之和</p><p>然后每次删除最小,并处理与之相连的点,重新排序</p><p>这样复杂度大概是O(n(n log n + m))</p><p>好像也能过?</p><p>但是还有更好的做法:</p><p>我们考虑这些点,删点之后,与之相连的边就都会消失</p><p>所以删点<=>删边</p><p>而每一条边的贡献就是其两端的点的权值min</p><p>这个东西我并不会证明。。。但是看了几组确实是这样</p><p>看了课件才知道这个做法。。。神仙图论。。。</p><p>这样的复杂度是O(n+m)</p><p>黄学长的课件是这么写的</p><pre><code>按照从⼤到小的次序删点。发现每条边的贡献是连接的两个点的权值的最小</code></pre><p>orzzzzzzzzzzzzz</p><hr><h1>Code</h1><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath> </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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"><span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line">{</span><br><span class="line">t=<span class="number">1</span>;</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">while</span>(c>=<span class="string">'0'</span>&&c<=<span class="string">'9'</span>)</span><br><span class="line">{</span><br><span class="line">a*=<span class="number">10</span>;</span><br><span class="line">a+=(c-<span class="string">'0'</span>);</span><br><span class="line">c=getchar();</span><br><span class="line">}</span><br><span class="line"><span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> n,m,w[<span class="number">1001</span>];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans;</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> tx,ty;</span><br><span class="line">n=read();m=read();</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> w[i]=read();</span><br><span class="line">rep(j,<span class="number">1</span>,m)</span><br><span class="line">{</span><br><span class="line">tx=read();ty=read();</span><br><span class="line">ans+=min(w[tx],w[ty]);</span><br><span class="line">}</span><br><span class="line"><span class="built_in">cout</span><<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>]]></content>
<tags>
<tag> 贪心 </tag>
<tag> 图论 </tag>
</tags>
</entry>
<entry>
<title>Mayan游戏</title>
<link href="/2018/10/10/Mayan%E6%B8%B8%E6%88%8F/"/>
<url>/2018/10/10/Mayan%E6%B8%B8%E6%88%8F/</url>
<content type="html"><![CDATA[<p>大码力模拟。。。</p><a id="more"></a><div class="am-u-md-8 lg-right" name="problemleft"><br><div class="lg-content-left"><br><div class="lg-article am-g"><br><h2>题目描述</h2><br><p><code>Mayan puzzle</code>是最近流行起来的一个游戏。游戏界面是一个$ 7$ 行$ \times 5 $列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上。游戏通关是指在规定的步数内消除所有的方块,消除方块的规则如下:</p><br><p>1 、每步移动可以且仅可以沿横向(即向左或向右)拖动某一方块一格:当拖动这一方块时,如果拖动后到达的位置(以下称目标位置)也有方块,那么这两个方块将交换位置(参见输入输出样例说明中的图$6 $到图$7$ );如果目标位置上没有方块,那么被拖动的方块将从原来的竖列中抽出,并从目标位置上掉落(直到不悬空,参见下面图1 和图2);</p><br><p><img src="https://cdn.luogu.org/upload/pic/103.png" alt=""> </p><br><p>2 、任一时刻,如果在一横行或者竖列上有连续三个或者三个以上相同颜色的方块,则它们将立即被消除(参见图1 到图3)。</p><br><p><img src="https://cdn.luogu.org/upload/pic/102.png" alt=""> </p><br><p>注意:</p><br><p>a) 如果同时有多组方块满足消除条件,几组方块会同时被消除(例如下面图$4$ ,三个颜色为$1$ 的方块和三个颜色为 $2$ 的方块会同时被消除,最后剩下一个颜色为$ 2 $的方块)。</p><br><p>b) 当出现行和列都满足消除条件且行列共享某个方块时,行和列上满足消除条件的所有方块会被同时消除(例如下面图5 所示的情形,5 个方块会同时被消除)。</p><br><p>3 、方块消除之后,消除位置之上的方块将掉落,掉落后可能会引起新的方块消除。注意:掉落的过程中将不会有方块的消除。</p><br><p>上面图1 到图 3 给出了在棋盘上移动一块方块之后棋盘的变化。棋盘的左下角方块的坐标为(0, 0 ),将位于(3, 3 )的方块向左移动之后,游戏界面从图 1 变成图 2 所示的状态,此时在一竖列上有连续三块颜色为4 的方块,满足消除条件,消除连续3 块颜色为4 的方块后,上方的颜色为3 的方块掉落,形成图 3 所示的局面。</p><br><h2>输入输出格式</h2><br><strong>输入格式:</strong><br><br><p></p><p>共 6 行。</p><br><p>第一行为一个正整数$n $,表示要求游戏通关的步数。</p><br><p>接下来的$5 $行,描述$ 7 \times 5$ 的游戏界面。每行若干个整数,每两个整数之间用一个空格隔开,每行以一个$0$ 结束,自下向上表示每竖列方块的颜色编号(颜色不多于$10$种,从$1 $开始顺序编号,相同数字表示相同颜色)。</p><br><p>输入数据保证初始棋盘中没有可以消除的方块。</p><p></p><br><strong>输出格式:</strong><br><br><p></p><p>如果有解决方案,输出$n$行,每行包含 $3$ 个整数$x,y,g$,表示一次移动,每两个整数之间用一个空格隔开,其中$(x ,y)$表示要移动的方块的坐标,$g$ 表示移动的方向,$1$ 表示向右移动,$-1$表示向左移动。注意:多组解时,按照$x$为第一关健字,$y$为第二关健字,$1$优先于$-1$ ,给出一组字典序最小的解。游戏界面左下角的坐标为$(0 ,0)$。</p><br><p>如果没有解决方案,输出一行,包含一个整数$-1$。</p><p></p><br><h2>输入输出样例</h2><br><div class="am-g"><br><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>3<br>1 0<br>2 1 0<br>2 3 4 0<br>3 1 0<br>2 4 3 4 0</pre><br></div><br><div class="am-u-md-6 copy-region"><br><strong>输出样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><br><pre>2 1 1<br>3 1 1<br>3 0 1</pre><br></div><br></div><br><h2>说明</h2><br><p>【输入输出样例说明】</p><br><p>按箭头方向的顺序分别为图$6$到图$11$</p><br><p><img src="https://cdn.luogu.org/upload/pic/104.png" alt=""> </p><br><p>样例输入的游戏局面如上面第一个图片所示,依次移动的三步是:$(2 ,1 )$处的方格向右移动,$(3,1)$处的方格向右移动,$(3,0)$处的方格向右移动,最后可以将棋盘上所有方块消除。</p><br><p>【数据范围】</p><br><p>对于$30\%$的数据,初始棋盘上的方块都在棋盘的最下面一行;</p><br><p>对于$100\%$的数据,$0 < n≤5$ 。</p><br><p>noip2011提高组day1第3题</p><br></div><br><div class="lg-article-sub am-g" id="sub"></div><br></div><br></div><hr><h1>sol</h1><p>大模拟+dfs orz</p><p>存图用的栈开小了(其实是没有删掉没用的图)调了一上午orz</p><p>其实题目只有几个核心操作</p><p>移动 move()</p><p>消除 del()</p><p>下落 down()</p><p>检验是否有块可以消除 judge()</p><p>基本上就是一道纯代码能力题。。。</p><p>搜索直接按字典序优先的顺序来搜,这样找到的第一个答案就是题目要求的</p><p>消除我是维护了一个栈,在检验到能消除的地方之后压入位置,长度和消除方向,都从左向右,从下向上</p><p>而且注意到下落之后可能还会引起新的消除,所以把judge()和down()做成了两个bool型的</p><p>然后再加一点小的剪枝就够了,毕竟3s</p><p>其实写这种码力题还是挺有趣的orzzzzzzzzz</p><p>但是不知道为什么在dfs后面加一句</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">--cnt;</span><br></pre></td></tr></table></figure><p>就会跑不出来,玄学</p><p>模拟题没什么好说的,总结一下代码里的sb错误:</p><pre><code>存图栈开小了,或者说搜完之后没有退栈搜索没判断是否在终态成功消除完了,然后就有好几个输出。。。</code></pre><hr><h1>Code</h1><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">short</span> <span class="keyword">int</span> mapp[<span class="number">500000</span>][<span class="number">8</span>][<span class="number">6</span>];</span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">int</span> cnt=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> step[<span class="number">500000</span>],n;</span><br><span class="line"><span class="keyword">short</span> <span class="keyword">int</span> ansx[<span class="number">6</span>],ansy[<span class="number">6</span>],ansg[<span class="number">6</span>];</span><br><span class="line"><span class="keyword">bool</span> solved=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">short</span> <span class="keyword">int</span> delx_que[<span class="number">1001</span>],dely_que[<span class="number">1001</span>],dell_que[<span class="number">1001</span>],delqt=<span class="number">0</span>;<span class="comment">//del_xpos_stack , del_ypos_stack , del_length_stack , del_stack_top</span></span><br><span class="line"><span class="keyword">bool</span> deld_que[<span class="number">1001</span>];<span class="comment">//del_direction_queue 0->up 1->right</span></span><br><span class="line"><span class="keyword">bool</span> vis[<span class="number">8</span>][<span class="number">6</span>][<span class="number">2</span>];</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">move</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,<span class="keyword">bool</span> dir,<span class="keyword">const</span> <span class="keyword">int</span> &w)</span><span class="comment">//change the pos of block , dir:0->left 1->right , w:last map</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="built_in">memcpy</span>(mapp[++cnt],mapp[w],<span class="keyword">sizeof</span> mapp[w]);<span class="comment">//copy the pre map</span></span><br><span class="line"><span class="keyword">if</span>(dir)<span class="comment">//move to right</span></span><br><span class="line"> swap(mapp[cnt][x][y],mapp[cnt][x][y+<span class="number">1</span>]);</span><br><span class="line"><span class="keyword">else</span><span class="comment">//move to left</span></span><br><span class="line"> swap(mapp[cnt][x][y],mapp[cnt][x][y<span class="number">-1</span>]);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">del</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &x)</span><span class="comment">//operate in the del_stack</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">while</span>(delqt)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">if</span>(deld_que[delqt])<span class="comment">//del_right</span></span><br><span class="line">{</span><br><span class="line">rep(j,dely_que[delqt],dely_que[delqt]+dell_que[delqt]<span class="number">-1</span>)</span><br><span class="line"> mapp[x][delx_que[delqt]][j]=<span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">else</span><span class="comment">//del_up</span></span><br><span class="line">{</span><br><span class="line">rep(i,delx_que[delqt],delx_que[delqt]+dell_que[delqt]<span class="number">-1</span>)</span><br><span class="line"> mapp[x][i][dely_que[delqt]]=<span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line">--delqt;</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">bool</span> <span class="title">down</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &x)</span><span class="comment">//blocks which are flying fall down to the ground , from bottom to the top</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">bool</span> sb=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> tx;</span><br><span class="line">rep(i,<span class="number">2</span>,<span class="number">7</span>)</span><br><span class="line"> rep(j,<span class="number">1</span>,<span class="number">5</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(mapp[x][i][j]&&!mapp[x][i<span class="number">-1</span>][j])</span><br><span class="line"> {</span><br><span class="line"> sb=<span class="number">1</span>;</span><br><span class="line"> tx=i;</span><br><span class="line"> <span class="keyword">while</span>(tx><span class="number">1</span>&&!mapp[x][tx<span class="number">-1</span>][j])<span class="comment">//fall to ground</span></span><br><span class="line"> {</span><br><span class="line"> swap(mapp[x][tx][j],mapp[x][tx<span class="number">-1</span>][j]);</span><br><span class="line"> --tx;</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> sb;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> &x)</span><span class="comment">//judge if there is any block is del_able & del it ,after del them ,drop</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">bool</span> sb;</span><br><span class="line"><span class="keyword">int</span> temp,tx,ty;</span><br><span class="line"><span class="built_in">memset</span>(vis,<span class="number">0</span>,<span class="keyword">sizeof</span> vis);</span><br><span class="line">rep(i,<span class="number">1</span>,<span class="number">7</span>)</span><br><span class="line"> rep(j,<span class="number">1</span>,<span class="number">5</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(!mapp[x][i][j])</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> temp=<span class="number">1</span>;</span><br><span class="line"> tx=i;ty=j;</span><br><span class="line"> <span class="keyword">while</span>(ty+<span class="number">1</span><=<span class="number">5</span>&&mapp[x][tx][ty]==mapp[x][tx][ty+<span class="number">1</span>]&&<span class="number">1</span><span class="comment">/*!vis[tx][ty+1][1]*/</span>)<span class="comment">//right</span></span><br><span class="line"> {</span><br><span class="line"> ++temp;++ty; </span><br><span class="line"> <span class="comment">//vis[tx][ty][1]=1;</span></span><br><span class="line"> <span class="comment">//vis[tx][ty+1][1]=1;</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(temp>=<span class="number">3</span>)</span><br><span class="line"> {</span><br><span class="line"> ++delqt;</span><br><span class="line"> delx_que[delqt]=i;</span><br><span class="line"> dely_que[delqt]=j;</span><br><span class="line"> deld_que[delqt]=<span class="number">1</span>;</span><br><span class="line"> dell_que[delqt]=temp;</span><br><span class="line"> }</span><br><span class="line"> temp=<span class="number">1</span>;</span><br><span class="line"> tx=i;ty=j;</span><br><span class="line"> <span class="keyword">while</span>(tx+<span class="number">1</span><=<span class="number">7</span>&&mapp[x][tx][ty]==mapp[x][tx+<span class="number">1</span>][ty]&&<span class="number">1</span><span class="comment">/*!vis[tx][ty+1][0]*/</span>)<span class="comment">//up</span></span><br><span class="line"> {</span><br><span class="line"> ++temp;++tx;</span><br><span class="line"> <span class="comment">//vis[tx][ty][0]=1;</span></span><br><span class="line"> <span class="comment">//vis[tx+1][ty][0]=1;</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(temp>=<span class="number">3</span>)</span><br><span class="line"> {</span><br><span class="line"> ++delqt;</span><br><span class="line"> delx_que[delqt]=i;</span><br><span class="line"> dely_que[delqt]=j;</span><br><span class="line"> deld_que[delqt]=<span class="number">0</span>;</span><br><span class="line"> dell_que[delqt]=temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"><span class="comment">//cout<<delqt<<'\n';</span></span><br><span class="line">del(x);</span><br><span class="line">sb=down(x);</span><br><span class="line"><span class="keyword">return</span> sb;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">rep(i,<span class="number">1</span>,n)</span><br><span class="line"> <span class="built_in">cout</span><<ansx[i]<<<span class="string">' '</span><<ansy[i]<<<span class="string">' '</span><<ansg[i]<<<span class="string">'\n'</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">read</span><span class="params">()</span><span class="comment">//read the natural map</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line">rep(i,<span class="number">1</span>,<span class="number">5</span>)</span><br><span class="line">{</span><br><span class="line">rep(j,<span class="number">1</span>,<span class="number">9</span>)</span><br><span class="line">{</span><br><span class="line"><span class="built_in">cin</span>>>mapp[<span class="number">0</span>][j][i];</span><br><span class="line"><span class="keyword">if</span>(!mapp[<span class="number">0</span>][j][i])</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"><span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> depth,<span class="keyword">int</span> x)</span><span class="comment">//dfs by dictionary num , if there is a sol , just print it . </span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"><span class="keyword">while</span>(judge(x));<span class="comment">//fall may be cause to successfully del</span></span><br><span class="line"><span class="keyword">if</span>(depth==n)</span><br><span class="line">{</span><br><span class="line">rep(i,<span class="number">1</span>,<span class="number">7</span>)</span><br><span class="line"> rep(j,<span class="number">1</span>,<span class="number">5</span>)</span><br><span class="line"> <span class="keyword">if</span>(mapp[x][i][j])</span><br><span class="line"> {</span><br><span class="line"> --cnt;</span><br><span class="line"><span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line">print();</span><br><span class="line">solved=<span class="number">1</span>; </span><br><span class="line">}</span><br><span class="line">rep(j,<span class="number">1</span>,<span class="number">5</span>)</span><br><span class="line"> rep(i,<span class="number">1</span>,<span class="number">7</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(mapp[x][i][j])<span class="comment">//if there is a block</span></span><br><span class="line"> {</span><br><span class="line"> ansx[depth+<span class="number">1</span>]=j<span class="number">-1</span>;</span><br><span class="line"> ansy[depth+<span class="number">1</span>]=i<span class="number">-1</span>;<span class="comment">//only move to suqare area on the left & the pos on the right</span></span><br><span class="line"> <span class="keyword">if</span>(j<<span class="number">5</span>)<span class="comment">//move to right</span></span><br><span class="line"> {</span><br><span class="line"> ansg[depth+<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line"> move(i,j,<span class="number">1</span>,x);</span><br><span class="line"> dfs(depth+<span class="number">1</span>,cnt);</span><br><span class="line"> <span class="keyword">if</span>(solved)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(j><span class="number">1</span>&&!mapp[x][i][j<span class="number">-1</span>])<span class="comment">//move to left</span></span><br><span class="line"> {</span><br><span class="line"> ansg[depth+<span class="number">1</span>]=<span class="number">-1</span>;</span><br><span class="line"> move(i,j,<span class="number">0</span>,x);</span><br><span class="line"> dfs(depth+<span class="number">1</span>,cnt);</span><br><span class="line"> <span class="keyword">if</span>(solved)</span><br><span class="line"> <span class="keyword">return</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="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"</span>,&n);</span><br><span class="line">read();</span><br><span class="line">dfs(<span class="number">0</span>,<span class="number">0</span>);</span><br><span class="line"><span class="keyword">if</span>(!solved)</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"-1\n"</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>]]></content>
<tags>
<tag> DFS </tag>
<tag> 模拟 </tag>
</tags>
</entry>
<entry>
<title>初探对偶图</title>
<link href="/2018/10/09/%E5%AF%B9%E5%81%B6%E5%9B%BE/"/>
<url>/2018/10/09/%E5%AF%B9%E5%81%B6%E5%9B%BE/</url>
<content type="html"><![CDATA[<p>平面图与对偶图及其在最大流最小割方面的简单应用</p><a id="more"></a><p>欧拉公式:</p><pre><code>对于一张n个点,m条边的平面图,面数f=m-n+2</code></pre><p>平面图:</p><pre><code>所有边只在顶点处相交</code></pre><p>对偶图:</p><pre><code>对任一平面图,都存在其对偶图,平面图中每一个面对应其对偶图中的点记原平面图为G,其对偶图为G* , G*的边有两种情况,回边和非回边,任一边与G中的边有交点</code></pre><p>所以这东西有什么用?</p><p>可以用来求最小割(最大流)!</p><p>对偶图G<em> 中的一条路对应原图G中的一个割 , 所以求G</em> 中的最短路就相当于求了G 中的最小割</p><p>求最大流只会一个FF算法。。。复杂度并不会分析</p><p>这样转化之后至少出现了方便分析复杂度(对我来说。。。)的算法–最短路</p><p>所以求平面图最小割就可以建对偶图后跑一边最短路</p><p>例题:</p><p><a href="https://www.lydsy.com/JudgeOnline/problem.php?id=1001" target="_blank" rel="noopener">bzoj 1001: [BeiJing2006]狼抓兔子</a></p><p>f <em> 之间边的权值就是之前原来的点 f 之间与f </em> 的边交叉的边的权值。 </p><p>一开始以为是裸的最小割。。。然而这样稠密的图好像并不能过orz</p><p><del>代码</del></p><h6>不存在的</h6><hr><h2>总结</h2><p>图论真的神奇啊orz</p><p>虽然了解了对偶图和它最短路求最小割这一应用,但是感觉理解并不透彻。。。并不会证明。。。</p><p>网络流还需要进一步学习</p><p>最后还是先保全noip 2018成绩吧。。。毕竟高三了,之前并没参加过比赛,也就只能先争取1=敲开自招大门了。。。大学再战</p><hr><h2>参考文章</h2><ul><li><a herf="https://www.cnblogs.com/abclzr/p/5033461.html">【BZOJ 1001】狼抓兔子 对偶图+SPFA</a></li><li><a herf="https://blog.csdn.net/MaxMercer/article/details/77976666">关于平面图到对偶图的转化</a></li><li><a herf="http://blog.sina.com.cn/s/blog_60707c0f01011fnn.html">对偶图的应用</a></li></ul>]]></content>
<tags>
<tag> 图论 </tag>
<tag> 最小割 </tag>
<tag> 对偶图 </tag>
</tags>
</entry>
<entry>
<title>SCOI2005-互不侵犯-状压dp入门</title>
<link href="/2018/10/09/%5BSCOI2005%5D%E4%BA%92%E4%B8%8D%E4%BE%B5%E7%8A%AF-%E7%8A%B6%E5%8E%8Bdp%E5%85%A5%E9%97%A8/"/>
<url>/2018/10/09/%5BSCOI2005%5D%E4%BA%92%E4%B8%8D%E4%BE%B5%E7%8A%AF-%E7%8A%B6%E5%8E%8Bdp%E5%85%A5%E9%97%A8/</url>
<content type="html"><![CDATA[<p>状压dp入门题目</p><a id="more"></a><h2>题目描述</h2><br><p>在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。</p><br><p>注:数据有加强(2018/4/25)</p><br><h2>输入输出格式</h2><p><strong>输入格式:</strong><br></p><p></p><p></p><p>只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)</p><p></p><br><strong>输出格式:</strong><br><p></p><p></p><p></p><p>所得的方案数</p><p></p><p></p><p></p><h2>输入输出样例</h2><p></p><p><div class="am-g"></div></p><p><div class="am-u-md-6 copy-region"><br><strong>输入样例#1:</strong><br><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a></div></p><pre>3 2</pre><div class="am-u-md-6 copy-region"><strong>输出样例#1:</strong><a class="am-badge am-radius lg-bg-orange sample-copy">复制</a><pre>16</pre><hr><h1>sol</h1><p>状压dp的经典入门题目</p><p>状态有三维(其实也可以滚动数组一下,不过我lazy了orz)</p><p>一维表示列,一维表示已经放置的king数,一维表示上一列king的位置(二进制表示,也就是状压dp的核心内容?)</p><p>然后转移枚举上一列的状态,judge一下然后转移就行了</p><p>没开long long居然调了10分钟orz,是真的烦人</p><p>复杂度O(nk2^{2n}),算出来好像1e9多,居然能过orz,还以为要加什么玄学优化来的</p><hr><h1>Code</h1><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> rep(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> per(i,l,r) for(int i=l;i>=r;--i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> dp[<span class="number">10</span>][<span class="number">82</span>][<span class="number">512</span>];</span><br><span class="line"><span class="keyword">int</span> n,k;</span><br><span class="line"><span class="keyword">int</span> maxx;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">bool</span> <span class="title">judge</span><span class="params">(<span class="keyword">int</span> zt)</span><span class="comment">//judge if there is two kings that can attack each other</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> temp=<span class="number">3</span>;</span><br><span class="line"> <span class="keyword">while</span>(temp<=maxx)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>((temp&zt)==temp)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> temp<<=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">count</span><span class="params">(<span class="keyword">int</span> zt)</span><span class="comment">//count 1 in zt</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">int</span> ans=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(zt)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(zt&<span class="number">1</span>)</span><br><span class="line"> ++ans;</span><br><span class="line"> zt>>=<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</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">long</span> <span class="keyword">long</span> ans=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>,&n,&k);</span><br><span class="line"> maxx=(<span class="number">1</span><<n)<span class="number">-1</span>;</span><br><span class="line"> dp[<span class="number">0</span>][<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> rep(i,<span class="number">1</span>,<span class="number">9</span>)<span class="comment">//lie</span></span><br><span class="line"> rep(w,<span class="number">0</span>,k)<span class="comment">//tot num of kings</span></span><br><span class="line"> rep(j,<span class="number">0</span>,maxx)<span class="comment">//condition of this lie</span></span><br><span class="line"> rep(t,<span class="number">0</span>,maxx)<span class="comment">//condition of last lie</span></span><br><span class="line"> <span class="keyword">if</span>(judge(j|t)&&(!(j&t))&&(w-count(j)>=<span class="number">0</span>))</span><br><span class="line"> dp[i][w][j]+=dp[i<span class="number">-1</span>][w-count(j)][t];</span><br><span class="line"> rep(i,<span class="number">0</span>,maxx)</span><br><span class="line"> ans+=dp[n][k][i];</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld"</span>,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><hr><h1>题外话</h1><p>其实状压dp也不一定用二进制</p><p>在qbxt做过一个,我用的三进制,虽然不是正解,但是可以A</p><p>而且状压dp也可以用矩阵加速一波</p></div>]]></content>
<tags>
<tag> 动态规划 </tag>
<tag> 状压dp </tag>
</tags>
</entry>
<entry>
<title>OI做题记录</title>
<link href="/2018/10/09/OI%E5%81%9A%E9%A2%98%E8%AE%B0%E5%BD%95/"/>
<url>/2018/10/09/OI%E5%81%9A%E9%A2%98%E8%AE%B0%E5%BD%95/</url>
<content type="html"><![CDATA[<p>不定期更新(咕咕咕)</p><a id="more"></a><p>CF492C Vanya and Exams 贪心</p><p>[SCOI2005]互不侵犯 状压dp</p><p>Hanoi双塔问题 高精+递推</p><p>luogu P1439 【模板】最长公共子序列 LIS(n log n)+离散化</p><p>奶酪 并查集</p><p>luogu P1784 数独 搜索</p><p>bzoj 3251: 树上三角形 暴力+找规律</p><p>Mayan游戏 : dfs,模拟</p><p>luogu P3916 图的遍历 : 搜索 记忆化</p><p>bzoj 2456 众数(mode) : 神奇操作</p><p>luogu P3368 【模板】树状数组 2 : 树状数组维护差分数组</p><p>luogu P2801 教主的魔法 : 线段树</p><p>考试:</p><p> 婆罗门的山区火箭运输 生成树</p><p> 婆罗门的井字棋 模拟</p><p> 婆罗门的城市斗鸡 数学,模拟</p><p>[USACO09NOV]找工就业Job Hunt spfa,判环</p><p>[USACO08OCT]打井Watering Hole 生成树,贪心</p><p>[USACO14FEB]路障Roadblock , luogu P1186 玛丽卡 : 暴力枚举,spfa</p><p>最接近神的人_NOI导刊2010提高(02) 逆序对,树状数组/归并排序</p><p>nowcoder 体积数 暴力枚举+快速幂+树状数组统计</p><p>[NOIP2007]树网的核 dfs,树的直径,暴力,floyd,尺取</p><p>[NOIP2008]双栈排序 dfs,二分图,并查集</p><p>luogu P4549 【模板】裴蜀定理 gcd,裴蜀定理</p><p>luogu P1119 灾后重建 Floyd的深入理解</p><p>[NOIP1999]旅行家的预算 堆,贪心</p><p>[NOIP2008]传纸条 记忆化搜索,dp</p><p>P1601 A+B Problem(高精) </p><p>组合数问题 前缀和,杨辉三角</p><p>方格取数 记忆化搜索,dp</p><p>P1341 无序字母对 dfs,欧拉路径/欧拉回路</p><p>矩阵取数游戏 记忆化搜索,dp,高精(__int128水过)</p><p>国王游戏 基于交换的贪心(然而__int128好像水不过,60分)</p>]]></content>
<tags>
<tag> 日志 </tag>
</tags>
</entry>
<entry>
<title>NOIp提高模拟赛1</title>
<link href="/2018/09/09/NOIp%E6%8F%90%E9%AB%98%E6%A8%A1%E6%8B%9F%E8%B5%9B1/"/>
<url>/2018/09/09/NOIp%E6%8F%90%E9%AB%98%E6%A8%A1%E6%8B%9F%E8%B5%9B1/</url>
<content type="html"><![CDATA[<h2>A</h2><h2 class="subject-item-title">题目描述<br><a href="javascript:void(0);" class="icon-fullscreen js-full-question" title="全屏查看题目" target="_blank" rel="noopener"></a><br><a href="javascript:void(0);" class="icon-resize-small js-small-question" style="display:none;" title="收起全屏,编写代码" target="_blank" rel="noopener"></a><br></h2><br><div class="subject-describe"><br><div style="margin-bottom:10px;"><div> 小N得到了一个非常神奇的序列A。<a id="more"></a>这个序列长度为N,下标从1开始。A的一个子区间对应一个序列,可以由数对[l,r]表示,代表A[l], A[l + 1], …, A[r]这段数。对于一个序列B[1], B[2], …, B[k],定义B的中位数如下: </div> <div> 1. 先对B排序。得到新的序列C。 </div> <div> 2. 假如k是奇数,那么中位数为<img src="https://uploadfiles.nowcoder.com/files/20180827/306603_1535370498644_equation?tex=C%5B%5Cfrac%7Bk%2B1%7D%7B2%7D%5D">。假如k为偶数,中位数为<img src="https://uploadfiles.nowcoder.com/files/20180827/306603_1535370528061_equation?tex=C%5B%5Cfrac%7Bk%7D%7B2%7D%5D">。 </div> <div> 对于A的所有的子区间,小N可以知道它们对应的中位数。现在小N想知道,所有长度>=Len的子区间中,中位数最大可以是多少。 </div></div><br><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输入描述:</h2><br><pre>第一行输入两个数N,Len。<br>第二行输入序列A,第i个数代表A[i]。</pre><br><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输出描述:</h2><br><pre>一行一个整数,代表所有长度>=Len的子区间中,最大的中位数。</pre><br><!-- <br/><h5 style="font-size:14px;font-weight:bold;">输入例子:</h5><pre>11 34864 8684 9511 8557 1122 1234 953 9819 101 1137 1759 </pre><br/><h5 style="font-size:14px;font-weight:bold;">输出例子:</h5><pre>8684</pre> --><br><div class="question-oi"><br><div class="question-oi-hd"><br>示例1<br></div><br><div class="question-oi-bd"><br><div class="question-oi-mod"><br><h2>输入</h2><br><a class="code-copy-btn js-clipboard" href="javascript:void(0);" data-clipboard-text="11 34864 8684 9511 8557 1122 1234 953 9819 101 1137 1759 " target="_blank" rel="noopener">复制</a><br><div class="question-oi-cont"><br><pre>11 3<br>4864 8684 9511 8557 1122 1234 953 9819 101 1137 1759 </pre><br></div><br></div><br><div class="question-oi-mod"><br><h2>输出</h2><br><a class="code-copy-btn js-clipboard" href="javascript:void(0);" data-clipboard-text="8684" target="_blank" rel="noopener">复制</a><br><div class="question-oi-cont"><br><pre>8684</pre><br></div><br></div><br></div><br></div><br><h2 style="font-size:14px;font-weight:bold;color:#34495e;">备注:</h2><br><pre>数据范围:<br>30%: n <= 200<br>60%: n <= 2000<br>另外有20%:不超过50个不同的数<br>100%:1<=Len<=n<=10^5, 1 <= a[i] <= 10^9</pre><br></div><br><br><br>—<br><br>考试的时候想到了一个错误的做法,只考虑len==Len的情况,然后找中位数最大值<br><br>主要是得出了错误的结论:一个较大数据在一个较大区间内的位置不会比在一个较小区间里的位置更居中。<br><br>但是这个结论我不会证伪<br><br>然后就这样,然后主席树求区间k/2或(k+1)/2小值,50分<br><br>那么下面来说一下正解<br><br>—<br><br>让中位数最大,可以二分答案<br><br>那么怎么验证答案是否可行?<br><br>比如说我们二分到的为x,那么我们就让序列中>=x的数=1,<x的数=-1<br><br>这样对于一段序列,<br><br>当且仅当当前序列的和大于0时,这段序列的中位数才大于等于x<br><br>其实这个就可以成为一个区间长度>=len的最大子段和<br><br>然后复杂度就是O(n log max{a_i})<br><br>显然可以过<br><br>Code<br><br><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> For(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN 100010</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> mn,ans,n,a[MAXN],b[MAXN],len;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">judge</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"> mn=<span class="number">2147483647</span>;</span><br><span class="line"> For(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(a[i]>=x)</span><br><span class="line"> b[i]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> b[i]=<span class="number">-1</span>;</span><br><span class="line"> b[i]+=b[i<span class="number">-1</span>];</span><br><span class="line"> <span class="keyword">if</span>(i>=len)</span><br><span class="line"> {</span><br><span class="line"> mn=min(mn,b[i-len]);</span><br><span class="line"> <span class="keyword">if</span>(b[i]-mn><span class="number">0</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</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 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> mid,l=<span class="number">2147483647</span>,r=<span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d %d"</span>,&n,&len);</span><br><span class="line"> For(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&a[i]);</span><br><span class="line"> l=min(l,a[i]);r=max(r,a[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(l<=r)</span><br><span class="line"> {</span><br><span class="line"> mid=((l+r)>><span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(judge(mid))</span><br><span class="line"> {</span><br><span class="line"> l=mid+<span class="number">1</span>;</span><br><span class="line"> ans=mid;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> r=mid<span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>,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><br><br><h2>B</h2><h2>C</h2><h2>总结</h2><p>A题提供了一个以前没见过的中位数思路,最大子段和。。。</p>]]></content>
<tags>
<tag> 动态规划 </tag>
<tag> 比赛 </tag>
<tag> 二分 </tag>
</tags>
</entry>
<entry>
<title>NOIp pj模拟赛1</title>
<link href="/2018/09/09/NOIp%E6%99%AE%E5%8F%8A%E6%A8%A1%E6%8B%9F%E8%B5%9B1/"/>
<url>/2018/09/09/NOIp%E6%99%AE%E5%8F%8A%E6%A8%A1%E6%8B%9F%E8%B5%9B1/</url>
<content type="html"><![CDATA[<h2>A</h2><div style="margin-bottom:10px;"><div> 小A刚考完大学考试。现在已经出了n门课的成绩,他想自己先算一下这些课的绩点是多少。设第i门课的他拿到的绩点是gpa<sub>i</sub>,而这门课的学分是sc<sub>i</sub>,那么他的总绩点用下面的公式计算: </div> <div> <img src="https://uploadfiles.nowcoder.com/files/20180902/306603_1535894110324_equation?tex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5En%20gpa_i%5Ctimes%20sc_i%7D%7B%5Csum_%7Bi%3D1%7D%5En%20sc_i%7D">,<br> </div> <div> 换言之,设S为<span>sc</span><sub>i</sub>的和,T为gpa<sub>i</sub>与sc<sub>i</sub>的乘积的和。那么小A的绩点就是T除以S的值。 </div></div><a id="more"></a><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输入描述:</h2><pre><div>第一行一个整数n。</div><div>接下来n行,每行两个数gpa<sub>i</sub>和sc<sub>i。</sub></div></pre><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输出描述:</h2><pre>输出一行一个实数,表示小A的绩点。输出四舍五入保留1位小数。</pre><hr><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> For(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">isdigit</span>(c))</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> n,m,t,mx,my,q;</span><br><span class="line"><span class="keyword">char</span> c;</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">long</span> <span class="keyword">long</span> <span class="keyword">int</span> sx,sy;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld %lld %lld"</span>,&n,&m,&t);</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">while</span>(c!=<span class="string">' '</span>&&c!=<span class="string">'\n'</span>&&c!=<span class="string">'\r'</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'U'</span>)</span><br><span class="line"> mx+=<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'L'</span>)</span><br><span class="line"> my+=<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'R'</span>)</span><br><span class="line"> my+=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> mx+=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> mx=((mx%n)*(t%n))%n;</span><br><span class="line"> my=((my%m)*(t%m))%m;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&q);</span><br><span class="line"> For(i,<span class="number">1</span>,q)</span><br><span class="line"> {</span><br><span class="line"> sx=read();sy=read();</span><br><span class="line"> sx=(sx+mx)%n;</span><br><span class="line"> sy=(sy+my)%m;</span><br><span class="line"> <span class="keyword">if</span>(sx<=<span class="number">0</span>)sx+=n;</span><br><span class="line"> <span class="keyword">if</span>(sy<=<span class="number">0</span>)sy+=m;</span><br><span class="line"> <span class="comment">//if(!sx)sx=n;</span></span><br><span class="line"> <span class="comment">//if(!sy)sy=m;</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld %lld\n"</span>,sx,sy);</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><h2>B</h2><p><div style="margin-bottom:10px;"><div> 小A站在一个巨大的棋盘上。这个棋盘可以看成是一个网格图。这个网格图的大小为n<em>m。左上角坐标为(1,1),右下角坐标为(n,m)。这个棋盘很特别,他每行每列都是一个环。具体来说,当小A站在第一行,他往上走的时候,他会走到第n行,站在第n行往下走会走到第一行。对于第一列和第m列类似。小A在棋盘上可以上下左右走,假设他站在位置(i,j),向上走,会走到(i-1,j),向下回到(i+1,j),向左到(i,j-1),向右到(i,j+1)。注意由于棋盘是循环的,他不会走出这个棋盘。 </em></div> <div> 现在小A有一个固定的行走序列S,代表他每一步走的方向,U代表向上,D代表向下,L代表向左,R代表向右。比如小A一开始在(1,1),棋盘大小为34。行走序列为UULRD。那么他会依次经过(3,1),(2,1),(2,4),(2,1),(3,1)。但小A觉得只走一遍S太无聊,因此他会重复走这个序列T次。比如上面的例子,当T=2时,真正的行走序列为UULRDUULRD。 </div> <div> 小A有q个备选的起点位置。他一开始先给定你棋盘大小与行走序列,对于每个起点位置,他想知道,他沿着序列走,最终会走到哪个位置停下。 </div></div></p><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输入描述:</h2><br><pre>第一行三个整数n,m,T。<br>接下来一行一个字符串S,代表行走序列。注意行走序列在真实走的时候要重复T次。<br>接下来一个整数q。<br>接下来q行,每行两个整数x,y,代表小A的一个备选起点。</pre><br><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输出描述:</h2><br><pre>输出q行,每行两个整数,输出对于这个起点,最后的终点是哪里。</pre><br><br><div class="question-oi"><br><div class="question-oi-hd"><br>示例1<br></div><br><div class="question-oi-bd"><br><div class="question-oi-mod"><br><h2>输入</h2><br><a class="code-copy-btn js-clipboard" href="javascript:void(0);" data-clipboard-text="3 6 4DUUUDLLLLR33 22 51 4" target="_blank" rel="noopener">复制</a><br><div class="question-oi-cont"><br><pre>3 6 4<br>DUUUDLLLLR<br>3<br>3 2<br>2 5<br>1 4<br></pre><br></div><br></div><br><div class="question-oi-mod"><br><h2>输出</h2><br><a class="code-copy-btn js-clipboard" href="javascript:void(0);" data-clipboard-text="2 21 53 4" target="_blank" rel="noopener">复制</a><br><div class="question-oi-cont"><br><pre>2 2<br>1 5<br>3 4<br></pre><br></div><br></div><br></div><br></div><br><h2 style="font-size:14px;font-weight:bold;color:#34495e;">备注:</h2><br><pre>20%: |S| <em> T <= 10^6, q = 1<br>40%: |S| </em> T <= 10^6, q <= 10^5<br>60%: |S|, T <= 10^5, q <= 10^5<br>100%: 1 <= T,n,m <= 10^9, 1 <= x <= n, 1 <= y <= m. 1<= q, |S| <= 10^5<br>其中|S|代表S的长度。</pre><br>—<br><br>逐个字符读入,然后把所有操作加起来$<em>t %n$和$</em>t %m$<br><br>然后再对每个坐标加上操作%n %m<br><br>然后特判一下非正数处理即可<br><br>$$O(|S|+q)$$<br><br><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> For(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">isdigit</span>(c))</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;a+=(c-<span class="string">'0'</span>);c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> n,m,t,mx,my,q;</span><br><span class="line"><span class="keyword">char</span> c;</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">long</span> <span class="keyword">long</span> <span class="keyword">int</span> sx,sy;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld %lld %lld"</span>,&n,&m,&t);</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">while</span>(c!=<span class="string">' '</span>&&c!=<span class="string">'\n'</span>&&c!=<span class="string">'\r'</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'U'</span>)</span><br><span class="line"> mx+=<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'L'</span>)</span><br><span class="line"> my+=<span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(c==<span class="string">'R'</span>)</span><br><span class="line"> my+=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> mx+=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> mx=((mx%n)*(t%n))%n;</span><br><span class="line"> my=((my%m)*(t%m))%m;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%lld"</span>,&q);</span><br><span class="line"> For(i,<span class="number">1</span>,q)</span><br><span class="line"> {</span><br><span class="line"> sx=read();sy=read();</span><br><span class="line"> sx=(sx+mx)%n;</span><br><span class="line"> sy=(sy+my)%m;</span><br><span class="line"> <span class="keyword">if</span>(sx<=<span class="number">0</span>)sx+=n;</span><br><span class="line"> <span class="keyword">if</span>(sy<=<span class="number">0</span>)sy+=m;</span><br><span class="line"> <span class="comment">//if(!sx)sx=n;</span></span><br><span class="line"> <span class="comment">//if(!sy)sy=m;</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld %lld\n"</span>,sx,sy);</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><br><br><h2>C</h2><p><div class="subject-describe"></div></p><p><div style="margin-bottom:10px;"><div> 小A有一个只包含左右括号的字符串S。但他觉得这个字符串不够美观,因为它不是一个合法的括号串。一个合法的括号串是这样定义的: </div> <div> 1. ()是合法的括号串 </div> <div> 2. 若A是合法的括号串,则(A)则是合法的括号串 </div> <div> 3. 若A,B是合法的括号串,则AB也是合法的括号串。 </div> <div> <br> </div> <div> 小A现在希望删掉S中若干个字符,使得剩下的字符串是一个合法的括号串。小A想知道有多少不同的方案。两个方案是不同的,当且仅当他们删除的位置不同。比如当S是(()时,有两种方案。分别是删掉第一个位置,或是删掉第二个位置。 </div></div></p><p></p><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输入描述:</h2><p></p><p><pre>第一行一个整数n,代表S的长度。<br>第二行输入n个字符,字符要么是(,要么是)。代表串S。</pre></p><p></p><h2 style="font-size:14px;font-weight:bold;color:#34495e;">输出描述:</h2><p></p><p><pre>一行一个整数,代表不同的方案数。答案对10^9+7取模。</pre><br><!– <br></p><p></p><h5 style="font-size:14px;font-weight:bold;">输入例子:</h5><p></p><p><pre>8<br>)(()(())</pre><br><br></p><p></p><h5 style="font-size:14px;font-weight:bold;">输出例子:</h5><p></p><p><pre>30<br></pre><br> –></p><p><div class="question-oi"></div></p><p><div class="question-oi-hd"><br>示例1<br></div></p><p><div class="question-oi-bd"></div></p><p><div class="question-oi-mod"></div></p><p></p><h2>输入</h2><br><a class="code-copy-btn js-clipboard" href="javascript:void(0);" data-clipboard-text="8)(()(())" target="_blank" rel="noopener">复制</a><p></p><p><div class="question-oi-cont"></div></p><p><pre>8<br>)(()(())</pre><br><br></p><p><div class="question-oi-mod"></div></p><p></p><h2>输出</h2><br><a class="code-copy-btn js-clipboard" href="javascript:void(0);" data-clipboard-text="30" target="_blank" rel="noopener">复制</a><p></p><p><div class="question-oi-cont"></div></p><p><pre>30<br></pre><br><br><br></p><h2 id=""><a href="#" class="headerlink" title=""></a></h2><p>看得出来是dp题推不出式子。。。前两个题做半小时这个题做三小时都没做出来。。。最后连暴力都没打上。。。</p><p>我们首先考虑爆搜,枚举每一位上括号是否被删去,然后对于每一次搜索到的结果judge一下,具体就是用栈来搞,这样的时间复杂度显然是$O(2^n * n)$的,显然过不了</p><p>那我们再来看之后用栈验证的过程,可以注意到和当前剩下的未匹配的左括号数有密切关系(爆搜验证的时候就是如果是左括号就++a,如果是右括号就–a,如果最后是0那就说明合法),那我们状态中就可以有一个量来描述这个东西</p><p>我们用$f_{i,j}$来表示考虑第i位有j个未匹配的左括号的方案数,那么</p><p>如果s[i]是左括号,那么f[i]<!--???-->[j+1]=sigma f[0~i-1]<!--???-->[j]</p><p>如果是右括号,那么f[i]<!--???-->[j-1]=sigma f[0~i-1]<!--???-->[j]</p><p>最后的答案是(sigma f[1~i]<!--???-->[j-1] ) -1,不太清楚为什么要-1 。。。</p><p>然后显然就可以用前缀和来优化,前缀和优化之后就只用得到当前i的前一项,可以用一个滚动数组交替滚动</p><p>先给出不加滚动数组的代码</p><figure class="highlight cpp"><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"><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> For(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">int</span> f[<span class="number">10010</span>][<span class="number">10010</span>];</span><br><span class="line"><span class="keyword">int</span> p=<span class="number">1000000007</span>;</span><br><span class="line"><span class="keyword">char</span> a[<span class="number">10010</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">char</span> c;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line"> For(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> a[i]=c;</span><br><span class="line"> }</span><br><span class="line"> f[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> For(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> For(j,<span class="number">0</span>,n)</span><br><span class="line"> {</span><br><span class="line"> f[i][j]=f[i<span class="number">-1</span>][j];</span><br><span class="line"> <span class="keyword">if</span>(a[i]==<span class="string">'('</span>&&j)f[i][j]=(f[i][j]+f[i<span class="number">-1</span>][j<span class="number">-1</span>])%p;</span><br><span class="line"> <span class="keyword">if</span>(a[i]==<span class="string">')'</span>&&j<n)f[i][j]=(f[i][j]+f[i<span class="number">-1</span>][j+<span class="number">1</span>])%p;</span><br><span class="line"> <span class="comment">//cout<<f[i][j]<<'\n';</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span><<(f[n][<span class="number">0</span>]<span class="number">-1</span>+p)%p;</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>稍微改动一下即为AC代码</p><figure class="highlight cpp"><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></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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">define</span> For(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">int</span> f[<span class="number">2</span>][<span class="number">10010</span>];</span><br><span class="line"><span class="keyword">int</span> p=<span class="number">1000000007</span>;</span><br><span class="line"><span class="keyword">char</span> a[<span class="number">10010</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">bool</span> w=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">char</span> c;</span><br><span class="line"><span class="built_in">scanf</span>(<span class="string">"%d"</span>,&n);</span><br><span class="line">For(i,<span class="number">1</span>,n)</span><br><span class="line">{</span><br><span class="line"><span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line">a[i]=c;</span><br><span class="line">}</span><br><span class="line">f[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line">For(i,<span class="number">1</span>,n)</span><br><span class="line">{</span><br><span class="line">For(j,<span class="number">0</span>,n)</span><br><span class="line">{</span><br><span class="line">f[w^<span class="number">1</span>][j]=f[w][j];</span><br><span class="line"><span class="keyword">if</span>(a[i]==<span class="string">'('</span>&&j)f[w^<span class="number">1</span>][j]=(f[w^<span class="number">1</span>][j]+f[w][j<span class="number">-1</span>])%p;</span><br><span class="line"><span class="keyword">if</span>(a[i]==<span class="string">')'</span>&&j<n)f[w^<span class="number">1</span>][j]=(f[w^<span class="number">1</span>][j]+f[w][j+<span class="number">1</span>])%p;</span><br><span class="line">}</span><br><span class="line">w^=<span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="built_in">cout</span><<(f[w][<span class="number">0</span>]<span class="number">-1</span>+p)%p;</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>
<tags>
<tag> 模拟 </tag>
<tag> 动态规划 </tag>
<tag> 比赛 </tag>
</tags>
</entry>
<entry>
<title>主席树入门:静态主席树</title>
<link href="/2018/08/30/%E4%B8%BB%E5%B8%AD%E6%A0%91%E5%85%A5%E9%97%A8%EF%BC%9A%E9%9D%99%E6%80%81%E4%B8%BB%E5%B8%AD%E6%A0%91/"/>
<url>/2018/08/30/%E4%B8%BB%E5%B8%AD%E6%A0%91%E5%85%A5%E9%97%A8%EF%BC%9A%E9%9D%99%E6%80%81%E4%B8%BB%E5%B8%AD%E6%A0%91/</url>
<content type="html"><![CDATA[<h2>问题</h2><p>首先用一个问题来引入:</p><p>给出n个数,m次查询,每次查询一个区间[l,r]中第k大的值</p><a id="more"></a><h2>sol</h2><p>显然可以有一个O(nm)的暴力</p><p>那么我们考虑用线段树来做这道题</p><p>用裸的权值线段树可以支持询问数列中的第k大值,我们询问[l,r]中第k大的值,其实可以调用插入第l个数之前的权值线段树和插入第r个数之后的权值线段树,用它们的权值做差,其实就是一颗[l,r]的权值线段树</p><p>很容易想到可以构建n颗权值线段树,不过空间肯定会炸</p><p>那么我们考虑只更新那些有插入的点,没有更新到的点直接用上个版本的点来代替,这样每次插入一个数的新增空间是log n</p><p>这里就不能用之前普通的线段树那种左儿子(x<<1)右儿子((x<<1)|1)的方式来存储了</p><p>当然,一般还要把这些数离散化一下,如果数的范围小也可以不用离散化</p><p>我们就预先开好一个内存池一样的东西,新开点时直接从这里取就好了,new好像比较慢</p><p><a href="https://www.luogu.org/problemnew/show/P3834" target="_blank" rel="noopener">模板题</a>></p><figure class="highlight cpp"><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><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><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"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cmath></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><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> For(i,l,r) for(int i=l;i<=r;++i)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAXN (200010)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> mid ((l+r)>>1)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">read</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line"> <span class="keyword">bool</span> t=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> a=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>((c=getchar())==<span class="string">' '</span>||c==<span class="string">'\n'</span>||c==<span class="string">'\r'</span>);</span><br><span class="line"> <span class="keyword">if</span>(c==<span class="string">'-'</span>)</span><br><span class="line"> {</span><br><span class="line"> t=<span class="number">1</span>;</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">isdigit</span>(c))</span><br><span class="line"> {</span><br><span class="line"> a*=<span class="number">10</span>;</span><br><span class="line"> a+=(c-<span class="string">'0'</span>);</span><br><span class="line"> c=getchar();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> a*(t?<span class="number">-1</span>:<span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> ls[MAXN<<<span class="number">5</span>],rs[MAXN<<<span class="number">5</span>],sum[MAXN<<<span class="number">5</span>];</span><br><span class="line"><span class="keyword">int</span> root[MAXN],cnt;<span class="comment">//第i个版本的根节点,节点数</span></span><br><span class="line"><span class="keyword">int</span> n,m,a[MAXN],hash[MAXN],len,ans;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getHash</span><span class="params">(<span class="keyword">const</span> <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">return</span> lower_bound(hash+<span class="number">1</span>,hash+len+<span class="number">1</span>,x)-hash;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">build</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r,<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">if</span>(l==r)</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> ls[x]=++cnt;</span><br><span class="line"> build(l,mid,cnt);</span><br><span class="line"> rs[x]=++cnt;</span><br><span class="line"> build(mid+<span class="number">1</span>,r,cnt);</span><br><span class="line"> <span class="comment">//printf("[l:%d r:%d x:%d ls:%d rs:%d]\n",l,r,x,ls[x],rs[x]);</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">(<span class="keyword">int</span> w,<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> x,<span class="keyword">int</span> his)</span><span class="comment">//要修改的值 左端 右端 要修改的点 要修改的点的前一个版本 </span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(l==r)</span><br><span class="line"> {sum[x]=sum[his]+<span class="number">1</span>;<span class="keyword">return</span>;}<span class="comment">//AAAA</span></span><br><span class="line"> ls[x]=ls[his];rs[x]=rs[his];sum[x]=sum[his]+<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(w<=mid)</span><br><span class="line"> {</span><br><span class="line"> ls[x]=++cnt;</span><br><span class="line"> update(w,l,mid,cnt,ls[his]);</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"> rs[x]=++cnt;</span><br><span class="line"> update(w,mid+<span class="number">1</span>,r,cnt,rs[his]);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//sum[x]=sum[ls[x]]+sum[rs[x]];</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">ask</span><span class="params">(<span class="keyword">int</span> l,<span class="keyword">int</span> r,<span class="keyword">int</span> rx,<span class="keyword">int</span> lx,<span class="keyword">int</span> k)</span><span class="comment">//当前点左 当前点右 当前右点 当前左点 第k小 </span></span></span><br><span class="line"><span class="function"></span>{</span><br><span class="line"> <span class="keyword">if</span>(l==r)</span><br><span class="line"> {</span><br><span class="line"> ans=hash[l];</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(k<=sum[ls[rx]]-sum[ls[lx]])<span class="comment">//在左边 </span></span><br><span class="line"> ask(l,mid,ls[rx],ls[lx],k);</span><br><span class="line"> <span class="keyword">else</span><span class="comment">//在右边 </span></span><br><span class="line"> ask(mid+<span class="number">1</span>,r,rs[rx],rs[lx],k-sum[ls[rx]]+sum[ls[lx]]);</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> tx,ty,tk;</span><br><span class="line"> n=read();m=read();</span><br><span class="line"> For(i,<span class="number">1</span>,n)</span><br><span class="line"> a[i]=read();</span><br><span class="line"> <span class="built_in">memcpy</span>(hash,a,<span class="keyword">sizeof</span> a);</span><br><span class="line"> sort(hash+<span class="number">1</span>,hash+n+<span class="number">1</span>);</span><br><span class="line"> len=unique(hash+<span class="number">1</span>,hash+n+<span class="number">1</span>)-hash<span class="number">-1</span>;</span><br><span class="line"> root[<span class="number">0</span>]=++cnt;</span><br><span class="line"> build(<span class="number">1</span>,len,cnt);</span><br><span class="line"> For(i,<span class="number">1</span>,n)</span><br><span class="line"> {</span><br><span class="line"> root[i]=++cnt;</span><br><span class="line"> update(getHash(a[i]),<span class="number">1</span>,len,cnt,root[i<span class="number">-1</span>]);</span><br><span class="line"> }</span><br><span class="line"> For(i,<span class="number">1</span>,m)</span><br><span class="line"> {</span><br><span class="line"> tx=read();ty=read();tk=read();</span><br><span class="line"> <span class="keyword">if</span>(tx==ty)</span><br><span class="line"> {<span class="built_in">printf</span>(<span class="string">"%d\n"</span>,a[tx]);<span class="keyword">continue</span>;}</span><br><span class="line"> ask(<span class="number">1</span>,len,root[ty],root[tx<span class="number">-1</span>],tk);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,ans);</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>
<tags>
<tag> 数据结构 </tag>
<tag> 主席树 </tag>
</tags>
</entry>
<entry>
<title>博客迁移</title>
<link href="/2018/08/30/%E5%8D%9A%E5%AE%A2%E8%BF%81%E7%A7%BB/"/>
<url>/2018/08/30/%E5%8D%9A%E5%AE%A2%E8%BF%81%E7%A7%BB/</url>
<content type="html"><![CDATA[<p>其他博客地址:</p><p>csdn:<a href="https://blog.csdn.net/suxuyu01" target="_blank" rel="noopener">https://blog.csdn.net/suxuyu01</a></p><p>Luogu:<a href="https://www.luogu.org/blog/lqyzsun/" target="_blank" rel="noopener">https://www.luogu.org/blog/lqyzsun/</a></p><p>一般情况下不会再使用,如果在除此博客之外的地方发了我认为有价值的新文章也搬到这里来</p>]]></content>
<tags>
<tag> 日志 </tag>
</tags>
</entry>
</search>