forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
1856 lines (1685 loc) · 79.5 KB
/
common.go
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package copypasta
import (
"math"
"math/bits"
"math/rand"
"reflect"
"slices"
"sort"
"time"
"unsafe"
)
// Competitive Programming Roadmap (target: [gray, blue]) https://codeforces.com/blog/entry/111099
// A way to Practice Competitive Programming : From Rating 1000 to 2400+ https://codeforces.com/blog/entry/66909
// https://cin.ufpe.br/~fbma/Crack/%5BTutorial%5D%20A%20Way%20to%20Practice%20Competitive%20Programming.pdf
// 解决问题的一般方法 https://codeforces.com/blog/entry/92248?#comment-809401
// General ideas https://codeforces.com/blog/entry/48417
// 从特殊到一般:尝试修改条件或缩小题目的数据范围,先研究某个特殊情况下的思路,然后再逐渐扩大数据范围来思考怎么改进算法
// 重谈主定理及其证明 https://www.luogu.com.cn/blog/GJY-JURUO/master-theorem
/*
力扣题目分类汇总
https://leetcode.cn/circle/article/04PVPY/
https://leetcode.cn/circle/discuss/vEFf96/
枚举右,维护左
- [1. 两数之和](https://leetcode.cn/problems/two-sum/)
- https://codeforces.com/problemset/problem/702/B
- [219. 存在重复元素 II](https://leetcode.cn/problems/contains-duplicate-ii/)
- [121. 买卖股票的最佳时机](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/)
- [1512. 好数对的数目](https://leetcode.cn/problems/number-of-good-pairs/) 1161
- [2815. 数组中的最大数对和](https://leetcode.cn/problems/max-pair-sum-in-an-array/) 1295
- [2748. 美丽下标对的数目](https://leetcode.cn/problems/number-of-beautiful-pairs/) 1301
- [2342. 数位和相等数对的最大和](https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/) 1309
- [1010. 总持续时间可被 60 整除的歌曲](https://leetcode.cn/problems/pairs-of-songs-with-total-durations-divisible-by-60/) 1377
- [2874. 有序三元组中的最大值 II](https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-ii/) 1583
巧妙安排更新顺序,使得 ans,pre_max 只能使用之前的值,从而符合 i<j<k 的要求
- [454. 四数相加 II](https://leetcode.cn/problems/4sum-ii/)
- https://leetcode.cn/problems/find-polygon-with-the-largest-perimeter/
https://leetcode.com/discuss/interview-question/3685049/25-variations-of-Two-sum-question
哈希表
- [2260. 必须拿起的最小连续卡牌数](https://leetcode.cn/problems/minimum-consecutive-cards-to-pick-up/) 1365
- [982. 按位与为零的三元组](https://leetcode.cn/problems/triples-with-bitwise-and-equal-to-zero/) 2085
- [面试题 16.21. 交换和](https://leetcode.cn/problems/sum-swap-lcci/)
前缀和
- [1732. 找到最高海拔](https://leetcode.cn/problems/find-the-highest-altitude/)
- [303. 区域和检索 - 数组不可变](https://leetcode.cn/problems/range-sum-query-immutable/)
- [1310. 子数组异或查询](https://leetcode.cn/problems/xor-queries-of-a-subarray/)
- [2615. 等值距离和](https://leetcode.cn/problems/sum-of-distances/) 1793
- [2602. 使数组元素全部相等的最少操作次数](https://leetcode.cn/problems/minimum-operations-to-make-all-array-elements-equal/) 1903
- [2955. Number of Same-End Substrings](https://leetcode.cn/problems/number-of-same-end-substrings/)(会员题)
前缀和+哈希表(双变量思想)
- [930. 和相同的二元子数组](https://leetcode.cn/problems/binary-subarrays-with-sum/) 1592 *同 560
- [560. 和为 K 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/)
- [1524. 和为奇数的子数组数目](https://leetcode.cn/problems/number-of-sub-arrays-with-odd-sum/) 1611
- [974. 和可被 K 整除的子数组(数目)](https://leetcode.cn/problems/subarray-sums-divisible-by-k/) 1676
- 变形:乘积可以被 k 整除
- a[i] = gcd(a[i], k) 之后窗口乘积是 k 的倍数就行,不会乘爆
- [523. 连续的子数组和(长度至少为 2 且可被 K 整除)](https://leetcode.cn/problems/continuous-subarray-sum/)
- [1590.(移除最短子数组)使数组和能被 P 整除](https://leetcode.cn/problems/make-sum-divisible-by-p/) 2039
- [525. 连续数组](https://leetcode.cn/problems/contiguous-array/) *转换
- [1124. 表现良好的最长时间段](https://leetcode.cn/problems/longest-well-performing-interval/) 1908 *转换
- [2488. 统计中位数为 K 的子数组](https://leetcode.cn/problems/count-subarrays-with-median-k/) 1999 *转换
- [2949. 统计美丽子字符串 II](https://leetcode.cn/problems/count-beautiful-substrings-ii/)
- [面试题 17.05. 字母与数字](https://leetcode.cn/problems/find-longest-subarray-lcci/)
- [1983. 范围和相等的最宽索引对](https://leetcode.cn/problems/widest-pair-of-indices-with-equal-range-sum/)(会员题)
- [2489. 固定比率的子字符串数](https://leetcode.cn/problems/number-of-substrings-with-fixed-ratio/)(会员题)
https://atcoder.jp/contests/abc233/tasks/abc233_d
交错前缀和 https://codeforces.com/contest/1915/problem/E
前缀和思想 LC1523 https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/
有点数形结合 https://codeforces.com/problemset/problem/1748/C
前缀和的前缀和(二重前缀和)
LC2281 https://leetcode.cn/problems/sum-of-total-strength-of-wizards/
https://atcoder.jp/contests/abc058/tasks/arc071_b
前缀和+异或
- [1177. 构建回文串检测](https://leetcode.cn/problems/can-make-palindrome-from-substring/)
- [1371. 每个元音包含偶数次的最长子字符串](https://leetcode.cn/problems/find-the-longest-substring-containing-vowels-in-even-counts/)
- [1542. 找出最长的超赞子字符串](https://leetcode.cn/problems/find-longest-awesome-substring/)
- [1915. 最美子字符串的数目](https://leetcode.cn/problems/number-of-wonderful-substrings/),[题解](https://leetcode.cn/problems/number-of-wonderful-substrings/solution/qian-zhui-he-chang-jian-ji-qiao-by-endle-t57t/)
https://atcoder.jp/contests/abc295/tasks/abc295_d
模 3 & 字符集大小为 n https://codeforces.com/problemset/problem/1418/G
https://leetcode.cn/problems/find-longest-subarray-lcci/
https://codeforces.com/problemset/problem/1296/C
前后缀分解(右边数字为难度分)
- [42. 接雨水](https://leetcode.cn/problems/trapping-rain-water/)([视频讲解](https://www.bilibili.com/video/BV1Qg411q7ia/?t=3m05s))
注:带修改的接雨水 https://codeforces.com/gym/104821/problem/M
- https://www.zhihu.com/question/627281278/answer/3280684055
- [2256. 最小平均差](https://leetcode.cn/problems/minimum-average-difference/) 1395
- [2909. 元素和最小的山形三元组 II](https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-ii/) 1479
- [2483. 商店的最少代价](https://leetcode.cn/problems/minimum-penalty-for-a-shop/) 1495
- [2874. 有序三元组中的最大值 II](https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-ii/) 1583
- [2420. 找到所有好下标](https://leetcode.cn/problems/find-all-good-indices/) 1695
- [1671. 得到山形数组的最少删除次数](https://leetcode.cn/problems/minimum-number-of-removals-to-make-mountain-array/) 1913 *DP
- [238. 除自身以外数组的乘积](https://leetcode.cn/problems/product-of-array-except-self/) ~2000
- [2906. 构造乘积矩阵](https://leetcode.cn/problems/construct-product-matrix/) 2075
- [2167. 移除所有载有违禁货物车厢所需的最少时间](https://leetcode.cn/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/) 2219 *DP
- [2484. 统计回文子序列数目](https://leetcode.cn/problems/count-palindromic-subsequences/) 2223
- [2565. 最少得分子序列](https://leetcode.cn/problems/subsequence-with-the-minimum-score/) 2432
- [2552. 统计上升四元组](https://leetcode.cn/problems/count-increasing-quadruplets/) 2433
- [3003. 执行操作后的最大分割数量](https://leetcode.cn/problems/maximize-the-number-of-partitions-after-operations/) 3039
#### 定长滑动窗口(右边数字为难度分)
- [1456. 定长子串中元音的最大数目](https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/) 1263
- [2269. 找到一个数字的 K 美丽值](https://leetcode.cn/problems/find-the-k-beauty-of-a-number/) 1280
- [1984. 学生分数的最小差值](https://leetcode.cn/problems/minimum-difference-between-highest-and-lowest-of-k-scores/) 1306
- [643. 子数组最大平均数 I](https://leetcode.cn/problems/maximum-average-subarray-i/)
- [1343. 大小为 K 且平均值大于等于阈值的子数组数目](https://leetcode.cn/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/) 1317
- [2090. 半径为 k 的子数组平均值](https://leetcode.cn/problems/k-radius-subarray-averages/) 1358
- [2379. 得到 K 个黑块的最少涂色次数](https://leetcode.cn/problems/minimum-recolors-to-get-k-consecutive-black-blocks/) 1360
- [1052. 爱生气的书店老板](https://leetcode.cn/problems/grumpy-bookstore-owner/) 1418
- [2841. 几乎唯一子数组的最大和](https://leetcode.cn/problems/maximum-sum-of-almost-unique-subarray/) 1546
- [2461. 长度为 K 子数组中的最大和](https://leetcode.cn/problems/maximum-sum-of-distinct-subarrays-with-length-k/) 1553
- [1423. 可获得的最大点数](https://leetcode.cn/problems/maximum-points-you-can-obtain-from-cards/) 1574
- [2134. 最少交换次数来组合所有的 1 II](https://leetcode.cn/problems/minimum-swaps-to-group-all-1s-together-ii/) 1748
- [2653. 滑动子数组的美丽值](https://leetcode.cn/problems/sliding-subarray-beauty/) 1786
- [567. 字符串的排列](https://leetcode.cn/problems/permutation-in-string/)
- [438. 找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)
- [2156. 查找给定哈希值的子串](https://leetcode.cn/problems/find-substring-with-given-hash-value/) 2063
- [2953. 统计完全子字符串](https://leetcode.cn/problems/count-complete-substrings/) 2449 *分组循环
- [346. 数据流中的移动平均值](https://leetcode.cn/problems/moving-average-from-data-stream/)(会员题)
- [1100. 长度为 K 的无重复字符子串](https://leetcode.cn/problems/find-k-length-substrings-with-no-repeated-characters/)(会员题)
- [1852. 每个子数组的数字种类数](https://leetcode.cn/problems/distinct-numbers-in-each-subarray/)(会员题)
- [2067. 等计数子串的数量](https://leetcode.cn/problems/number-of-equal-count-substrings/)(会员题)
- [2107. 分享 K 个糖果后独特口味的数量](https://leetcode.cn/problems/number-of-unique-flavors-after-sharing-k-candies/)(会员题)
https://codeforces.com/problemset/problem/69/E 1800
#### 不定长滑动窗口(求最长/最大)
- [3. 无重复字符的最长子串](https://leetcode.cn/problems/longest-substring-without-repeating-characters/)
- 翻转至多一个任意子串后的无重复字符的最长子串 https://codeforces.com/contest/1234/problem/F
- [1493. 删掉一个元素以后全为 1 的最长子数组](https://leetcode.cn/problems/longest-subarray-of-1s-after-deleting-one-element/) 1423
- [1208. 尽可能使字符串相等](https://leetcode.cn/problems/get-equal-substrings-within-budget/) 1497
- [2730. 找到最长的半重复子字符串](https://leetcode.cn/problems/find-the-longest-semi-repetitive-substring/) 1502
- [904. 水果成篮](https://leetcode.cn/problems/fruit-into-baskets/) 1516
- [1695. 删除子数组的最大得分](https://leetcode.cn/problems/maximum-erasure-value/) 1529
- [2958. 最多 K 个重复元素的最长子数组](https://leetcode.cn/problems/length-of-longest-subarray-with-at-most-k-frequency/) 1535
- [2024. 考试的最大困扰度](https://leetcode.cn/problems/maximize-the-confusion-of-an-exam/) 1643
- [1004. 最大连续1的个数 III](https://leetcode.cn/problems/max-consecutive-ones-iii/) 1656
- [1438. 绝对差不超过限制的最长连续子数组](https://leetcode.cn/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) 1672 *需要 SortedList
- [2401. 最长优雅子数组](https://leetcode.cn/problems/longest-nice-subarray/) 1750 *位运算
- [1658. 将 x 减到 0 的最小操作数](https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/) 1817
- [1838. 最高频元素的频数](https://leetcode.cn/problems/frequency-of-the-most-frequent-element/) 1876
- [2516. 每种字符至少取 K 个](https://leetcode.cn/problems/take-k-of-each-character-from-left-and-right/) 1948
- [2831. 找出最长等值子数组](https://leetcode.cn/problems/find-the-longest-equal-subarray/) 1976
- [2106. 摘水果](https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/) 2062
- [1610. 可见点的最大数目](https://leetcode.cn/problems/maximum-number-of-visible-points/) 2147
- [2781. 最长合法子字符串的长度](https://leetcode.cn/problems/length-of-the-longest-valid-substring/) 2204
- [2968. 执行操作使频率分数最大](https://leetcode.cn/problems/apply-operations-to-maximize-frequency-score/) 2444
- [395. 至少有 K 个重复字符的最长子串](https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/)
- [1763. 最长的美好子字符串](https://leetcode.cn/problems/longest-nice-substring/)
- [424. 替换后的最长重复字符](https://leetcode.cn/problems/longest-repeating-character-replacement/) *有些特殊
- [159. 至多包含两个不同字符的最长子串](https://leetcode.cn/problems/longest-substring-with-at-most-two-distinct-characters/)(会员题)
- [340. 至多包含 K 个不同字符的最长子串](https://leetcode.cn/problems/longest-substring-with-at-most-k-distinct-characters/)(会员题)
与单调队列结合 https://www.luogu.com.cn/problem/P3594
#### 不定长滑动窗口(求最短/最小)
- [209. 长度最小的子数组](https://leetcode.cn/problems/minimum-size-subarray-sum/)
- [1234. 替换子串得到平衡字符串](https://leetcode.cn/problems/replace-the-substring-for-balanced-string/) 1878
- [1574. 删除最短的子数组使剩余数组有序](https://leetcode.cn/problems/shortest-subarray-to-be-removed-to-make-array-sorted/) 1932
- [76. 最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/)
- [面试题 17.18. 最短超串](https://leetcode.cn/problems/shortest-supersequence-lcci/)
改成和最小 https://codeforces.com/problemset/problem/224/B 1500
https://codeforces.com/problemset/problem/701/C 1500
#### 不定长滑动窗口(求子数组个数)
- [2799. 统计完全子数组的数目](https://leetcode.cn/problems/count-complete-subarrays-in-an-array/) 1398
- [713. 乘积小于 K 的子数组](https://leetcode.cn/problems/subarray-product-less-than-k/)
- [1358. 包含所有三种字符的子字符串数目](https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters/) 1646
- [2962. 统计最大元素出现至少 K 次的子数组](https://leetcode.cn/problems/count-subarrays-where-max-element-appears-at-least-k-times/) 1701
- [LCP 68. 美观的花束](https://leetcode.cn/problems/1GxJYY/)
- [2302. 统计得分小于 K 的子数组数目](https://leetcode.cn/problems/count-subarrays-with-score-less-than-k/) 1808
- [2537. 统计好子数组的数目](https://leetcode.cn/problems/count-the-number-of-good-subarrays/) 1892
- [2762. 不间断子数组](https://leetcode.cn/problems/continuous-subarrays/) 1940
- [2972. 统计移除递增子数组的数目 II](https://leetcode.cn/problems/count-the-number-of-incremovable-subarrays-ii/) 2153
- [1918. 第 K 小的子数组和](https://leetcode.cn/problems/kth-smallest-subarray-sum/)(会员题)*二分答案
- [2743. 计算没有重复字符的子字符串数量](https://leetcode.cn/problems/count-substrings-without-repeating-character/)(会员题)
和至少为 k 的子数组个数 https://atcoder.jp/contests/abc130/tasks/abc130_d
变形:改成子数组 https://codeforces.com/problemset/problem/550/B
其它题目见【前缀和】
#### 多指针滑动窗口
- [930. 和相同的二元子数组](https://leetcode.cn/problems/binary-subarrays-with-sum/) 1592 恰好等于
- [1248. 统计「优美子数组」](https://leetcode.cn/problems/count-number-of-nice-subarrays/) 1624 类似 930
- [2563. 统计公平数对的数目](https://leetcode.cn/problems/count-the-number-of-fair-pairs/) 1721
- [1712. 将数组分成三个子数组的方案数](https://leetcode.cn/problems/ways-to-split-array-into-three-subarrays/) 2079
- [2444. 统计定界子数组的数目](https://leetcode.cn/problems/count-subarrays-with-fixed-bounds/) 2093
- [992. K 个不同整数的子数组](https://leetcode.cn/problems/subarrays-with-k-different-integers/) 2210
- [1989. 捉迷藏中可捕获的最大人数](https://leetcode.cn/problems/maximum-number-of-people-that-can-be-caught-in-tag/)(会员题)
类似 [795. 区间子数组个数](https://leetcode.cn/problems/number-of-subarrays-with-bounded-maximum/) 1817
入门题 https://codeforces.com/problemset/problem/602/B
入门题 https://codeforces.com/problemset/problem/279/B
https://atcoder.jp/contests/abc229/tasks/abc229_d
LC2271 毯子覆盖的最多白色砖块数 需要多思考一点点 https://leetcode.cn/problems/maximum-white-tiles-covered-by-a-carpet/
- https://atcoder.jp/contests/abc098/tasks/arc098_b
较为复杂 https://atcoder.jp/contests/abc294/tasks/abc294_e
- https://ac.nowcoder.com/acm/contest/62033/D
https://codeforces.com/problemset/problem/1208/B
https://codeforces.com/problemset/problem/1765/D
多指针 https://codeforces.com/problemset/problem/895/B
https://codeforces.com/contest/1833/problem/F
计算有多少子数组,其中有至少 k 个相同的数 https://codeforces.com/problemset/problem/190/D
mex https://atcoder.jp/contests/abc194/tasks/abc194_e
https://codeforces.com/problemset/problem/165/C
双序列双指针
LC88 https://leetcode.cn/problems/merge-sorted-array/
LC360(背向双指针)https://leetcode.cn/problems/sort-transformed-array/
LC986 区间列表的交集 https://leetcode.cn/problems/interval-list-intersections/
https://codeforces.com/contest/489/problem/B 1200
相向双指针
LC2824 https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/
LC923 https://leetcode.cn/problems/3sum-with-multiplicity/
https://www.facebook.com/codingcompetitions/hacker-cup/2023/practice-round/problems/C
同时用到同向双指针和相向双指针的题
https://atcoder.jp/contests/abc155/tasks/abc155_d
- 相似题目 https://leetcode.cn/problems/kth-smallest-product-of-two-sorted-arrays/
a[i] + b[j] = target 的方案数
a[i] + b[j] < target 的方案数 相向双指针 https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/
a[i] + b[j] > target 的方案数 同上
a[i] - b[j] = target 的方案数
a[i] - b[j] < target 的方案数 滑窗
a[i] - b[j] > target 的方案数 同上
子数组元素和 = < > target 的方案数:用前缀和,转换成上面 a[i] - b[j] 的形式
子序列元素和 = < > target 的方案数:0-1 背包恰好/至多/至少,见 https://www.bilibili.com/video/BV16Y411v7Y6/ 末尾的总结
## 分组循环
https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solution/jiao-ni-yi-ci-xing-ba-dai-ma-xie-dui-on-zuspx/
**适用场景**:按照题目要求,数组会被分割成若干组,每一组的判断/处理逻辑是相同的。
**核心思想**:
- 外层循环负责遍历组之前的准备工作(记录开始位置),和遍历组之后的统计工作(更新答案最大值)。
- 内层循环负责遍历组,找出这一组最远在哪结束。
这个写法的好处是,各个逻辑块分工明确,也不需要特判最后一组(易错点)。以我的经验,这个写法是所有写法中最不容易出 bug 的,推荐大家记住。
- [1446. 连续字符](https://leetcode.cn/problems/consecutive-characters/) 1165
- [1869. 哪种连续子字符串更长](https://leetcode.cn/problems/longer-contiguous-segments-of-ones-than-zeros/) 1205
- [1957. 删除字符使字符串变好](https://leetcode.cn/problems/delete-characters-to-make-fancy-string/) 1358
- [2110. 股票平滑下跌阶段的数目](https://leetcode.cn/problems/number-of-smooth-descent-periods-of-a-stock/) 1408
- [228. 汇总区间](https://leetcode.cn/problems/summary-ranges/)
- [2760. 最长奇偶子数组](https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/) 1420
- [1887. 使数组元素相等的减少操作次数](https://leetcode.cn/problems/reduction-operations-to-make-the-array-elements-equal/) 1428
- [2038. 如果相邻两个颜色均相同则删除当前颜色](https://leetcode.cn/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) 1468
- [1759. 统计同质子字符串的数目](https://leetcode.cn/problems/count-number-of-homogenous-substrings/) 1491
- [3011. 判断一个数组是否可以变为有序](https://leetcode.cn/problems/find-if-array-can-be-sorted/) 1497
- [1578. 使绳子变成彩色的最短时间](https://leetcode.cn/problems/minimum-time-to-make-rope-colorful/) 1574
- [1839. 所有元音按顺序排布的最长子字符串](https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/) 1580
- [2765. 最长交替子序列](https://leetcode.cn/problems/longest-alternating-subarray/) 1581
- [2948. 交换得到字典序最小的数组](https://leetcode.cn/problems/make-lexicographically-smallest-array-by-swapping-elements/) 2047
LC1180(会员)https://leetcode.cn/problems/count-substrings-with-only-one-distinct-letter/
LC2257 https://leetcode.cn/problems/count-unguarded-cells-in-the-grid/
- https://atcoder.jp/contests/abc317/tasks/abc317_e
LC2495(会员)逆向思维 https://leetcode.cn/problems/number-of-subarrays-having-even-product/
https://codeforces.com/problemset/problem/1272/C 1200
https://codeforces.com/problemset/problem/1380/C 1400
https://codeforces.com/problemset/problem/620/C 1500
https://codeforces.com/problemset/problem/525/C 1600
https://codeforces.com/problemset/problem/1748/C 1600
巧妙枚举
LC939 https://leetcode.cn/problems/minimum-area-rectangle/
https://codeforces.com/problemset/problem/1181/C
https://codeforces.com/problemset/problem/1626/D
https://codeforces.com/problemset/problem/846/C
贪心及其证明
- [455. 分发饼干](https://leetcode.cn/problems/assign-cookies/)
- [1029. 两地调度](https://leetcode.cn/problems/two-city-scheduling/) 1348
- [2410. 运动员和训练师的最大匹配数](https://leetcode.cn/problems/maximum-matching-of-players-with-trainers/) 1381
- [881. 救生艇](https://leetcode.cn/problems/boats-to-save-people/) 1530
- https://codeforces.com/problemset/problem/1690/E
- https://www.lanqiao.cn/problems/4174/learning/?contest_id=135
- https://codeforces.com/problemset/problem/1765/D
- [2611. 老鼠和奶酪](https://leetcode.cn/problems/mice-and-cheese/) 1663
- [2931. 购买物品的最大开销](https://leetcode.cn/problems/maximum-spending-after-buying-items/) 1822
- [2952. 需要添加的硬币的最小数量](https://leetcode.cn/problems/minimum-number-of-coins-to-be-added/)
- LC330 https://leetcode.cn/problems/patching-array/
- [2136. 全部开花的最早一天](https://leetcode.cn/problems/earliest-possible-day-of-full-bloom/) 2033
- todo 复习 [2193. 得到回文串的最少操作次数](https://leetcode.cn/problems/minimum-number-of-moves-to-make-palindrome/) 2091
- [1505. 最多 K 次交换相邻数位后得到的最小整数](https://leetcode.cn/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/) 2337
https://codeforces.com/problemset/problem/1920/B 1100
https://codeforces.com/problemset/problem/545/D 1300
https://codeforces.com/problemset/problem/388/A 1400
https://codeforces.com/problemset/problem/1443/C 1400
https://codeforces.com/problemset/problem/864/D 1500
https://codeforces.com/problemset/problem/1691/C
https://codeforces.com/problemset/problem/1369/C
提示 1:前 k 大的数一定可以作为最大值。且尽量把大的数放在 w[i] = 1 的组中,这样可以计入答案两次。
如果某个前 k 大的数 x 没有作为最大值(其中一个组的最大值是不在前 k 大中的 y),那么把 x 和 y 交换,
如果 x 是某个组的最小值,那么交换后 y 必然也是最小值,此时答案不变。
如果 x 不是某个组的最小值(这个组的最小值是 z):
如果 y 交换后变成了最小值,那么答案变大了 x-z。
如果 y 交换后也不是最小值,那么答案变大了 x-y。
无论如何,这样交换都不会使答案变小,因此前 k 大的数一定可以作为最大值。
提示 2:然后来说最小值。a 的最小值必然要分到某个组中,为了「跳过」尽量多的较小的数,优先把 a 中较小的数分到 w 较大的组中。所以 a 从小到大遍历,w 从大到小遍历。
https://codeforces.com/problemset/problem/1479/B1 https://codeforces.com/problemset/problem/1479/B2
https://www.luogu.com.cn/blog/wsyhb/post-ti-xie-cf1479b1-painting-the-array-i
https://codeforces.com/problemset/problem/1804/D
https://codeforces.com/problemset/problem/442/C
如果 x>=y<=z,那么删除 y 最优
结束后剩下一个长为 m 的 /\ 形状的序列,由于无法取到最大值和次大值,那么加上剩下最小的 m-2 个数
https://codeforces.com/problemset/problem/1157/C2
https://www.luogu.com.cn/problem/UVA11384 https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2379
倒序思维 https://codeforces.com/problemset/problem/1707/A
https://codeforces.com/contest/1873/problem/G
https://atcoder.jp/contests/arc147/tasks/arc147_e 难
区间贪心
- [435. 无重叠区间](https://leetcode.cn/problems/non-overlapping-intervals/)
- [452. 用最少数量的箭引爆气球](https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/)
- [646. 最长数对链](https://leetcode.cn/problems/maximum-length-of-pair-chain/)
- [1288. 删除被覆盖区间](https://leetcode.cn/problems/remove-covered-intervals/) 1375
- [757. 设置交集大小至少为2](https://leetcode.cn/problems/set-intersection-size-at-least-two/) 2379
- [2589. 完成所有任务的最少时间](https://leetcode.cn/problems/minimum-time-to-complete-all-tasks/) 2381
另见 misc.go 中的 mergeIntervals 和 minJumpNumbers
中位数贪心(右边数字为难度分) // 注:算长度用左闭右开区间思考,算中间值用闭区间思考 两个中位数分别是 a[(n-1)/2] 和 a[n/2]
把 $b_i$ 画在数轴上,$|b_i-x|$ 叫做「$b_i$ 到 $x$ 的距离」,现在求所有 $b_i$ 到 $x$ 的**最小距离和**。
**定理**:将 $b$ 的所有元素变为 $b$ 的**中位数**是最优的。
**证明**:设 $b$ 的长度为 $n$,设要将所有 $b_{i}$ 都变为 $x$。假设 $b$ 已经从小到大排序。首先,如果 $x$ 取在区间 $[b_{0},b_{n-1}]$ 之外,那么 $x$ 向区间靠近,可以使距离和变小;同时,如果 $x$ 取在区间 $[b_{0},b_{n-1}]$ 之内,无论如何移动 $x$,它到 $b_{0}$ 和 $b_{n-1}$ 的距离和都是一个定值 $b_{n-1}-b_{0}$,那么去掉 $b_{0}$ 和 $b_{n-1}$ 这两个最左最右的数,问题规模缩小。$x$ 继续向 $[b_{1},b_{n-2}]$ 靠近,同样可以使距离和变小。像剥洋葱一样,不断地缩小问题规模,如果最后剩下 $1$ 个数,那么 $x$ 就取它;如果最后剩下 $2$ 个数,那么 $x$ 可以取这两个数之间的任意值(包括这两个数)。因此 $x$ 可以取 $b_{\lfloor n/2\rfloor}$,即 $b$ 的一个中位数。
题单(右边数字为难度分)
- [462. 最小操作次数使数组元素相等 II](https://leetcode.cn/problems/minimum-moves-to-equal-array-elements-ii/)
- [2033. 获取单值网格的最小操作数](https://leetcode.cn/problems/minimum-operations-to-make-a-uni-value-grid/) 1672
- [2448. 使数组相等的最小开销](https://leetcode.cn/problems/minimum-cost-to-make-array-equal/) 2005
- [2607. 使子数组元素和相等](https://leetcode.cn/problems/make-k-subarray-sums-equal/) 2071
- [2967. 使数组成为等数数组的最小代价](https://leetcode.cn/problems/minimum-cost-to-make-array-equalindromic/) 2116
- [2968. 执行操作使频率分数最大](https://leetcode.cn/problems/apply-operations-to-maximize-frequency-score/) 2444
- [1703. 得到连续 K 个 1 的最少相邻交换次数](https://leetcode.cn/problems/minimum-adjacent-swaps-for-k-consecutive-ones/) 2467
- [LCP 24. 数字游戏](https://leetcode.cn/problems/5TxKeK/)
https://codeforces.com/problemset/problem/710/B 1400
排序不等式
- [2285. 道路的最大总重要性](https://leetcode.cn/problems/maximum-total-importance-of-roads/) 1496
- [3016. 输入单词需要的最少按键次数 II](https://leetcode.cn/problems/minimum-number-of-pushes-to-type-word-ii/) 1534
- [1402. 做菜顺序](https://leetcode.cn/problems/reducing-dishes/) 1679
- [2931. 购买物品的最大开销](https://leetcode.cn/problems/maximum-spending-after-buying-items/) 1822
- [2809. 使数组和小于等于 x 的最少时间](https://leetcode.cn/problems/minimum-time-to-make-array-sum-at-most-x/) 2979
每次取两个数减一,最后剩下的数最小
https://cs.stackexchange.com/a/145450
每次取数组中大于 0 的连续一段同时减 1,求使数组全为 0 的最少操作次数
https://leetcode.cn/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/solutions/371326/xing-cheng-mu-biao-shu-zu-de-zi-shu-zu-zui-shao-ze/
https://codeforces.com/problemset/problem/448/C
邻项交换(最小代价排序/字典序最小)
某些题目和逆序对有关
LC1665 https://leetcode.cn/problems/minimum-initial-energy-to-finish-tasks/ 1901
https://codeforces.com/problemset/problem/1638/B 1100
https://codeforces.com/problemset/problem/920/C 1400
https://codeforces.com/problemset/problem/435/B 1400
https://codeforces.com/contest/246/problem/A 900
https://atcoder.jp/contests/arc147/tasks/arc147_b
https://atcoder.jp/contests/abc268/tasks/abc268_f
相邻两数之差的绝对值为 1 https://ac.nowcoder.com/acm/contest/65259/C
非邻项交换(最小代价排序/字典序最小)
某些题目可以在 i 到 a[i] 之间连边建图
LC1202 https://leetcode.cn/problems/smallest-string-with-swaps/ 1855
LC2948 https://leetcode.cn/problems/make-lexicographically-smallest-array-by-swapping-elements/ 2047
https://codeforces.com/contest/252/problem/B
https://codeforces.com/problemset/problem/1768/D 1800
https://codeforces.com/contest/109/problem/D 2000
shift+reverse https://codeforces.com/contest/1907/problem/F
区间与点的最大匹配/覆盖问题
https://www.luogu.com.cn/problem/P2887
https://codeforces.com/problemset/problem/555/B
https://codeforces.com/problemset/problem/863/E
倒序
LC2718 https://leetcode.cn/problems/sum-of-matrix-after-queries/
- 加强版 https://www.luogu.com.cn/problem/P9715 ?contestId=126251
观察、结论
https://codeforces.com/problemset/problem/1442/A
https://codeforces.com/problemset/problem/558/C
https://codeforces.com/problemset/problem/1610/E
https://codeforces.com/problemset/problem/1811/C
https://codeforces.com/problemset/problem/1822/D
https://codeforces.com/problemset/problem/1608/C 对拍找反例
脑筋急转弯
LC1503 https://leetcode.cn/problems/last-moment-before-all-ants-fall-out-of-a-plank/
LC2731 https://leetcode.cn/problems/movement-of-robots/
LC280 https://leetcode.cn/problems/wiggle-sort/
LC3012 https://leetcode.cn/problems/minimize-length-of-array-using-operations/
https://codeforces.com/problemset/problem/1009/B 1400
https://codeforces.com/problemset/problem/1169/B 1500
https://codeforces.com/problemset/problem/500/C 1600
https://codeforces.com/problemset/problem/1763/C 2000
https://atcoder.jp/contests/abc194/tasks/abc194_e
https://atcoder.jp/contests/abc196/tasks/abc196_e
https://www.luogu.com.cn/problem/UVA10881 https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_problem&problem=1822
构造
todo 题单 https://www.luogu.com.cn/training/14#problems
LC767 https://leetcode.cn/problems/reorganize-string/
LC667 https://leetcode.cn/problems/beautiful-arrangement-ii/
LC2573 https://leetcode.cn/problems/find-the-string-with-lcp/ 2682
https://atcoder.jp/contests/arc145/tasks/arc145_a
https://codeforces.com/problemset/problem/1788/C 1300
https://codeforces.com/contest/803/problem/A 1400
https://codeforces.com/problemset/problem/1863/D 1400
https://codeforces.com/problemset/problem/1809/C 1500
贪心 https://codeforces.com/problemset/problem/118/C
分类讨论 https://codeforces.com/problemset/problem/584/C
分类讨论 https://codeforces.com/problemset/problem/708/B
https://codeforces.com/problemset/problem/1554/D 1800
分类讨论 https://codeforces.com/contest/1854/problem/A2 1900
相邻字母在字母表中不相邻 https://codeforces.com/contest/1156/problem/B
棋盘放最多的马 https://codeforces.com/problemset/problem/142/B
两点间恰好 k 条最短路径 http://codeforces.com/problemset/problem/388/B
https://codeforces.com/problemset/problem/327/D
https://codeforces.com/problemset/problem/515/D
度数均为 k 且至少(恰好)有一条割边 https://codeforces.com/problemset/problem/550/D
最短/最长 LIS https://codeforces.com/problemset/problem/1304/D
https://codeforces.com/problemset/problem/1789/D
交互 二分 https://codeforces.com/problemset/problem/1838/F
todo https://codeforces.com/problemset/problem/1823/D
OR 构造 https://atcoder.jp/contests/agc015/tasks/agc015_d
https://codeforces.com/problemset/problem/1761/E
https://codeforces.com/problemset/problem/847/C 1800
LC3003 https://leetcode.cn/problems/maximize-the-number-of-partitions-after-operations/
不好想到的构造
https://codeforces.com/contest/1659/problem/D
https://atcoder.jp/contests/abc178/tasks/abc178_f
https://codeforces.com/problemset/problem/1689/E 脑筋急转弯
https://codeforces.com/problemset/problem/1787/E
不变量(想一想,操作不会改变什么)
https://codeforces.com/contest/1775/problem/E 有点差分的味道,想想前缀和
https://atcoder.jp/contests/arc119/tasks/arc119_c 操作不影响交错和
https://codeforces.com/problemset/problem/1365/F 仍然对称
不变量 2(总和)
把一个环形数组切两刀,分成两段,要求相等,求方案数 => 和为 sum(a)/2 的子数组个数
LC494 https://leetcode.cn/problems/target-sum/
分类讨论(部分题是易错题)
https://codeforces.com/problemset/problem/1698/C 1300
https://codeforces.com/problemset/problem/193/A
https://codeforces.com/problemset/problem/489/C
https://codeforces.com/problemset/problem/1605/C
https://codeforces.com/problemset/problem/382/C
https://codeforces.com/problemset/problem/1051/C
https://codeforces.com/problemset/problem/1095/E
https://codeforces.com/problemset/problem/796/C
https://codeforces.com/problemset/problem/1594/F
https://codeforces.com/problemset/problem/1798/E
https://codeforces.com/problemset/problem/1811/F
https://codeforces.com/problemset/problem/1714/F 锻炼代码实现技巧的好题
https://codeforces.com/problemset/problem/1832/D2
https://codeforces.com/contest/1833/problem/G 样例给的挺良心的
https://codeforces.com/problemset/problem/411/C
https://codeforces.com/contest/1516/problem/C
https://codeforces.com/problemset/problem/1763/C
https://codeforces.com/problemset/problem/960/B
https://codeforces.com/problemset/problem/1799/C
https://codeforces.com/problemset/problem/1292/A 1400 也有简单写法
https://codeforces.com/problemset/problem/1009/B 1400 脑筋急转弯
https://codeforces.com/contest/1251/problem/B 1400
https://codeforces.com/problemset/problem/1180/B 1500
https://codeforces.com/problemset/problem/750/C 1600 *也有偏数学的做法
https://codeforces.com/problemset/problem/1861/C 1600 好题!
https://codeforces.com/contest/1914/problem/F 1900
https://codeforces.com/problemset/problem/1761/E 2400
https://codeforces.com/problemset/problem/1730/E 2700
https://atcoder.jp/contests/diverta2019/tasks/diverta2019_c
https://atcoder.jp/contests/abc155/tasks/abc155_d
https://atcoder.jp/contests/abc125/tasks/abc125_d
https://atcoder.jp/contests/arc134/tasks/arc134_d 1998
https://leetcode.cn/problems/maximize-the-number-of-partitions-after-operations/
https://leetcode.cn/problems/count-the-number-of-houses-at-a-certain-distance-ii/
大量分类讨论
https://codeforces.com/problemset/problem/356/C
https://codeforces.com/problemset/problem/460/D
https://codeforces.com/problemset/problem/796/C
https://codeforces.com/problemset/problem/1374/E2
https://codeforces.com/problemset/problem/1647/D
+构造 https://atcoder.jp/contests/arc153/tasks/arc153_c
https://atcoder.jp/contests/agc015/tasks/agc015_d
贡献法
LC979 https://leetcode.cn/problems/distribute-coins-in-binary-tree/ 1709
LC2477 https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital/ 2012
LC891 https://leetcode.cn/problems/sum-of-subsequence-widths/
LC1588 https://leetcode.cn/problems/sum-of-all-odd-length-subarrays/
LC2681 https://leetcode.cn/problems/power-of-heroes/
- https://atcoder.jp/contests/arc116/tasks/arc116_b
LC2763 https://leetcode.cn/problems/sum-of-imbalance-numbers-of-all-subarrays/
更多贡献法题目,见 monotone_stack.go
https://www.lanqiao.cn/problems/12467/learning/?contest_id=167
https://codeforces.com/problemset/problem/1648/A 1400 维度独立
https://codeforces.com/problemset/problem/912/D
https://codeforces.com/problemset/problem/915/F
https://codeforces.com/problemset/problem/1208/E
https://codeforces.com/problemset/problem/1691/C
https://codeforces.com/problemset/problem/1777/D 树
https://codeforces.com/problemset/problem/1788/D 好题!
https://codeforces.com/problemset/problem/1789/C 好题!
https://codeforces.com/problemset/problem/1808/D
https://atcoder.jp/contests/abc290/tasks/abc290_e 好题!
https://atcoder.jp/contests/abc159/tasks/abc159_f 与 0-1 背包结合
^+ https://atcoder.jp/contests/abc201/tasks/abc201_e
小模拟
LC2534 https://leetcode.cn/problems/time-taken-to-cross-the-door/
https://atcoder.jp/contests/abc279/tasks/abc279_f
中模拟
https://atcoder.jp/contests/abc319/tasks/abc319_f
其他
删除一个字符 + 删除最长连续前缀 https://codeforces.com/problemset/problem/1430/D
https://codeforces.com/problemset/problem/521/D
合法括号字符串 (Regular Bracket Sequence, RBS)
https://codeforces.com/problemset/problem/1097/C 1400
https://codeforces.com/problemset/problem/1837/D 1400
https://codeforces.com/problemset/problem/990/C 1500
https://codeforces.com/problemset/problem/1821/E 2100
https://codeforces.com/problemset/problem/1830/C 2400
https://codeforces.com/problemset/problem/847/C 构造
= 变成 <= 或者 >=
求前缀和/后缀和
https://leetcode.cn/problems/maximum-product-of-the-length-of-two-palindromic-substrings/
连续性 + 上下界
https://atcoder.jp/contests/arc137/tasks/arc137_b
https://codeforces.com/contest/1695/problem/C
*/
// 异类双变量:固定某变量统计另一变量的 [0,n)
// EXTRA: 值域上的双变量,见 https://codeforces.com/contest/486/problem/D
// 同类双变量①:固定 i 统计 [0,n)
// 同类双变量②:固定 i 统计 [0,i-1]
// 套路:预处理数据(按照某种顺序排序/优先队列/BST/...),或者边遍历边维护,
// 然后固定变量 i,用均摊 O(1)~O(logn) 的复杂度统计范围内的另一变量 j
// 这样可以将复杂度从 O(n^2) 降低到 O(n) 或 O(nlogn)
// https://codeforces.com/problemset/problem/1194/E
// 进阶:https://codeforces.com/problemset/problem/1483/D
// 删除一段的最长连续递增 CERC10D https://codeforces.com/gym/101487
// 统计量是二元组的情形 https://codeforces.com/problemset/problem/301/D
// 好题 空间优化 https://codeforces.com/contest/1830/problem/B
// 双变量+下取整:枚举分母,然后枚举分子的范围,使得在该范围内的分子/分母是一个定值
// LC1862 https://leetcode.cn/problems/sum-of-floored-pairs/
// https://codeforces.com/problemset/problem/1706/D2
// 利用前缀和实现巧妙的构造 https://www.luogu.com.cn/blog/duyi/qian-zhui-he
// 邻项修改->前缀和->单项修改 https://codeforces.com/problemset/problem/1254/B2 https://ac.nowcoder.com/acm/contest/7612/C
/* 二进制枚举
https://www.luogu.com.cn/problem/UVA11464 https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2459
*/
/* 横看成岭侧成峰
转换为距离的众数 https://codeforces.com/problemset/problem/1365/C
转换为差分数组 https://codeforces.com/problemset/problem/1110/E
https://codeforces.com/problemset/problem/1442/A
https://codeforces.com/problemset/problem/1700/C
https://codeforces.com/problemset/problem/1779/D 改成修改长为 x 的数组?
https://www.luogu.com.cn/problem/P4552
转换为差 http://www.51nod.com/Challenge/Problem.html#problemId=1217
考虑每个点产生的贡献 https://codeforces.com/problemset/problem/1009/E
考虑每条边产生的负贡献 https://atcoder.jp/contests/abc173/tasks/abc173_f
考虑符合范围要求的贡献 https://codeforces.com/problemset/problem/1151/E
和式的另一视角。若每一项的值都在一个范围,不妨考虑另一个问题:值为 x 的项有多少个?https://atcoder.jp/contests/abc162/tasks/abc162_e
对所有排列考察所有子区间的性质,可以转换成对所有子区间考察所有排列。将子区间内部的排列和区间外部的排列进行区分,内部的性质单独研究,外部的当作 (n-(r-l))! 个排列 https://codeforces.com/problemset/problem/1284/C
从最大值入手 https://codeforces.com/problemset/problem/1381/B
等效性 LC1183 https://leetcode.cn/problems/maximum-number-of-ones/
LC1526 https://leetcode.cn/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/
置换 https://atcoder.jp/contests/abc250/tasks/abc250_e
排序+最小操作次数 https://codeforces.com/contest/1367/problem/F2
https://codeforces.com/contest/1830/problem/A
从绝对值最大的开始思考 https://codeforces.com/contest/351/problem/E
https://codeforces.com/problemset/problem/777/C 1600
棋盘染色 LC2577 https://leetcode.cn/problems/minimum-time-to-visit-a-cell-in-a-grid/
https://codeforces.com/contest/1848/problem/A
others https://codeforces.com/blog/entry/118706
*/
/*
## 练习:离线(按难度分排序)
> 由于所有的询问数据都给出了,我们可以通过修改询问的顺序,达到降低时间复杂度的效果。相应的,在线算法就是按照输入的顺序处理,来一个处理一个。
- [2343. 裁剪数字后查询第 K 小的数字](https://leetcode.cn/problems/query-kth-smallest-trimmed-number/) 1652
- [2070. 每一个查询的最大美丽值](https://leetcode.cn/problems/most-beautiful-item-for-each-query/) 1724
- [2503. 矩阵查询可获得的最大分数](https://leetcode.cn/problems/maximum-number-of-points-from-grid-queries/) 2196
- [1851. 包含每个查询的最小区间](https://leetcode.cn/problems/minimum-interval-to-include-each-query/) 2286
- [1697. 检查边长度限制的路径是否存在](https://leetcode.cn/problems/checking-existence-of-edge-length-limited-paths/) 2300
- [2747. 统计没有收到请求的服务器数目](https://leetcode.cn/problems/count-zero-request-servers/)
- [1938. 查询最大基因差](https://leetcode.cn/problems/maximum-genetic-difference-query/) 2503
- [2736. 最大和查询](https://leetcode.cn/problems/maximum-sum-queries/) 2533
*/
/* 逆向思维 / 正难则反:从终点出发 / 小学奥数告诉我们,不可行方案永远比可行方案好求
LC803 https://leetcode.cn/problems/bricks-falling-when-hit/
LC936 https://leetcode.cn/problems/stamping-the-sequence/
LC1199 https://leetcode.cn/problems/minimum-time-to-build-blocks/
LC2382 https://leetcode.cn/problems/maximum-segment-sum-after-removals/
LCP52 https://leetcode.cn/problems/QO5KpG/
https://codeforces.com/problemset/problem/1792/C 1500
- 相似题目 https://codeforces.com/problemset/problem/1367/F1 2100
https://codeforces.com/problemset/problem/1882/B
https://codeforces.com/problemset/problem/712/C 1600
https://codeforces.com/problemset/problem/621/C 1700
https://codeforces.com/problemset/problem/1644/D 1700
https://codeforces.com/problemset/problem/1672/D 1700
https://codeforces.com/problemset/problem/1759/G 1900 求字典序最小,通常可以从大往小思考
https://codeforces.com/problemset/problem/1638/D 2000
https://codeforces.com/problemset/problem/571/A 2100
https://codeforces.com/problemset/problem/369/E 2200
删除变添加
https://codeforces.com/problemset/problem/295/B
https://leetcode.cn/problems/maximum-segment-sum-after-removals/
*/
/* 奇偶性
https://codeforces.com/problemset/problem/763/B
https://codeforces.com/problemset/problem/1270/E
https://codeforces.com/problemset/problem/1332/E 配对法:将合法局面与非法局面配对
LC932 https://leetcode.cn/problems/beautiful-array/ 分治
*/
/* 相邻 传递性
https://codeforces.com/problemset/problem/1582/E
*/
/* 归纳:solve(n)->solve(n-1) 或者 solve(n-1)->solve(n)
https://codeforces.com/problemset/problem/1517/C
https://codeforces.com/problemset/problem/412/D
https://codeforces.com/problemset/problem/266/C
*/
/* 见微知著:考察单个点的规律,从而推出全局规律
https://codeforces.com/problemset/problem/1510/K
LC1806 https://leetcode.cn/problems/minimum-number-of-operations-to-reinitialize-a-permutation/ 1491
*/
// 「恰好」转换成「至少/至多」https://codeforces.com/problemset/problem/1188/C
/* 反悔贪心
另见 heap.go 中的「反悔堆」
https://djy-juruo.blog.luogu.org/qian-tan-fan-hui-tan-xin
https://www.jvruo.com/archives/1844/
https://www.cnblogs.com/nth-element/p/11768155.html
题单 https://www.luogu.com.cn/training/8793
LC1388 双向链表反悔贪心 https://leetcode.cn/problems/pizza-with-3n-slices/
*/
/* 集合哈希
https://codeforces.com/problemset/problem/1394/B
https://www.luogu.com.cn/problem/P6688
*/
/* 操作树
和莫队类似,通过改变查询的顺序来优化复杂度
https://codeforces.com/problemset/problem/707/D
*/
/* Golang 卡常技巧(注:关于 IO 的加速见 io.go)
对于存在海量小对象的情况(如 trie, treap 等),使用 debug.SetGCPercent(-1) 来禁用 GC,能明显减少耗时
对于可以回收的情况(如 append 在超过 cap 时),使用 debug.SetGCPercent(-1) 虽然会减少些许耗时,但若有大量内存没被回收,会有 MLE 的风险
其他情况下使用 debug.SetGCPercent(-1) 对耗时和内存使用无明显影响
对于多组数据的情况,若禁用 GC 会 MLE,可在每组数据的开头或末尾调用 runtime.GC() 或 debug.FreeOSMemory() 手动 GC
参考 https://draveness.me/golang/docs/part3-runtime/ch07-memory/golang-garbage-collector/
https://zhuanlan.zhihu.com/p/77943973
如果没有禁用 GC 但 MLE,可以尝试 1.19 新增的 debug.SetMemoryLimit
例如 debug.SetMemoryLimit(200<<20),其中 200 可以根据题目的约束来修改
具体见如下测试:
180<<20 1996ms 255100KB https://codeforces.com/contest/1800/submission/203769679
195<<20 779ms 257800KB https://codeforces.com/contest/1800/submission/203768086
200<<20 654ms 259300KB https://codeforces.com/contest/1800/submission/203768768
205<<20 764ms 220100KB https://codeforces.com/contest/1800/submission/203771041
210<<20 MLE
参考 https://go.dev/doc/gc-guide#Memory_limit
对于二维矩阵,以 make([][mx]int, n) 的方式使用,比 make([][]int, n) 嵌套 make([]int, m) 更高效(100MB 以上时可以快 ~150ms)
但需要注意这种方式可能会向 OS 额外申请一倍的内存
对比 https://codeforces.com/problemset/submission/375/118043978
https://codeforces.com/problemset/submission/375/118044262
函数内的递归 lambda 会额外消耗非常多的内存(~100MB / 1e6 递归深度)
写在 main 里面 + slice MLE https://codeforces.com/contest/767/submission/174193385
写在 main 里面 + array 257424KB https://codeforces.com/contest/767/submission/174194515
写在 main 外面 + slice 188364KB https://codeforces.com/contest/767/submission/174194380
写在 main 外面 + array 154500KB https://codeforces.com/contest/767/submission/174193693
在特殊情况下,改为手动模拟栈可以减少 > 100MB 的内存
见这题的 Go 提交记录 https://codeforces.com/problemset/problem/163/E
测试:哈希表用时是数组的 13 倍(本题瓶颈)
slice 249ms https://codeforces.com/problemset/submission/570/209063267
hashmap 3259ms https://codeforces.com/problemset/submission/570/209063603
*/
// slice 作为 map 的 key
// 长度为 0 的 slice 对应空字符串
func intSliceAsMapKeyExample(cnt map[string]int, a []int) {
// 如果后面还会修改 a,可以先 copy 一份
//a = append(a[:0:0], a...)
sh := (*reflect.SliceHeader)(unsafe.Pointer(&a))
sh.Len *= bits.UintSize / 8 // 装作 byte slice
s := *(*string)(unsafe.Pointer(sh))
cnt[s]++
}
// 力扣上的 int 和 int64 是一样的,但是有些题目要求返回 []int64
// 此时可以用指针强转
func intsToInt64s(a []int) []int64 {
int64s := *(*[]int64)(unsafe.Pointer(&a))
return int64s
}
func _() {
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
pow10 := func(x int) int { return int(math.Pow10(x)) } // 不需要 round
// TIPS: dir4[i] 和 dir4[i^1] 互为相反方向
dir4 := []struct{ x, y int }{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右(网格)
// TIPS: dir4[i] 和 dir4[i^2] 互为相反方向
dir4 = []struct{ x, y int }{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} // 右下左上(网格,顺时针)
dir4 = []struct{ x, y int }{{0, 1}, {-1, 0}, {0, -1}, {1, 0}} // 右上左下(网格,逆时针)
dir4 = []struct{ x, y int }{{1, 0}, {0, -1}, {-1, 0}, {0, 1}} // 右下左上(坐标系,顺时针)
dir4 = []struct{ x, y int }{{1, 0}, {0, 1}, {-1, 0}, {0, -1}} // 右上左下(坐标系,逆时针)
dir4 = []struct{ x, y int }{{1, 1}, {-1, 1}, {-1, -1}, {1, -1}} // 斜向
dir4 = []struct{ x, y int }{'W': {-1, 0}, 'E': {1, 0}, 'S': {0, -1}, 'N': {0, 1}} // 西东南北(坐标系)
dir4 = []struct{ x, y int }{'W': {0, -1}, 'E': {0, 1}, 'S': {1, 0}, 'N': {-1, 0}} // 西东南北(网格)
dir4 = []struct{ x, y int }{'L': {-1, 0}, 'R': {1, 0}, 'D': {0, -1}, 'U': {0, 1}} // 左右下上(坐标系)
dir4 = []struct{ x, y int }{'L': {0, -1}, 'R': {0, 1}, 'U': {-1, 0}, 'D': {1, 0}} // 左右下上(网格)
dir8 := []struct{ x, y int }{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}} // 逆时针(坐标系)
dir8 = []struct{ x, y int }{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}} // 顺时针(矩阵)
dir8 = []struct{ x, y int }{{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}} // 马走日
perm3 := [][]int{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}
perm4 := [][]int{
{0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {0, 3, 2, 1},
{1, 0, 2, 3}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 2, 3, 0}, {1, 3, 0, 2}, {1, 3, 2, 0},
{2, 0, 1, 3}, {2, 0, 3, 1}, {2, 1, 0, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {2, 3, 1, 0},
{3, 0, 1, 2}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 1, 2, 0}, {3, 2, 0, 1}, {3, 2, 1, 0},
}
abs := func(x int) int {
if x < 0 {
return -x
}
return x
}
ceil := func(a, b int) int {
// assert a >= 0 && b > 0
if a == 0 {
return 0
}
return (a-1)/b + 1
}
// 另一种写法,无需考虑 a 为 0 的情况
ceil = func(a, b int) int {
return (a + b - 1) / b
}
cmp := func(a, b int) int {
if a == b {
return 0
}
if a < b {
return -1
}
return 1
}
sort3 := func(a ...int) (x, y, z int) { slices.Sort(a); return a[0], a[1], a[2] }
ternaryI := func(cond bool, r1, r2 int) int {
if cond {
return r1
}
return r2
}
ternaryS := func(cond bool, r1, r2 string) string {
if cond {
return r1
}
return r2
}
mergeMap := func(x, y map[int]int) map[int]int {
res := make(map[int]int, len(x)+len(y))
for v, c := range x {
res[v] = c
}
for v, c := range y {
res[v] += c //
}
return res
}
xorSet := func(x, y map[int]bool) map[int]bool { // xorMap
res := make(map[int]bool, len(x)+len(y))
for v := range x {
res[v] = true
}
for v := range y {
if res[v] {
delete(res, v)
} else {
res[v] = true
}
}
return res
}
// 顺时针旋转矩阵 90°
// 返回一个拷贝
rotateCopy := func(a [][]int) [][]int {
n, m := len(a), len(a[0])
b := make([][]int, m)
for i := range b {
b[i] = make([]int, n)
}
for i, r := range a {
for j, v := range r {
b[j][n-1-i] = v
}
}
return b
}
// 转置
transpose := func(a [][]int) [][]int {
n, m := len(a), len(a[0])
b := make([][]int, m)
for i := range b {
b[i] = make([]int, n)
for j, r := range a {
b[i][j] = r[i]
}
}
return b
}
// 从低位到高位
toAnyBase := func(x, base int) (res []int) {
for ; x > 0; x /= base {
res = append(res, x%base)
}
return
}
digits := func(x int) (res []int) {
for ; x > 0; x /= 10 {
res = append(res, x%10)
}
return
}
// 预处理所有回文数 https://oeis.org/A002113
// LC2967 https://leetcode.cn/problems/minimum-cost-to-make-array-equalindromic/
// LC906 https://leetcode.cn/problems/super-palindromes/
// LC2081 https://leetcode.cn/problems/sum-of-k-mirror-numbers/
// EXTRA: 单个数字的情况 LC564 https://leetcode.cn/problems/find-the-closest-palindrome/
initPalindromeNumber := func() {
const mx int = 1e9
pal := []int{}
// 哨兵。根据题目来定,也可以设置成 -2e9 等
pal = append(pal, 0)
// 严格按顺序从小到大生成所有回文数
outer:
for base := 1; ; base *= 10 {
// 生成奇数长度回文数,例如 base = 10,生成的范围是 101 ~ 999
for i := base; i < base*10; i++ {
x := i
for t := i / 10; t > 0; t /= 10 {
x = x*10 + t%10
}
if x > mx {
break outer
}
pal = append(pal, x)
}
// 生成偶数长度回文数,例如 base = 10,生成的范围是 1001 ~ 9999
for i := base; i < base*10; i++ {
x := i
for t := i; t > 0; t /= 10 {
x = x*10 + t%10
}
if x > mx {
break outer
}
pal = append(pal, x)
}
}
// 哨兵。根据 mx 调整,如果 mx 是 2e9 的话要写成 mx+2
pal = append(pal, mx+1)
}
// 合并有序数组,保留重复元素
// a b 必须是有序的(可以为空)
merge := func(a, b []int) []int {
i, n := 0, len(a)
j, m := 0, len(b)
res := make([]int, 0, n+m)
for {
if i == n {
return append(res, b[j:]...)
}
if j == m {
return append(res, a[i:]...)
}
if a[i] < b[j] { // 改成 > 为降序
res = append(res, a[i])
i++
} else {
res = append(res, b[j])
j++
}
}
}
// 合并有序数组,保留至多 k 个元素
// https://codeforces.com/problemset/problem/587/C
// https://codeforces.com/problemset/problem/1665/E
mergeWithLimit := func(a, b []int, k int) []int {
i, n := 0, len(a)
j, m := 0, len(b)
res := make([]int, 0, min(n+m, k))
for len(res) < k {
if i == n {
res = append(res, b[j:min(j+k-len(res), m)]...)
break
}
if j == m {
res = append(res, a[i:min(i+k-len(res), n)]...)
break
}
if a[i] < b[j] {
res = append(res, a[i])
i++
} else {
res = append(res, b[j])
j++
}
}
return res
}
// 返回 a 的各个子集的元素和
// 复杂度为 O(1+2+4+...+2^(n-1)) = O(2^n)
// https://codeforces.com/contest/1209/problem/E2
subSum := func(a []int) []int {
sum := make([]int, 1<<len(a))
for i, v := range a {
for mask, bit := 0, 1<<i; mask < bit; mask++ {
sv := sum[mask] + v
sum[bit|mask] = sv
// NOTE: 若要直接在此考察 sv(相当于遍历 sum),注意别漏了 sum[0] = 0 的情况
}
}
return sum
}
// 应用:给出由非负整数组成的数组 a 的子集和 sum,返回 a
// 保证输入有解且 len(sum) = 2^len(a)
// 变形:sum 包含负数 LC1982 https://leetcode.cn/problems/find-array-given-subset-sums/ 2872
// 做法是给所有 sum[i] 加上 -min(sum),这会导致:
// - 若 sum[i] 包含负数 a[i],则新的 sum'[i] 就不包含 a[i]
// - 若 sum[i] 不包含负数 a[i],则新的 sum'[i] 会包含 -a[i]
// 所以新的 sum' 也一样有解
// 对 sum' 求出 a'
// 由于 -min(sum) 是 a 的所有负数之和,所以找到一个 a' 的子集和,若其等于 -min(sum),则将该子集在 a' 中的元素取相反数,就得到了 a
recoverArrayFromSubsetSum := func(sum []int) []int {
slices.Sort(sum)
n := bits.TrailingZeros(uint(len(sum)))
skip := map[int]int{}
ans := make([]int, 0, n)