├── .github
├── CODEOWNERS
├── PULL_REQUEST_TEMPLATE.md
└── workflows
│ ├── auto_update.yml
│ └── auto_update_problems.yml
├── .gitignore
├── CodingTest.md
├── LICENSE
├── README.md
├── algorithm_list.md
├── algorithms
├── backtracking
│ ├── README.md
│ ├── header.md
│ └── list.md
├── binary_search
│ ├── README.md
│ ├── header.md
│ └── list.md
├── brute_force
│ ├── README.md
│ ├── header.md
│ └── list.md
├── data_structure
│ ├── README.md
│ ├── header.md
│ └── list.md
├── data_structure2
│ ├── README.md
│ ├── header.md
│ └── list.md
├── disjoint_set
│ ├── README.md
│ ├── header.md
│ └── list.md
├── divide_and_conquer
│ ├── README.md
│ ├── header.md
│ └── list.md
├── dynamic_programming_1
│ ├── README.md
│ ├── header.md
│ └── list.md
├── dynamic_programming_2
│ ├── README.md
│ ├── header.md
│ └── list.md
├── dynamic_programming_on_trees
│ ├── README.md
│ ├── header.md
│ └── list.md
├── graph_traversal
│ ├── README.md
│ ├── header.md
│ └── list.md
├── greedy
│ ├── README.md
│ ├── header.md
│ └── list.md
├── implementation
│ ├── README.md
│ ├── header.md
│ └── list.md
├── math
│ ├── README.md
│ ├── header.md
│ └── list.md
├── minimum_spanning_tree
│ ├── README.md
│ ├── header.md
│ └── list.md
├── prefix_sum
│ ├── README.md
│ ├── header.md
│ └── list.md
├── shortest_path
│ ├── README.md
│ ├── header.md
│ └── list.md
├── simulation
│ ├── README.md
│ ├── header.md
│ └── list.md
├── string
│ ├── README.md
│ ├── header.md
│ └── list.md
├── topological_sorting
│ ├── README.md
│ ├── header.md
│ └── list.md
├── tree
│ ├── README.md
│ ├── header.md
│ └── list.md
├── trie
│ ├── README.md
│ ├── header.md
│ └── list.md
└── two_pointer
│ ├── README.md
│ ├── header.md
│ └── list.md
├── assets
└── image
│ └── image_problems.png
├── baekjoon_utils
├── README.md
├── baekjoon_utils
│ ├── __init__.py
│ ├── baekjoon_types.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── database.py
│ │ └── solved_api.py
│ ├── daily
│ │ ├── __init__.py
│ │ └── pick.py
│ ├── docs
│ │ ├── __init__.py
│ │ ├── contributor.py
│ │ ├── docs_types.py
│ │ ├── problem.py
│ │ └── utils.py
│ ├── main.py
│ └── utils.py
├── poetry.lock
├── poetry.toml
├── pyproject.toml
└── tests
│ └── __init__.py
├── guideline_for_contribute.md
├── link_for_study.md
├── markdown
├── TODO.md
├── codingtest_info.md
├── contributor.json
├── contributors.md
├── footer.md
├── header.md
├── list.md
├── tags.md
├── updatelog.md
├── workbook.md
├── workbook_footer.md
└── workbook_header.md
├── picked.md
├── picked_legacy.md
├── scripts
├── database.json
└── picked.json
├── solution
├── backtracking
│ ├── 1038
│ │ └── main.py
│ ├── 1182
│ │ ├── Main.java
│ │ └── main.py
│ ├── 1189
│ │ └── main.cpp
│ ├── 1405
│ │ └── main.py
│ ├── 2026
│ │ └── main.py
│ ├── 2580
│ │ └── main.cpp
│ ├── 9663
│ │ └── main.cpp
│ ├── 10974
│ │ └── main.py
│ ├── 14712
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 14889
│ │ └── main.cpp
│ ├── 15649
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15650
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15651
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15652
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15654
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15655
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15656
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15657
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15658
│ │ └── main.py
│ ├── 15663
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15664
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15665
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 15666
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 16198
│ │ └── main.py
│ ├── 18430
│ │ └── main.cpp
│ └── 22944
│ │ └── main.py
├── binary_search
│ ├── 1477
│ │ └── main.py
│ ├── 1654
│ │ └── main.py
│ ├── 1789
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 1920
│ │ └── main.cpp
│ ├── 2110
│ │ └── Main.java
│ ├── 2143
│ │ └── main.cpp
│ ├── 2417
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 2470
│ │ └── main.py
│ ├── 2512
│ │ └── main.py
│ ├── 2776
│ │ └── main.py
│ ├── 2805
│ │ ├── Main.java
│ │ └── main.py
│ ├── 3079
│ │ └── main.py
│ ├── 6236
│ │ └── main.py
│ ├── 9024
│ │ └── Main.java
│ ├── 10815
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 10816
│ │ └── main.py
│ ├── 19637
│ │ └── main.cpp
│ └── 20444
│ │ └── Main.java
├── brute_force
│ ├── 1107
│ │ └── Main.java
│ ├── 1145
│ │ └── main.py
│ ├── 1436
│ │ └── main.py
│ ├── 1969
│ │ └── main.py
│ ├── 2231
│ │ └── main.cpp
│ ├── 2309
│ │ └── main.py
│ ├── 2422
│ │ └── main.cpp
│ ├── 2503
│ │ └── main.py
│ ├── 2615
│ │ └── Main.java
│ ├── 2798
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 4096
│ │ └── main.py
│ ├── 4690
│ │ └── main.py
│ ├── 5568
│ │ └── main.py
│ ├── 7568
│ │ └── main.py
│ ├── 9079
│ │ └── main.cpp
│ ├── 9094
│ │ └── main.py
│ ├── 13140
│ │ └── main.cpp
│ ├── 13410
│ │ └── main.py
│ ├── 14500
│ │ └── main.py
│ ├── 14620
│ │ └── main.py
│ ├── 15661
│ │ └── main.py
│ ├── 15721
│ │ └── main.cpp
│ ├── 16439
│ │ └── main.cpp
│ ├── 17471
│ │ └── main.py
│ ├── 17626
│ │ └── main.py
│ ├── 18312
│ │ └── main.cpp
│ ├── 18511
│ │ └── main.py
│ ├── 18808
│ │ └── main.py
│ ├── 19532
│ │ └── main.cpp
│ ├── 21278
│ │ └── main.py
│ └── 21315
│ │ └── main.py
├── data_structure
│ ├── 1021
│ │ └── main.py
│ ├── 1158
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 1874
│ │ └── main.cpp
│ ├── 1918
│ │ └── main.cpp
│ ├── 1935
│ │ └── main.py
│ ├── 1966
│ │ └── main.py
│ ├── 2164
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 2346
│ │ └── main.py
│ ├── 2493
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 2504
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 2800
│ │ ├── Main.java
│ │ └── main.py
│ ├── 3986
│ │ └── main.py
│ ├── 5397
│ │ └── main.py
│ ├── 5430
│ │ ├── Main.java
│ │ └── main.py
│ ├── 9012
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 10799
│ │ └── main.cpp
│ ├── 10828
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 10845
│ │ └── main.py
│ ├── 10866
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 18115
│ │ └── main.cpp
│ ├── 18258
│ │ ├── main.cpp
│ │ └── main.py
│ └── 22942
│ │ └── main.cpp
├── data_structure2
│ ├── 1269
│ │ └── main.py
│ ├── 1302
│ │ └── main.py
│ ├── 1620
│ │ └── main.cpp
│ ├── 1927
│ │ └── main.py
│ ├── 2075
│ │ └── main.cpp
│ ├── 2696
│ │ └── main.cpp
│ ├── 4358
│ │ └── main.cpp
│ ├── 7662
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 10546
│ │ └── main.py
│ ├── 11279
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 11286
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 14425
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 21939
│ │ └── Main.java
│ └── 21942
│ │ └── main.py
├── disjoint_set
│ ├── 1043
│ │ └── main.cpp
│ ├── 1717
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 1976
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 4195
│ │ └── main.cpp
│ ├── 10775
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 16562
│ │ └── main.cpp
│ ├── 17352
│ │ └── main.cpp
│ └── 18116
│ │ └── main.cpp
├── divide_and_conquer
│ ├── 1074
│ │ └── main.cpp
│ ├── 1802
│ │ └── main.py
│ ├── 1992
│ │ └── main.cpp
│ ├── 2447
│ │ └── main.cpp
│ ├── 2448
│ │ └── main.cpp
│ ├── 2630
│ │ └── main.cpp
│ ├── 4256
│ │ └── main.cpp
│ ├── 4779
│ │ └── main.cpp
│ ├── 17829
│ │ └── main.cpp
│ └── 18222
│ │ └── main.cpp
├── dynamic_programming_1
│ ├── 1010
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 1106
│ │ └── Main.java
│ ├── 1463
│ │ └── main.cpp
│ ├── 1890
│ │ └── main.py
│ ├── 1912
│ │ └── Main.java
│ ├── 2294
│ │ └── main.py
│ ├── 2407
│ │ └── Main.java
│ ├── 2579
│ │ └── main.py
│ ├── 2748
│ │ └── main.cpp
│ ├── 2839
│ │ └── main.cpp
│ ├── 9095
│ │ ├── Main.java
│ │ └── main.py
│ ├── 9461
│ │ └── main.py
│ ├── 9465
│ │ └── main.py
│ ├── 9655
│ │ ├── Main.java
│ │ └── main.py
│ ├── 10164
│ │ └── main.cpp
│ ├── 10870
│ │ └── main.cpp
│ ├── 11048
│ │ └── main.cpp
│ ├── 11053
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 11055
│ │ └── main.cpp
│ ├── 11660
│ │ └── main.cpp
│ ├── 11722
│ │ └── main.py
│ ├── 11726
│ │ ├── Main.java
│ │ └── main.py
│ ├── 11727
│ │ └── main.py
│ ├── 13699
│ │ └── main.py
│ ├── 14722
│ │ └── main.cpp
│ ├── 15486
│ │ └── Main.java
│ ├── 15989
│ │ └── main.cpp
│ ├── 15990
│ │ └── main.cpp
│ ├── 17626
│ │ └── main.py
│ ├── 19622
│ │ └── main.py
│ ├── 21317
│ │ └── main.cpp
│ └── 22869
│ │ └── main.py
├── dynamic_programming_2
│ ├── 1005
│ │ └── main.cpp
│ ├── 1516
│ │ └── main.cpp
│ ├── 1520
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 3687
│ │ └── main.cpp
│ ├── 5557
│ │ └── main.py
│ ├── 7579
│ │ └── main.cpp
│ ├── 9251
│ │ └── main.py
│ ├── 11909
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 11985
│ │ └── main.py
│ ├── 12015
│ │ └── main.cpp
│ ├── 14567
│ │ └── main.py
│ ├── 17485
│ │ └── main.py
│ ├── 18427
│ │ └── main.py
│ └── 21923
│ │ └── main.py
├── dynamic_programming_on_trees
│ ├── 1135
│ │ └── main.py
│ ├── 1949
│ │ └── main.cpp
│ ├── 2213
│ │ └── main.cpp
│ ├── 2533
│ │ └── main.cpp
│ ├── 15681
│ │ └── main.cpp
│ └── 17831
│ │ └── main.py
├── graph_traversal
│ ├── 1012
│ │ └── main.py
│ ├── 1058
│ │ └── main.py
│ ├── 1260
│ │ └── main.cpp
│ ├── 1325
│ │ └── main.cpp
│ ├── 1707
│ │ └── main.py
│ ├── 1726
│ │ └── main.py
│ ├── 2178
│ │ └── main.cpp
│ ├── 2206
│ │ └── main.py
│ ├── 2573
│ │ └── Main.java
│ ├── 2589
│ │ └── main.py
│ ├── 2606
│ │ └── main.cpp
│ ├── 2636
│ │ └── main.cpp
│ ├── 2667
│ │ └── main.cpp
│ ├── 4179
│ │ └── main.py
│ ├── 4963
│ │ └── main.py
│ ├── 5547
│ │ └── main.cpp
│ ├── 7569
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 7576
│ │ └── main.cpp
│ ├── 9328
│ │ └── main.cpp
│ ├── 11724
│ │ └── main.py
│ ├── 11725
│ │ └── main.cpp
│ ├── 13023
│ │ └── Main.java
│ ├── 13549
│ │ └── main.cpp
│ ├── 14502
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 14716
│ │ └── main.py
│ ├── 14940
│ │ └── Main.java
│ ├── 16174
│ │ └── main.py
│ ├── 16234
│ │ └── main.cpp
│ ├── 16918
│ │ └── main.cpp
│ ├── 16953
│ │ └── main.py
│ ├── 16954
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 17836
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 18513
│ │ └── main.cpp
│ └── 20924
│ │ └── main.py
├── greedy
│ ├── 1092
│ │ └── Main.java
│ ├── 1541
│ │ └── Main.java
│ ├── 1758
│ │ └── main.py
│ ├── 1817
│ │ └── main.py
│ ├── 1931
│ │ └── main.py
│ ├── 2141
│ │ ├── Main.java
│ │ └── main.py
│ ├── 2212
│ │ └── main.py
│ ├── 2217
│ │ └── Main.java
│ ├── 2812
│ │ └── main.py
│ ├── 11000
│ │ └── main.py
│ ├── 11399
│ │ └── main.py
│ ├── 11508
│ │ └── main.py
│ ├── 13164
│ │ └── main.py
│ ├── 13305
│ │ └── main.cpp
│ ├── 14247
│ │ └── main.py
│ ├── 14916
│ │ └── main.py
│ ├── 16208
│ │ └── main.py
│ ├── 16435
│ │ └── main.py
│ ├── 19598
│ │ └── main.py
│ ├── 20115
│ │ └── main.py
│ ├── 20300
│ │ └── main.cpp
│ ├── 20365
│ │ └── main.cpp
│ └── 21758
│ │ └── main.cpp
├── implementation
│ ├── 1212
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 1244
│ │ └── main.cpp
│ ├── 1913
│ │ └── main.cpp
│ ├── 2578
│ │ └── main.cpp
│ ├── 2729
│ │ └── main.py
│ ├── 2753
│ │ └── main.cpp
│ ├── 4396
│ │ └── main.cpp
│ ├── 5597
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 10994
│ │ └── main.cpp
│ ├── 12933
│ │ └── main.cpp
│ ├── 14467
│ │ └── main.cpp
│ ├── 14719
│ │ └── main.py
│ ├── 15787
│ │ └── main.py
│ ├── 15806
│ │ └── main.py
│ ├── 16926
│ │ └── main.cpp
│ ├── 16935
│ │ └── main.py
│ ├── 17413
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 20053
│ │ └── main.cpp
│ ├── 20207
│ │ └── main.cpp
│ ├── 20291
│ │ └── main.cpp
│ ├── 20436
│ │ └── main.cpp
│ ├── 20546
│ │ └── main.cpp
│ ├── 21608
│ │ └── main.py
│ ├── 21756
│ │ └── main.cpp
│ ├── 21918
│ │ └── main.py
│ └── 22856
│ │ └── Main.java
├── math
│ ├── 1110
│ │ └── main.cpp
│ ├── 1747
│ │ └── main.cpp
│ ├── 1934
│ │ └── main.cpp
│ ├── 1978
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 1990
│ │ └── Main.java
│ ├── 2581
│ │ └── main.cpp
│ ├── 2609
│ │ └── main.cpp
│ ├── 2745
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 2753
│ │ └── main.py
│ ├── 2960
│ │ └── main.cpp
│ ├── 4134
│ │ └── main.cpp
│ ├── 5347
│ │ └── main.cpp
│ ├── 5618
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 9613
│ │ └── main.cpp
│ ├── 11653
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 21275
│ │ └── main.cpp
│ ├── 21312
│ │ └── main.py
│ ├── 21919
│ │ └── main.py
│ ├── 21920
│ │ └── main.py
│ └── 22864
│ │ └── Main.java
├── minimum_spanning_tree
│ ├── 1197
│ │ └── main.cpp
│ ├── 1368
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 1647
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 1774
│ │ └── main.cpp
│ ├── 1922
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 2887
│ │ └── main.py
│ ├── 4386
│ │ └── main.py
│ ├── 6497
│ │ └── main.py
│ ├── 14621
│ │ └── main.cpp
│ ├── 16398
│ │ └── main.cpp
│ ├── 20010
│ │ └── main.cpp
│ └── 21924
│ │ └── main.py
├── prefix_sum
│ ├── 1749
│ │ └── main.cpp
│ ├── 2015
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 2167
│ │ └── main.cpp
│ ├── 10986
│ │ └── main.cpp
│ ├── 11659
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 11660
│ │ └── main.cpp
│ ├── 14929
│ │ └── main.cpp
│ ├── 16971
│ │ └── main.cpp
│ ├── 20438
│ │ └── main.cpp
│ ├── 20440
│ │ └── main.cpp
│ ├── 20543
│ │ └── main.cpp
│ ├── 21318
│ │ └── main.cpp
│ └── 21757
│ │ └── main.cpp
├── shortest_path
│ ├── 1719
│ │ └── main.py
│ ├── 10282
│ │ └── main.py
│ └── main.py
├── simulation
│ ├── 3190
│ │ └── main.py
│ ├── 5373
│ │ └── main.cpp
│ ├── 14499
│ │ └── main.py
│ ├── 14503
│ │ └── Main.java
│ ├── 14891
│ │ └── main.py
│ ├── 16234
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 16236
│ │ └── main.cpp
│ ├── 17144
│ │ └── main.cpp
│ ├── 20057
│ │ └── main.cpp
│ ├── 20058
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 20061
│ │ └── main.cpp
│ ├── 20165
│ │ └── main.cpp
│ └── 21610
│ │ └── main.py
├── string
│ ├── 1032
│ │ └── main.py
│ ├── 1152
│ │ └── main.py
│ ├── 1181
│ │ ├── Main.java
│ │ └── main.py
│ ├── 1316
│ │ ├── Main.java
│ │ └── main.py
│ ├── 1764
│ │ └── Main.java
│ ├── 2204
│ │ └── main.py
│ ├── 2744
│ │ └── main.py
│ ├── 3029
│ │ ├── Main.java
│ │ └── main.py
│ ├── 4659
│ │ └── Main.java
│ ├── 6550
│ │ └── Main.java
│ ├── 9046
│ │ ├── Main.java
│ │ └── main.py
│ ├── 9342
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.py
│ ├── 10798
│ │ ├── Main.java
│ │ └── main.py
│ ├── 11365
│ │ ├── Main.java
│ │ └── main.py
│ ├── 11720
│ │ ├── Main.java
│ │ └── main.py
│ ├── 12871
│ │ └── main.py
│ ├── 16171
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 17413
│ │ └── Main.java
│ ├── 17609
│ │ └── main.py
│ ├── 20154
│ │ ├── Main.java
│ │ └── main.py
│ ├── 20291
│ │ └── Main.java
│ └── 20437
│ │ └── Main.java
├── tree
│ ├── 1068
│ │ └── main.cpp
│ ├── 1967
│ │ └── Main.java
│ ├── 1991
│ │ └── main.cpp
│ ├── 3584
│ │ ├── Main.java
│ │ └── main.cpp
│ ├── 11437
│ │ └── main.cpp
│ └── 15681
│ │ ├── Main.java
│ │ └── main.py
├── trie
│ └── 14425
│ │ └── main.cpp
└── two_pointer
│ ├── 1806
│ └── main.py
│ ├── 1940
│ └── main.py
│ ├── 2018
│ └── main.py
│ ├── 2230
│ └── Main.java
│ ├── 2470
│ └── Main.java
│ ├── 2473
│ └── main.py
│ ├── 2559
│ └── main.py
│ ├── 15961
│ └── main.py
│ └── 20922
│ ├── Main.java
│ └── main.cpp
├── status.md
└── update_log.md
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | # 백준 문제 솔루션
2 |
3 | ## 문제 번호
4 |
5 | [문제 이름](https://www.acmicpc.net/problem/문제 번호)
6 |
7 | ~~문제 이름에는 백준 문제 제목을, 문제 번호엔 문제 번호를 넣어주시면 됩니다. (이 라인은 지워주세요.)~~
8 |
9 | ## 백준 아이디
10 |
11 | [백준아이디](https://www.acmicpc.net/user/백준아이디)
12 |
13 | ~~위에 "백준아이디"에 본인의 아이디를 적어주세요. (이 라인은 지워주세요.)~~
14 |
15 | ## 문제 풀이
16 |
17 | ~~여기에 간단하게 어떻게 푸셨는지 적어주세요. (이 라인은 지워주세요.)~~
--------------------------------------------------------------------------------
/algorithm_list.md:
--------------------------------------------------------------------------------
1 | # 알고리즘 유형 정리
2 |
3 | 코테에 나올 만한 & 코테에 나온 적 있는 알고리즘을 정리 해봤습니다.
4 |
5 | ### 알고리즘
6 |
7 | - 수학
8 | - 그리디
9 | - 동적계획법
10 | - 투 포인터
11 | - 구현
12 | - BFS
13 | - DFS
14 | - 완전 탐색
15 | - 시뮬레이션
16 | - 이분탐색
17 | - 백트래킹
18 | - 분할정복
19 | - 누적 합
20 | - 문자열
21 | - 최단거리
22 | - 위상정렬
23 | - 분리 집합
24 | - 최소 스패닝 트리
25 | - 트라이
26 | - 트리디피
27 |
28 | ### 자료구조
29 | - map
30 | - set
31 | - tree
32 | - priority queue
33 | - queue
34 | - stack
35 | - deque
36 | - list
37 |
--------------------------------------------------------------------------------
/algorithms/backtracking/header.md:
--------------------------------------------------------------------------------
1 | # Backtracking (백트래킹)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 백트래킹 문제를 뽑았습니다.
6 |
7 | 백트래킹의 기본 연습 문제인 N과 M 시리즈 모든 문제를 추천 문제로 뽑았습니다.
8 |
9 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
10 |
11 |
12 |
13 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
14 |
15 | [백준 문제집](https://www.acmicpc.net/workbook/view/7135)
16 |
--------------------------------------------------------------------------------
/algorithms/binary_search/header.md:
--------------------------------------------------------------------------------
1 | # Binary Search (이분탐색)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 |
10 |
11 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
12 |
13 | [백준 문제집](https://www.acmicpc.net/workbook/view/7277)
14 |
--------------------------------------------------------------------------------
/algorithms/brute_force/header.md:
--------------------------------------------------------------------------------
1 | # Brute Force (완전탐색)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 완전탐색 중 백트래킹 있었던 문제와 최대한 겹치지 않도록 했습니다.
10 |
11 | 다른 알고리즘 풀었어도 완전탐색으로 풀어보시면 좋습니다.
12 |
13 |
14 |
15 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
16 |
17 | [백준 문제집](https://www.acmicpc.net/workbook/view/7271)
18 |
--------------------------------------------------------------------------------
/algorithms/data_structure/header.md:
--------------------------------------------------------------------------------
1 | # Data Structre (자료구조)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 이 자료구조에서는 큐, 스택, 덱을 익히는 문제들로 뽑았습니다.
6 |
7 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
8 |
9 |
10 |
11 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
12 |
13 | [백준 문제집](https://www.acmicpc.net/workbook/view/6779)
14 |
--------------------------------------------------------------------------------
/algorithms/data_structure2/header.md:
--------------------------------------------------------------------------------
1 | # Data Structre2 (자료구조2)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 이 자료구조에서는 Map, Set, Priority Queue(우선순위 큐)를 익히는 문제들로 뽑았습니다.
6 |
7 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
8 |
9 |
10 |
11 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
12 |
13 | [백준 문제집](https://www.acmicpc.net/workbook/view/6780)
14 |
--------------------------------------------------------------------------------
/algorithms/disjoint_set/header.md:
--------------------------------------------------------------------------------
1 | # Disjoint Set (분리집합)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 일반적으로 분리집합을 표현하는데에는 배열을 활용한 유니언 파인드 자료구조를 사용합니다.
10 |
11 | 다만, 카카오 기출 문제에서 일반적인 유니언 파인드 구조를 사용하지 못하도록 전체 크기를 엄청나게 늘린 문제가 출제 되었던 만큼,
12 | 배열이 아닌 해시나 이진트리를 활용해 유니언 파인드 구조를 만들어 보는 연습도 필요합니다!
13 |
14 |
15 |
16 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
17 |
18 | [백준 문제집](https://www.acmicpc.net/workbook/view/6784)
19 |
--------------------------------------------------------------------------------
/algorithms/disjoint_set/list.md:
--------------------------------------------------------------------------------
1 | 1,1717,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1717
2 | 1,1976,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1976
3 | 1,16562,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/16562
4 | 1,4195,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/4195
5 | 1,10775,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/10775
6 | ,20040,
7 | ,11085,
8 | ,17398,
9 | ,17352,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/17352
10 | ,12893,
11 | ,1043,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1043
12 | ,16168,
13 | ,7511,
14 | ,20955,
15 | ,3108,
16 | ,15789,
17 | 1,18116,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/18116
18 | ,17090,
19 | ,16724,
20 | ,14595,
--------------------------------------------------------------------------------
/algorithms/divide_and_conquer/header.md:
--------------------------------------------------------------------------------
1 | # Divide and conquer (분할정복)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 |
10 |
11 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
12 |
13 | [백준 문제집](https://www.acmicpc.net/workbook/view/7275)
14 |
--------------------------------------------------------------------------------
/algorithms/divide_and_conquer/list.md:
--------------------------------------------------------------------------------
1 | 1,2630,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/2630
2 | ,4779,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/4779
3 | ,1780,
4 | 1,17829,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/17829
5 | 1,18222,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/18222
6 | ,1802,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1802
7 | 1,2447,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/2447
8 | 1,1992,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1992
9 | 1,1074,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1074
10 | ,5904,
11 | ,1493,
12 | ,2374,
13 | 1,4256,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/4256
14 | ,14600,
15 | ,1030,
16 | ,16438,
17 | ,14601,
18 | 1,2448,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/2448
--------------------------------------------------------------------------------
/algorithms/dynamic_programming_1/header.md:
--------------------------------------------------------------------------------
1 | # Dynamic Programming 1 (동적계획법 1)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 다이나믹 프로그래밍 유형 쉬운 문제 위주로 뽑았습니다.
6 |
7 | 다이나믹 프로그래밍은 점화식을 세우면 절반 이상은 풀었다고 볼 수 있습니다.
8 |
9 | 점화식 세우는 건 금방 익히기 힘들어 코딩테스트에 나올만한 문제들,
10 | 다이나믹 프로그래밍을 공부할만한 문제들을 최대한 뽑았습니다.
11 |
12 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
13 |
14 |
15 |
16 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
17 |
18 | [백준 문제집](https://www.acmicpc.net/workbook/view/7020)
19 |
--------------------------------------------------------------------------------
/algorithms/dynamic_programming_2/header.md:
--------------------------------------------------------------------------------
1 | # Dynamic Programming 2 (동적계획법 2)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 다이나믹 프로그래밍 유형 문제 위주로 뽑았습니다.
6 |
7 | 다이나믹 프로그래밍은 점화식을 세우면 절반 이상은 풀었다고 볼 수 있습니다.
8 |
9 | 점화식 세우는 건 금방 익히기 힘들어 코딩테스트에 나올만한 문제들,
10 | 다이나믹 프로그래밍을 공부할만한 문제들을 최대한 뽑았습니다.
11 |
12 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
13 |
14 |
15 |
16 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
17 |
18 | [백준 문제집](https://www.acmicpc.net/workbook/view/7021)
19 |
--------------------------------------------------------------------------------
/algorithms/dynamic_programming_on_trees/header.md:
--------------------------------------------------------------------------------
1 | # Dynamic Programming On Trees (트리 디피)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 트리에서의 다이나믹 프로그래밍(Tree DP, 트리디피) 문제를 뽑아봤습니다.
6 |
7 | 이 유형은 코딩테스트에서 나올 확률이 매우 적습니다.
8 |
9 | 이 유형을 몰라도 충분히 통과할 수 있다고 추측되니 다른 유형을 더 보완하는게 좋을 수 있습니다.
10 | **또한 이 유형은 최근 카카오 코딩테스트를 제외하고 본 적이 없습니다.
11 | 하지만 카카오 코딩테스트에서 나왔기 때문에 추가하였습니다.**
12 |
13 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
14 |
15 |
16 |
17 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
18 |
19 | [백준 문제집](https://www.acmicpc.net/workbook/view/7166)
20 |
--------------------------------------------------------------------------------
/algorithms/dynamic_programming_on_trees/list.md:
--------------------------------------------------------------------------------
1 | 1,15681,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/15681
2 | 1,2533,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/2533
3 | ,2058,
4 | 1,2213,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/2213
5 | 1,1949,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1949
6 | ,12978,
7 | ,17831,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/17831
8 | ,1135,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1135
--------------------------------------------------------------------------------
/algorithms/graph_traversal/header.md:
--------------------------------------------------------------------------------
1 | # Graph Traversal (그래프 탐색)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | BFS, DFS 유형을 다양하게 골랐습니다.
10 |
11 | 문제를 보고 어떤 알고리즘을 써야할지 잘 판단하셔야 합니다.
12 |
13 |
14 |
15 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
16 |
17 | [백준 문제집](https://www.acmicpc.net/workbook/view/6853)
18 |
--------------------------------------------------------------------------------
/algorithms/greedy/header.md:
--------------------------------------------------------------------------------
1 | # Greedy (그리디)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 쉬운 그리디부터 조금 고민을 해야하는 그리디까지 다양하게 뽑아봤습니다.
6 |
7 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
8 |
9 |
10 |
11 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
12 |
13 | [백준 문제집](https://www.acmicpc.net/workbook/view/6833)
14 |
--------------------------------------------------------------------------------
/algorithms/implementation/header.md:
--------------------------------------------------------------------------------
1 | # Implementation (구현)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 시뮬레이션 관련 문제는 시뮬레이션에 있습니다.
10 |
11 | (매우 [단순한 구현](https://www.acmicpc.net/problem/1000)는 추가하지 않았습니다.)
12 |
13 |
14 |
15 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
16 |
17 | [백준 문제집](https://www.acmicpc.net/workbook/view/6783)
18 |
--------------------------------------------------------------------------------
/algorithms/math/header.md:
--------------------------------------------------------------------------------
1 | # Math (수학)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 현재 단계는 기초 수학을 익히는 문제들로 뽑았습니다.
6 |
7 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
8 |
9 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
10 |
11 |
12 |
13 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
14 |
15 | [백준 문제집](https://www.acmicpc.net/workbook/view/6781)
16 |
--------------------------------------------------------------------------------
/algorithms/minimum_spanning_tree/header.md:
--------------------------------------------------------------------------------
1 | # Minimum Spanning Tree (최소 스패닝 트리)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 최소 스패닝 트리 문제를 뽑아봤습니다.
6 |
7 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
8 |
9 |
10 |
11 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
12 |
13 | [백준 문제집](https://www.acmicpc.net/workbook/view/7175)
14 |
--------------------------------------------------------------------------------
/algorithms/prefix_sum/header.md:
--------------------------------------------------------------------------------
1 | # Prefix Sum (누적 합)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 누적합은 단순히 이 알고리즘만 사용하는 문제보다 누적합과 다른 알고리즘을 섞어 쓰는 경우가 많습니다.
10 |
11 |
12 |
13 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
14 |
15 | [백준 문제집](https://www.acmicpc.net/workbook/view/7274)
16 |
--------------------------------------------------------------------------------
/algorithms/shortest_path/header.md:
--------------------------------------------------------------------------------
1 | # Shortest Path (최단거리)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 최단거리 문제를 해결할 때 사용하는 알고리즘 중 다익스트라, 벨만-포드, 플로이드 위주로 뽑았습니다.
10 |
11 | 문제를 읽어보고 문제를 해결하기 위해 필요한 무엇인지 생각해봐야 합니다.
12 |
13 | 위 알고리즘들의 차이와 각 알고리즘의 특성을 이해하지 못한 상황에서 문제를 푸는 것은 도움이 안된다고 생각합니다.
14 |
15 |
16 |
17 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
18 |
19 | [백준 문제집](https://www.acmicpc.net/workbook/view/7273)
20 |
--------------------------------------------------------------------------------
/algorithms/simulation/header.md:
--------------------------------------------------------------------------------
1 | # Simulation (시뮬레이션)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 기출 문제를 맨 위로 올렸습니다.
10 |
11 |
12 |
13 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
14 |
15 | [백준 문제집](https://www.acmicpc.net/workbook/view/6832)
16 |
--------------------------------------------------------------------------------
/algorithms/string/header.md:
--------------------------------------------------------------------------------
1 | # String (문자열)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 |
10 |
11 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
12 |
13 | [백준 문제집](https://www.acmicpc.net/workbook/view/7276)
14 |
--------------------------------------------------------------------------------
/algorithms/topological_sorting/header.md:
--------------------------------------------------------------------------------
1 | # Topological Sorting (위상정렬)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 위상정렬 문제를 뽑아봤습니다.
6 |
7 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
8 |
9 | 계보 복원가 호석은 위상정렬로 말고 다른 풀이로도 풀이 가능합니다.
10 |
11 |
12 |
13 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
14 |
15 | [백준 문제집](https://www.acmicpc.net/workbook/view/7165)
16 |
--------------------------------------------------------------------------------
/algorithms/topological_sorting/list.md:
--------------------------------------------------------------------------------
1 | 1,14567,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/14567
2 | ,2056,
3 | ,14676,
4 | 1,1005,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1005
5 | ,1516,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/1516
6 | ,9470,
7 | 1,2252,
8 | 1,1766,
9 | 1,2623,
10 | ,2637,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/2637
11 | ,20119,
12 | ,3665,
13 | ,1948,
14 | ,21276,
--------------------------------------------------------------------------------
/algorithms/tree/header.md:
--------------------------------------------------------------------------------
1 | # tree (트리)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 문제에 트리가 나오는 문제들을 모아보았습니다.
10 |
11 |
12 |
13 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
14 |
15 | [백준 문제집](https://www.acmicpc.net/workbook/view/7645)
16 |
--------------------------------------------------------------------------------
/algorithms/trie/header.md:
--------------------------------------------------------------------------------
1 | # Trie (트라이)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 트라이 추천 문제에서는 꼭 트라이로 안풀어도 되는 문제가 있습니다.
10 |
11 | 하지만 트라이를 연습하려고 하시는 분들은 반드시 트라이로 풀어보는걸 추천드립니다.
12 | (코테에 안나올 가능성이 매우 높은 문제도 있습니다. 골드 수준까지만 풀고 넘어가도 무방합니다.)
13 |
14 |
15 |
16 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
17 |
18 | [백준 문제집](https://www.acmicpc.net/workbook/view/6785)
19 |
--------------------------------------------------------------------------------
/algorithms/trie/list.md:
--------------------------------------------------------------------------------
1 | 1,14425,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/14425
2 | 1,5052,
3 | 1,4358,https://github.com/tony9402/algorithm-solutions/tree/main/solutions/baekjoon/4358
4 | 1,14725,
5 | 1,20166,
6 | ,9202,
7 | ,5670,
8 | ,5446,
9 | ,19585,
--------------------------------------------------------------------------------
/algorithms/two_pointer/header.md:
--------------------------------------------------------------------------------
1 | # Two Pointers (투 포인터)
2 |
3 | [메인으로 돌아가기](https://github.com/tony9402/baekjoon)
4 |
5 | 풀어보면 좋을 문제는 추천 문제에 체크(:heavy_check_mark:) 해놨습니다.
6 |
7 | 추천 문제 아닌 나머지는 나머지를 난이도 섞었습니다.
8 |
9 | 최근 많이 나오는 유형이니 꼭 풀어보시길 바랍니다.
10 |
11 | 특히, 투 포인터는 부분합과 많이 연관되어 나오는 만큼, 투 포인터 문제는 아니지만 부분합을 연습할 수 있는 문제를 첫 문제로 넣어놨습니다.
12 |
13 | 광부 호석은 최근 제 2회 류호석배 코딩테스트에서 나온 문제로 코딩테스트 치곤 상당히 어려운 문제에 속하지만 한번 접해보면 매우 좋은 문제라 넣었습니다.
14 |
15 |
16 |
17 | ***❗️❗️꼭 문제를 순서대로 안풀어도 됩니다.❗️❗️***
18 |
19 | [백준 문제집](https://www.acmicpc.net/workbook/view/6782)
20 |
--------------------------------------------------------------------------------
/assets/image/image_problems.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/assets/image/image_problems.png
--------------------------------------------------------------------------------
/baekjoon_utils/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/baekjoon_utils/README.md
--------------------------------------------------------------------------------
/baekjoon_utils/baekjoon_utils/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/baekjoon_utils/baekjoon_utils/__init__.py
--------------------------------------------------------------------------------
/baekjoon_utils/baekjoon_utils/baekjoon_types.py:
--------------------------------------------------------------------------------
1 | from typing import List, Dict
2 |
3 | from pydantic import BaseModel, Field
4 |
5 |
6 | class ProblemTagNamesType(BaseModel):
7 | language: str = Field(default="")
8 | name: str = Field(default="")
9 | short: str = Field(default="")
10 |
11 |
12 | class ProblemTagType(BaseModel):
13 | key: str = Field(default="")
14 | bojTagId: int = Field(default=-1)
15 | displayNames: List[ProblemTagNamesType] = Field(default_factory=list)
16 |
17 |
18 | class ProblemType(BaseModel):
19 | problemId: int = Field(...)
20 | titleKo: str = Field(...)
21 | isSolvable: bool = Field(...)
22 | level: int = Field(default=0)
23 | averageTries: float = Field(default=0.0)
24 | tags: List[ProblemTagType] = Field(default_factory=list)
25 |
26 |
27 | class DatabaseType(BaseModel):
28 | problems: Dict[str, ProblemType] = Field(default_factory=dict)
29 |
30 |
--------------------------------------------------------------------------------
/baekjoon_utils/baekjoon_utils/core/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/baekjoon_utils/baekjoon_utils/core/__init__.py
--------------------------------------------------------------------------------
/baekjoon_utils/baekjoon_utils/daily/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/baekjoon_utils/baekjoon_utils/daily/__init__.py
--------------------------------------------------------------------------------
/baekjoon_utils/baekjoon_utils/docs/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/baekjoon_utils/baekjoon_utils/docs/__init__.py
--------------------------------------------------------------------------------
/baekjoon_utils/baekjoon_utils/docs/utils.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/baekjoon_utils/baekjoon_utils/docs/utils.py
--------------------------------------------------------------------------------
/baekjoon_utils/poetry.toml:
--------------------------------------------------------------------------------
1 | [virtualenvs]
2 | in-project = true
3 |
--------------------------------------------------------------------------------
/baekjoon_utils/pyproject.toml:
--------------------------------------------------------------------------------
1 | [tool.poetry]
2 | name = "baekjoon-utils"
3 | version = "0.1.0"
4 | description = ""
5 | authors = ["tony9402 "]
6 | readme = "README.md"
7 |
8 | [tool.poetry.dependencies]
9 | python = "^3.12"
10 | pydantic = "^2.7.4"
11 | tqdm = "^4.66.4"
12 | requests = "^2.32.3"
13 | pydantic-core = "^2.27.1"
14 |
15 |
16 | [build-system]
17 | requires = ["poetry-core"]
18 | build-backend = "poetry.core.masonry.api"
19 |
--------------------------------------------------------------------------------
/baekjoon_utils/tests/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/baekjoon_utils/tests/__init__.py
--------------------------------------------------------------------------------
/markdown/TODO.md:
--------------------------------------------------------------------------------
1 |
2 | ## TODO
3 |
4 |
5 | 자세히
6 |
7 | - [x] 코드 리팩토링
8 | - [x] Contributors 추가 스크립트화
9 | - [ ] 문제 뽑으면 Discussions 또는 Issue에서 바로 간단한 명령어로 추가 기능(스크립트화)
10 | - [x] 문제 추가 방식 변경
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/markdown/codingtest_info.md:
--------------------------------------------------------------------------------
1 | ## 최근 기업 코딩테스트 알고리즘 분류 정리
2 |
3 | [바로가기](./CodingTest.md)
4 |
5 | 최근 기업 코딩테스트에 나왔던 알고리즘들을 정리해보았습니다.
6 |
7 | 대부분은 코딩테스트를 본 분들에게 들은거라 몇개가 누락되었거나 잘못된 부분이 있을 수도 있습니다.
8 |
9 |
--------------------------------------------------------------------------------
/markdown/footer.md:
--------------------------------------------------------------------------------
1 | [Backtracking]: ./backtracking
2 | [Binary Search]: ./binary_search
3 | [Data Structure]: ./data_structure
4 | [Data Structure2]: ./data_structure2
5 | [Math]: ./math
6 | [Greedy]: ./greedy
7 | [DP1]: ./dynamic_programming_1
8 | [DP2]: ./dynamic_programming_2
9 | [MST]: ./minimum_spanning_tree
10 | [Two Pointer]: ./two_pointer
11 | [Topological Sorting]: ./topological_sorting
12 | [Implementation]: ./implementation
13 | [Graph Traversal]: ./graph_traversal
14 | [Simulation]: ./simulation
15 | [DFS]: ./dfs
16 | [BFS]: ./bfs
17 | [Brute Force]: ./brute_force
18 | [Disjoint Set]: ./disjoint_set
19 | [Trie]: ./trie
20 | [TreeDP]: ./dynamic_programming_on_trees
21 | [Shortest Path]: ./shortest_path
22 | [Prefix Sum]: ./prefix_sum
23 | [Divide and conquer]: ./divide_and_conquer
24 | [String]: ./string
25 | [Tree]: ./tree
26 | [TODO]: https://img.shields.io/badge/-TODO-DFFD26
27 | [DOING]: https://img.shields.io/badge/-DOING-31AE0F
28 | [DONE]: https://img.shields.io/badge/-DONE-0885CC
29 |
--------------------------------------------------------------------------------
/markdown/list.md:
--------------------------------------------------------------------------------
1 | data_structure,Data Structure,자료구조,Doing
2 | data_structure2,Data Structure 2,자료구조 2,Doing
3 | tree,Tree,트리,Doing
4 | math,Math,수학,Doing
5 | greedy,Greedy,탐욕법,Doing
6 | dynamic_programming_1,Dynamic Programming 1,동적계획법 1,Doing
7 | dynamic_programming_2,Dynamic Programming 2,동적계획법 2,Doing
8 | two_pointer,Two Pointer,투 포인터,Doing
9 | implementation,Implementation,구현,Doing
10 | graph_traversal,Graph Traversal,그래프 탐색,Doing
11 | brute_force,Brute Force,완전탐색,Doing
12 | simulation,Simulation,시뮬레이션,Doing
13 | binary_search,Binary Search,이분탐색,Doing
14 | backtracking,Backtracking,백트래킹,Doing
15 | divide_and_conquer,Divide and conquer,분할정복,Doing
16 | prefix_sum,Prefix Sum,누적 합,Doing
17 | string,String,문자열,Doing
18 | shortest_path,Shortest Path,최단거리,Doing
19 | topological_sorting,Topological Sorting,위상정렬,Doing
20 | disjoint_set,Disjoint Set,분리 집합,Doing
21 | minimum_spanning_tree,Minimum Spanning Tree(MST),최소 스패닝 트리,Doing
22 | trie,Trie,트라이,Doing
23 | dynamic_programming_on_trees,Dynamic Programming On Trees,트리디피,Doing
24 |
--------------------------------------------------------------------------------
/markdown/tags.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tony9402/baekjoon/b6dbcab9aae369de15cdf7f8dc32b1b416787e37/markdown/tags.md
--------------------------------------------------------------------------------
/markdown/workbook_footer.md:
--------------------------------------------------------------------------------
1 |
2 | [현재 진행정도](./status.md)
3 |
4 |
5 | ## 모의 문제집
6 |
7 | ***이 문제는 알고리즘 분류와 관련 없이 문제를 뽑아놓은 문제입니다.***
8 |
9 | **이 부분은 [오늘의 문제](https://github.com/tony9402/baekjoon/blob/main/picked.md)로 대체합니다**
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/markdown/workbook_header.md:
--------------------------------------------------------------------------------
1 | ## 각 알고리즘 문제집
2 |
3 | **❗️❗️순번은 알고리즘 공부 순서와는 무관합니다.❗️❗️**
4 |
5 | 여기에서는 각 알고리즘 개념을 설명하는 것이 없습니다. [알고리즘 설명 링크 모음](./link_for_study.md)
6 |
7 | 문제 뽑은 기준 : 각 태그에 해당하는 문제(코딩 테스트에 나올 정도) 들을 최대한 많이 뽑고 반드시 풀고 넘어가면 좋은 문제를 체크해놨습니다.
8 |
9 | ### **❈ 중요❗️❗️ ❈**
10 |
11 | 이 레포는 **코딩테스트에 나올만한 유형**에 대한 문제를 모았습니다.
12 | 알고리즘 유형이 회사마다 다릅니다. 따라서 아래 알고리즘들을 꼭 다 안풀어도 됩니다.
13 | 지원하시는 회사에 나오는 **유형에 맞춰** 골라 푸시기 바랍니다.
14 | (문제집에도 추천 문제도 골라 푸셔도 됩니다.)
15 |
16 |
--------------------------------------------------------------------------------
/picked.md:
--------------------------------------------------------------------------------
1 | ## 2025/06/03
2 |
3 | | 번호 | 문제 이름 |
4 | | :----: | :---------: |
5 | | [16208](https://www.acmicpc.net/problem/16208) | [귀찮음](https://www.acmicpc.net/problem/16208) |
6 | | [16432](https://www.acmicpc.net/problem/16432) | [떡장수와 호랑이](https://www.acmicpc.net/problem/16432) |
7 | | [4803](https://www.acmicpc.net/problem/4803) | [트리](https://www.acmicpc.net/problem/4803) |
8 | | [2623](https://www.acmicpc.net/problem/2623) | [음악프로그램](https://www.acmicpc.net/problem/2623) |
9 | | [5670](https://www.acmicpc.net/problem/5670) | [휴대폰 자판](https://www.acmicpc.net/problem/5670) |
--------------------------------------------------------------------------------
/solution/backtracking/1038/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gkgg123
2 | # Co-authored by : -
3 | # Link : http://boj.kr/ab9a5bb2d14943b3aefaf98f4648a469
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | def bfs(N):
11 | queue = deque()
12 | for i in range(1, 10):
13 | queue.append((i, str(i)))
14 | while queue:
15 | if len(result) == N + 1:
16 | break
17 | cur_num,totol_num = queue.popleft()
18 | if cur_num != 0:
19 | for k in range(cur_num):
20 | next_num = totol_num + str(k)
21 | queue.append((k,next_num))
22 | result.append(next_num)
23 |
24 | N = int(input())
25 | result = []
26 | for i in range(10):
27 | result.append(i)
28 |
29 | if N >=10:
30 | bfs(N)
31 |
32 | if len(result) > N:
33 | print(result[N])
34 | else:
35 | print(-1)
36 |
--------------------------------------------------------------------------------
/solution/backtracking/10974/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/f386e090dfe84518a7329d6f0a77f8c6
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def DFS():
10 | if len(arr) == N:
11 | print(*arr)
12 | return
13 | for i in range(1, N+1):
14 | if visited[i] == 0:
15 | visited[i] = 1
16 | arr.append(i)
17 | DFS()
18 | arr.pop()
19 | visited[i] = 0
20 |
21 | N = int(input())
22 | arr = []
23 | visited = [0] * (N+1)
24 | DFS()
--------------------------------------------------------------------------------
/solution/backtracking/1182/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/1938989c82c1488282cdb2c22d4a9e17
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def backTracking(idx):
10 | global ans
11 | if len(poc) >= N:
12 | if sum(poc) == S:
13 | ans += 1
14 | return
15 | else:
16 | if sum(poc) == S and poc:
17 | ans += 1
18 | for i in range(idx,N):
19 | poc.append(arr[i])
20 | backTracking(i+1)
21 | poc.pop()
22 |
23 | N, S = map(int, input().split())
24 | arr = list(map(int, input().split()))
25 | visited = [0] * N
26 | poc = []
27 | ans = 0
28 | backTracking(0)
29 | print(ans)
--------------------------------------------------------------------------------
/solution/backtracking/1405/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/90f99f64ed7f4b2ba7af6cba10fac4b9
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | def DFS(x,y,ct,now):
11 | global ans
12 | if ct == N:
13 | ans += now
14 | return
15 | for i in range(4):
16 | dx = x + nx[i]
17 | dy = y + ny[i]
18 | if visited[dx][dy] == 0:
19 | visited[dx][dy] = 1
20 | DFS(dx,dy,ct+1,now*dir[i]/100)
21 | visited[dx][dy] = 0
22 |
23 | arr = list(map(int, input().split()))
24 | ans = 0
25 | nx = [0, 0, 1, -1]
26 | ny = [1, -1, 0, 0]
27 | visited = [[0 for i in range(31)] for j in range(31)]
28 | N = arr[0]
29 | dir = arr[1:]
30 | visited[14][14] = 1
31 | DFS(14,14,0,1)
32 | print(ans)
--------------------------------------------------------------------------------
/solution/backtracking/14712/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/1a1754c8c07c462e9d52af5ae146809c
4 |
5 | #include
6 |
7 | using namespace std;
8 |
9 | int N, M, Map[33][33]; // 1-index
10 | int answer = 0;
11 |
12 | bool check(int y, int x) {
13 | return Map[y-1][x] && Map[y][x-1] && Map[y-1][x-1];
14 | }
15 |
16 | void go(int usedCnt) {
17 | if(usedCnt == N * M) {
18 | answer ++;
19 | return ;
20 | }
21 |
22 | int y = usedCnt / M + 1;
23 | int x = usedCnt % M + 1;
24 |
25 | go(usedCnt + 1);
26 | if(!check(y, x)) {
27 | Map[y][x] = 1;
28 | go(usedCnt + 1);
29 | Map[y][x] = 0;
30 | }
31 | }
32 |
33 | int main(){
34 | ios::sync_with_stdio(false);
35 | cin.tie(0);
36 |
37 | cin >> N >> M;
38 | go(0);
39 | cout << answer;
40 |
41 | return 0;
42 | }
43 |
--------------------------------------------------------------------------------
/solution/backtracking/14712/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/117634e91c71493787610c9e0406a605
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 |
11 | # 1-index
12 | Map = [ [ 0 for _ in range(M + 1) ] for __ in range(N + 1) ]
13 | answer = 0
14 |
15 | def dfs(cnt):
16 | global answer
17 | if cnt == N * M:
18 | answer += 1
19 | return
20 |
21 | y = cnt // M + 1
22 | x = cnt % M + 1
23 |
24 | dfs(cnt + 1)
25 | if Map[y - 1][x] == 0 or Map[y][x - 1] == 0 or Map[y - 1][x - 1] == 0: # 만약 놓을 수 있는 곳이라면
26 | Map[y][x] = 1
27 | dfs(cnt + 1)
28 | Map[y][x] = 0
29 |
30 | dfs(0)
31 | print(answer)
32 |
--------------------------------------------------------------------------------
/solution/backtracking/15649/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/51963cf92cd741ea8bb67b164cc944f9
4 | #include
5 |
6 | using namespace std;
7 |
8 | int choose[10], N, M;
9 | bool used[10];
10 |
11 | void dfs(int cnt) {
12 | if(cnt == M) {
13 | for(int i=0;i> N >> M;
34 | dfs(0);
35 |
36 | return 0;
37 | }
38 |
--------------------------------------------------------------------------------
/solution/backtracking/15649/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/69f68131effd4506a17fdac4b8569c6c
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | choose = [ 0 for _ in range(10) ]
11 | used = [ 0 for _ in range(10) ]
12 |
13 | def dfs(cnt):
14 | global N, M
15 | if cnt == M:
16 | for idx in range(cnt):
17 | print(choose[idx], end=' ')
18 | print()
19 | return
20 |
21 | for i in range(1, N + 1):
22 | if used[i]:
23 | continue
24 | used[i] = 1
25 | choose[cnt] = i
26 | dfs(cnt + 1)
27 | used[i] = 0
28 |
29 | dfs(0)
30 |
--------------------------------------------------------------------------------
/solution/backtracking/15650/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/dc4a2e040e7d406b875abaa4d969bd50
4 | #include
5 |
6 | using namespace std;
7 |
8 | int choose[10], N, M;
9 | bool used[10];
10 |
11 | void dfs(int pre, int cnt) {
12 | if(cnt == M) {
13 | for(int i=0;i> N >> M;
34 | dfs(0, 0);
35 |
36 | return 0;
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/solution/backtracking/15650/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/afca9b0c42354f99a6578ab92e0de0a3
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | choose = [ 0 for _ in range(10) ]
11 | used = [ 0 for _ in range(10) ]
12 |
13 | def dfs(idx, cnt):
14 | global N, M
15 | if cnt == M:
16 | for idx in range(cnt):
17 | print(choose[idx], end=' ')
18 | print()
19 | return
20 |
21 | for i in range(idx, N + 1):
22 | if used[i]:
23 | continue
24 | used[i] = 1
25 | choose[cnt] = i
26 | dfs(i + 1, cnt + 1)
27 | used[i] = 0
28 |
29 | dfs(1, 0)
30 |
31 |
32 |
--------------------------------------------------------------------------------
/solution/backtracking/15651/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/0a5ce31db1ff47cf8190b0275e74bef8
4 | #include
5 |
6 | using namespace std;
7 |
8 | int choose[10], N, M;
9 |
10 | void dfs(int cnt) {
11 | if(cnt == M) {
12 | for(int i=0;i> N >> M;
30 | dfs(0);
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/solution/backtracking/15651/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/4631083fc0c04817af9b1020e2396ddc
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | choose = [ 0 for _ in range(10) ]
11 |
12 | def dfs(idx, cnt):
13 | global N, M
14 | if cnt == M:
15 | for idx in range(cnt):
16 | print(choose[idx], end=' ')
17 | print()
18 | return
19 |
20 | for i in range(1, N + 1):
21 | choose[cnt] = i
22 | dfs(i + 1, cnt + 1)
23 |
24 | dfs(1, 0)
25 |
26 |
--------------------------------------------------------------------------------
/solution/backtracking/15652/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/a90bad59d92f4f87bbb056c9e643b330
4 | #include
5 |
6 | using namespace std;
7 |
8 | int choose[10], N, M;
9 |
10 | void dfs(int pre, int cnt) {
11 | if(cnt == M) {
12 | for(int i=0;i> N >> M;
30 | dfs(1, 0);
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/solution/backtracking/15652/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/2a9874489cb74d7babd30d70501acff9
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | choose = [ 0 for _ in range(10) ]
11 |
12 | def dfs(idx, cnt):
13 | global N, M
14 | if cnt == M:
15 | for idx in range(cnt):
16 | print(choose[idx], end=' ')
17 | print()
18 | return
19 |
20 | for i in range(idx, N + 1):
21 | choose[cnt] = i
22 | dfs(i, cnt + 1)
23 |
24 | dfs(1, 0)
25 |
26 |
27 |
--------------------------------------------------------------------------------
/solution/backtracking/15654/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/3955b249d38d4c79851d4cf97d474c0b
4 | #include
5 |
6 | using namespace std;
7 |
8 | int choose[10];
9 | int arr[10], N, M;
10 | bool used[10];
11 |
12 | void dfs(int cnt) {
13 | if(cnt == M) {
14 | for(int i=0;i> N >> M;
34 | for(int i=0;i> arr[i];
35 | sort(arr, arr + N);
36 | dfs(0);
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/solution/backtracking/15654/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/fa5e88e6aac14dbabbbb8b5bc7037963
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 | used = [ 0 for _ in range(10) ]
13 |
14 | def dfs(cnt):
15 | global N, M
16 | if cnt == M:
17 | for idx in range(cnt):
18 | print(arr[choose[idx]], end=' ')
19 | print()
20 | return
21 |
22 | for i in range(0,N):
23 | if used[i]:
24 | continue
25 | used[i] = 1
26 | choose[cnt] = i
27 | dfs(cnt + 1)
28 | used[i] = 0
29 |
30 | dfs(0)
31 |
--------------------------------------------------------------------------------
/solution/backtracking/15655/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/cb5e9a75972a4ac59cc7b4428d07f2fa
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[10], choose[10], N, M;
9 | bool used[10];
10 |
11 | void dfs(int idx, int cnt) {
12 | if(cnt == M){
13 | for(int i=0;i> N >> M;
34 | for(int i=0;i> arr[i];
35 | sort(arr, arr + N);
36 | dfs(0, 0);
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/solution/backtracking/15655/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/d4f609a8ba2e40e8b578c37f3d21d344
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 | used = [ 0 for _ in range(10) ]
13 |
14 | def dfs(idx, cnt):
15 | global N, M
16 | if cnt == M:
17 | for idx in range(cnt):
18 | print(arr[choose[idx]], end=' ')
19 | print()
20 | return
21 |
22 | for i in range(idx,N):
23 | if used[i]:
24 | continue
25 | used[i] = 1
26 | choose[cnt] = i
27 | dfs(i + 1, cnt + 1)
28 | used[i] = 0
29 |
30 | dfs(0, 0)
31 |
--------------------------------------------------------------------------------
/solution/backtracking/15656/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/83f80cd932d54d52bc8cd412a6ee3abc
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[10], choose[10], N, M;
9 |
10 | void dfs(int cnt) {
11 | if(cnt == M) {
12 | for(int i=0;i> N >> M;
29 | for(int i=0;i> arr[i];
30 | sort(arr, arr + N);
31 | dfs(0);
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/solution/backtracking/15656/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/dd28c9dd06a54efeb12b4f72e82e8c77
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().strip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 |
13 | def dfs(cnt):
14 | global N, M
15 | if cnt == M:
16 | for idx in range(cnt):
17 | print(arr[choose[idx]], end=' ')
18 | print()
19 | return
20 |
21 | for i in range(0,N):
22 | choose[cnt] = i
23 | dfs(cnt + 1)
24 |
25 | dfs(0)
26 |
--------------------------------------------------------------------------------
/solution/backtracking/15657/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/ba10084d78354aeabad7a57999d07356
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[10], choose[10], N, M;
9 |
10 | void dfs(int idx, int cnt) {
11 | if(cnt == M) {
12 | for(int i=0;i> N >> M;
29 | for(int i=0;i> arr[i];
30 | sort(arr, arr + N);
31 | dfs(0, 0);
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/solution/backtracking/15657/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/7bbdc54527b2456d9e1e829834a2ece2
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 |
13 | def dfs(idx, cnt):
14 | global N, M
15 | if cnt == M:
16 | for idx in range(cnt):
17 | print(arr[choose[idx]], end=' ')
18 | print()
19 | return
20 |
21 | for i in range(idx,N):
22 | choose[cnt] = i
23 | dfs(i, cnt + 1)
24 |
25 | dfs(0, 0)
26 |
27 |
--------------------------------------------------------------------------------
/solution/backtracking/15663/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/07e0a7b8b518418d86ac107dcdf56a7e
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[10], choose[10], N, M;
9 | bool used[10];
10 |
11 | void dfs(int cnt) {
12 | if(cnt == M) {
13 | for(int i=0;i> N >> M;
35 | for(int i=0;i> arr[i];
36 | sort(arr, arr + N);
37 | dfs(0);
38 |
39 | return 0;
40 | }
41 |
--------------------------------------------------------------------------------
/solution/backtracking/15663/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/186f2997e3dd48859f2ef8b729d98bf3
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 | used = [ 0 for _ in range(10) ]
13 |
14 | def dfs(cnt):
15 | global N, M
16 | if cnt == M:
17 | for idx in range(cnt):
18 | print(arr[choose[idx]], end=' ')
19 | print()
20 | return
21 |
22 | pre = -1
23 | for i in range(N):
24 | if used[i] or pre == arr[i]:
25 | continue
26 | pre = arr[i]
27 | used[i] = 1
28 | choose[cnt] = i
29 | dfs(cnt + 1)
30 | used[i] = 0
31 |
32 | dfs(0)
33 |
--------------------------------------------------------------------------------
/solution/backtracking/15664/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/6b0fef4c57c541529fa7feb2b74cd385
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[10], choose[10], N, M;
9 | bool used[10];
10 |
11 | void dfs(int idx, int cnt) {
12 | if(cnt == M) {
13 | for(int i=0;i> N >> M;
35 | for(int i=0;i> arr[i];
36 | sort(arr, arr + N);
37 | dfs(0, 0);
38 |
39 | return 0;
40 | }
41 |
--------------------------------------------------------------------------------
/solution/backtracking/15664/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/e43bf55556be4bd19c0ae586bcf5d12a
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 | used = [ 0 for _ in range(10) ]
13 |
14 | def dfs(idx, cnt):
15 | global N, M
16 | if cnt == M:
17 | for idx in range(cnt):
18 | print(arr[choose[idx]], end=' ')
19 | print()
20 | return
21 |
22 | pre = -1
23 | for i in range(idx, N):
24 | if used[i] or pre == arr[i]:
25 | continue
26 | pre = arr[i]
27 | used[i] = 1
28 | choose[cnt] = i
29 | dfs(i + 1, cnt + 1)
30 | used[i] = 0
31 |
32 | dfs(0, 0)
33 |
34 |
--------------------------------------------------------------------------------
/solution/backtracking/15665/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/216c114645a74d2fa0bcba6ddec5370b
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[10], choose[10], N, M;
9 |
10 | void dfs(int cnt) {
11 | if(cnt == M) {
12 | for(int i=0;i> N >> M;
32 | for(int i=0;i> arr[i];
33 | sort(arr, arr + N);
34 | dfs(0);
35 |
36 | return 0;
37 | }
38 |
--------------------------------------------------------------------------------
/solution/backtracking/15665/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/fc8e94e5fb3f4384a7272130a0624d8d
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 |
13 | def dfs(cnt):
14 | global N, M
15 | if cnt == M:
16 | for idx in range(cnt):
17 | print(arr[choose[idx]], end=' ')
18 | print()
19 | return
20 |
21 | pre = -1
22 | for i in range(0, N):
23 | if pre == arr[i]:
24 | continue
25 | pre = arr[i]
26 | choose[cnt] = i
27 | dfs(cnt + 1)
28 |
29 | dfs(0)
30 |
--------------------------------------------------------------------------------
/solution/backtracking/15666/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/f952a2a2e4724f8981ff82ceccbdf854
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[10], choose[10], N, M;
9 |
10 | void dfs(int idx, int cnt) {
11 | if(cnt == M) {
12 | for(int i=0;i> N >> M;
32 | for(int i=0;i> arr[i];
33 | sort(arr, arr + N);
34 | dfs(0, 0);
35 |
36 | return 0;
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/solution/backtracking/15666/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/dbd389b484d4468990a877b205126f0f
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = sorted(list(map(int, input().split())))
11 | choose = [ 0 for _ in range(10) ]
12 |
13 | def dfs(idx, cnt):
14 | global N, M
15 | if cnt == M:
16 | for idx in range(cnt):
17 | print(arr[choose[idx]], end=' ')
18 | print()
19 | return
20 |
21 | pre = -1
22 | for i in range(idx, N):
23 | if pre == arr[i]:
24 | continue
25 | pre = arr[i]
26 | choose[cnt] = i
27 | dfs(i, cnt + 1)
28 |
29 | dfs(0, 0)
30 |
31 |
--------------------------------------------------------------------------------
/solution/backtracking/16198/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/3ba9e83879cc4489a5db43c977f87f45
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | def DFS(x):
11 | global MAX
12 | if len(arr) == 2:
13 | MAX = max(MAX, x)
14 | return
15 | for i in range(1, len(arr)-1):
16 | save = arr[i]
17 | arr.pop(i)
18 | DFS(x + arr[i-1] * arr[i])
19 | arr.insert(i, save)
20 |
21 | N = int(input())
22 | MAX = 0
23 | arr = list(map(int, input().split()))
24 | DFS(0)
25 | print(MAX)
26 |
--------------------------------------------------------------------------------
/solution/backtracking/9663/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/bb3cca54213d47268ddc82a82728f730
4 | #include
5 |
6 | using namespace std;
7 |
8 | int n, ans;
9 | bool Y[22], DL[44], DR[44];
10 |
11 | void dfs(int y, int cnt){
12 | if(cnt == n){
13 | ans++;
14 | return;
15 | }
16 | for(int j=0;j> n;
29 | dfs(0, 0);
30 | cout << ans;
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/solution/binary_search/10815/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/9e4cc6a176c6444d99cbc0379c5fd53a
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | vector V(N);
14 | for(auto &i: V) cin >> i;
15 | sort(V.begin(), V.end());
16 | int Q; cin >> Q;
17 | for(int i=0;i> x;
19 | if(binary_search(V.begin(), V.end(), x)) cout << 1 << ' ';
20 | else cout << 0 << ' ';
21 | }
22 |
23 | return 0;
24 | }
25 |
--------------------------------------------------------------------------------
/solution/binary_search/10816/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/3bcac4799e584285b9df8be8d69ab79a
4 | import sys
5 | from bisect import bisect_left, bisect_right
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | arr = list(map(int, input().split()))
11 | M = int(input())
12 | arr2 = list(map(int, input().split()))
13 | arr.sort()
14 | for i in arr2:
15 | idx_left = bisect_left(arr,i)
16 | idx_right = bisect_right(arr,i)
17 | print(idx_right - idx_left, end=' ')
--------------------------------------------------------------------------------
/solution/binary_search/1477/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : yj2221
2 | # Co-authored by : -
3 | # Link : http://boj.kr/b93ed1e8f2a7413fa8fe39483692604f
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | n, m, l = map(int, input().split())
11 | rests = sorted([0] + list(map(int, input().split())) + [l])
12 |
13 | low = 1
14 | high = l
15 | answer = high
16 | while low <= high:
17 | mid = (low + high) // 2
18 |
19 | cnt = 0
20 | for i in range(n+1):
21 | cnt += (rests[i+1] - rests[i] - 1) // mid
22 |
23 | if cnt<=m:
24 | answer = mid
25 | high = mid - 1
26 | else:
27 | low = mid + 1
28 |
29 | print(answer)
30 |
--------------------------------------------------------------------------------
/solution/binary_search/1654/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/5200a798517a4ec09af16f496ad137bd
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def binary_search():
10 | global ans
11 | start, end = 1, max(arr)
12 |
13 | while start <= end:
14 | ct = 0
15 | mid = (start + end) // 2
16 | for i in arr:
17 | ct += i // mid
18 | if ct < N:
19 | end = mid - 1
20 | else:
21 | start = mid + 1
22 | ans = end
23 |
24 | K,N = map(int, input().split())
25 | arr = []
26 | ans = 0
27 | for i in range(K):
28 | arr.append(int(input()))
29 | binary_search()
30 | print(ans)
31 |
--------------------------------------------------------------------------------
/solution/binary_search/1789/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/b757695822a7451ca902c666e024bb5f
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | ll S;
10 | bool chk(ll n){ return n*(n+1)/2 > S; }
11 |
12 | int main(){
13 | ios::sync_with_stdio(false);
14 | cin.tie(0);
15 | cin >> S;
16 | ll l = 1, r = 93000; // sqrt(4294967295 * 2)
17 | while(l <= r){
18 | ll mid = (l + r) / 2;
19 | if(chk(mid))r = mid - 1;
20 | else l = mid + 1;
21 | }
22 | cout << r;
23 | }
24 |
--------------------------------------------------------------------------------
/solution/binary_search/1920/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/ecf2284ada79472da95d933e310ca959
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | vector V(N);
14 | for(auto &i: V) cin >> i;
15 | sort(V.begin(), V.end());
16 | int Q; cin >> Q;
17 | for(int i=0;i> x;
19 | if(binary_search(V.begin(), V.end(), x)) cout << 1 << '\n';
20 | else cout << 0 << '\n';
21 | }
22 |
23 | return 0;
24 | }
25 |
--------------------------------------------------------------------------------
/solution/binary_search/2417/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/dc2b68715aff442db67228432e9510ed
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | bool chk(ll a, ll b) {
10 | if(b % a == 0) return a >= b / a;
11 | return a > b / a;
12 | }
13 |
14 | int main(){
15 | ios::sync_with_stdio(false);
16 | cin.tie(0);
17 |
18 | ll N; cin >> N;
19 | if(N == 0) {
20 | cout << 0;
21 | return 0;
22 | }
23 | ll L = 1, R = 1LL << 32;
24 | while(L <= R) {
25 | ll mid = L + (R - L) / 2;
26 | if(chk(mid, N)) R = mid - 1;
27 | else L = mid + 1;
28 | }
29 | cout << L;
30 |
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/solution/binary_search/2512/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/7910ebb7f8ec409ba2e93cb14d047a19
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def binary_search():
10 | start, end = 0, max(arr)
11 | while start <= end:
12 | mid = (start + end) // 2
13 | total = 0
14 | for i in arr:
15 | if mid < i:
16 | total += mid
17 | else:
18 | total += i
19 | if total <= M:
20 | start = mid + 1
21 | else:
22 | end = mid - 1
23 | return end
24 |
25 | N = int(input())
26 | arr = list(map(int, input().split()))
27 | M = int(input())
28 | print(binary_search())
29 |
--------------------------------------------------------------------------------
/solution/binary_search/2776/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/93afacc450454aedbd2b0d6667914846
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def binary_search(t):
10 | start, end = 0, len(arr)-1
11 | while start <= end:
12 | mid = (start + end) // 2
13 | if arr[mid] == t:
14 | return 1
15 | elif arr[mid] > t:
16 | end = mid - 1
17 | else:
18 | start = mid + 1
19 | return 0
20 |
21 | T = int(input())
22 | for i in range(T):
23 | N = int(input())
24 | arr = list(map(int, input().split()))
25 | M = int(input())
26 | arr2 = list(map(int, input().split()))
27 | arr.sort()
28 | for j in arr2:
29 | print(binary_search(j))
--------------------------------------------------------------------------------
/solution/binary_search/2805/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/331c1288d35f4db9b12e371e014dfffd
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def binary_search():
10 | global ans
11 | start, end = 0, max(arr)
12 |
13 | while start <= end:
14 | mid = (start + end) // 2
15 | total_length = 0
16 | for i in arr:
17 | if i - mid >= 0:
18 | total_length += i - mid
19 |
20 | if total_length < M:
21 | end = mid - 1
22 | else:
23 | start = mid + 1
24 | ans = end
25 |
26 | N, M = map(int, input().split())
27 | arr = list(map(int, input().split()))
28 | ans = 0
29 | binary_search()
30 | print(ans)
31 |
--------------------------------------------------------------------------------
/solution/binary_search/3079/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : yj2221
2 | # Co-authored by : -
3 | # Link : http://boj.kr/efcb8c8f772044538717ee1203d7c02d
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | n, m = map(int, input().split())
11 | arr = [int(input()) for _ in range(n)]
12 |
13 | low, high = 0, 1000000000 * m
14 | while low <= high:
15 | mid = (low + high)//2
16 | cnt = 0
17 | for time in arr:
18 | cnt += mid//time
19 | if cnt>=m:
20 | high = mid - 1
21 | else:
22 | low = mid + 1
23 |
24 | print(low)
25 |
--------------------------------------------------------------------------------
/solution/binary_search/6236/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/2fe2f6f8da4f448a8b1b2e6a4da13627
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def binary_search():
10 | global K
11 | start, end = max(arr), sum(arr)
12 | while start <= end:
13 | mid = (start + end) // 2
14 | have,ct = 0,0
15 | for i in arr:
16 | if have < i:
17 | have = mid - i
18 | ct += 1
19 | else:
20 | have = have - i
21 | if ct > M:
22 | start = mid + 1
23 | else:
24 | end = mid - 1
25 | K = mid
26 |
27 | N, M = map(int, input().split())
28 | arr = []
29 | K = 0
30 | for i in range(N):
31 | arr.append(int(input()))
32 | binary_search()
33 | print(K)
34 |
--------------------------------------------------------------------------------
/solution/brute_force/1145/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/d16fad5335804ed3a30799f0cf9fdf11
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | arr = list(map(int, input().split()))
10 | for i in range(min(arr), 1000001):
11 | ct = 0
12 | for j in range(5):
13 | if i % arr[j] == 0:
14 | ct += 1
15 | if ct >= 3:
16 | print(i)
17 | break
--------------------------------------------------------------------------------
/solution/brute_force/13410/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/6cd0f036ab1d40eaad68c57ee4eb6ff4
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, K = map(int, input().split())
10 | num = [ int(str(N * i)[::-1]) for i in range(1, K + 1) ]
11 | print(max(num))
12 |
--------------------------------------------------------------------------------
/solution/brute_force/1436/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/32ff54d1000b438281226631f054c697
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | ct = 0
11 | ans = 0
12 | num = 666
13 |
14 | while True:
15 |
16 | if '666' in str(num):
17 | ct += 1
18 |
19 | if ct == N:
20 | ans = num
21 | break
22 |
23 | num += 1
24 |
25 | print(ans)
--------------------------------------------------------------------------------
/solution/brute_force/15661/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/946d652cbd7a4361aaeff5ff1f2612d5
4 | import sys
5 | from itertools import combinations
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | arr = []
12 | num = [i for i in range(N)]
13 | MIN = 1e9
14 | for i in range(N):
15 | arr.append(list(map(int, input().split())))
16 | comb_list = list(combinations(num, N//2))
17 | for comb in comb_list:
18 | start = 0
19 | link = 0
20 | remain = [i for i in range(N) if i not in comb]
21 | for com in combinations(comb, 2):
22 | start += arr[com[0]][com[1]]
23 | for ar in combinations(remain, 2):
24 | link += arr[ar[0]][ar[1]]
25 | MIN = min(MIN, abs(start - link))
26 | print(MIN)
--------------------------------------------------------------------------------
/solution/brute_force/15721/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/2e1ee57b7481444fad22d74a53c6ac21
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int A, T, B; cin >> A >> T >> B;
13 | int answer = -1;
14 | for(int i=1; T ;i++){
15 | for(int j=0;T && j<4;j++) {
16 | T -= (j % 2 == B);
17 | ++answer;
18 | }
19 | for(int j=0;T && j<2;j++) {
20 | for(int k=0;T && k<=i;k++) {
21 | T -= (j % 2 == B);
22 | ++answer;
23 | }
24 | }
25 | }
26 | cout << answer % A;
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/solution/brute_force/16439/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/e9a6c4d2144e4f7db1fd8ab2bfd14058
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[33][33];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N, M; cin >> N >> M;
15 | for(int i=0;i> arr[i][j];
16 | int answer = 0;
17 | for(int i=0;i N:
20 | break
21 | if a * a + b * b == N:
22 | ret = 2
23 |
24 | if ret <= 2:
25 | break
26 | for c in range(1, sqrtN + 1):
27 | if a * a + b * b + c * c > N:
28 | break
29 | if a * a + b * b + c * c == N:
30 | ret = 3
31 | break
32 |
33 | return ret
34 |
35 | N = int(input())
36 | sqrtN = int(sqrt(N))
37 | print(solve(N))
38 |
--------------------------------------------------------------------------------
/solution/brute_force/18312/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/a1f005e8ee00409590b55623b8057ff0
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool chk(int a, int b) {
9 | for(int i=0;i<2;i++) {
10 | if(a % 10 == b) return true;
11 | a /= 10;
12 | }
13 | return false;
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int N, K; cin >> N >> K;
21 | int answer = 0;
22 | for(int i=0;i<(N+1)*3600;i++) {
23 | int hh = i / 3600;
24 | int mm = i / 60 % 60;
25 | int ss = i % 60;
26 | if(chk(hh, K) || chk(mm, K) || chk(ss, K)) answer++;
27 | }
28 | cout << answer;
29 |
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/solution/brute_force/18511/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/b56f91d1e33b44c2b24d57cd460eace2
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def backTracking(num):
10 | global ans
11 | if num > N:
12 | return
13 | ans = max(ans,num)
14 | for i in K:
15 | num = num * 10 + i
16 | backTracking(num)
17 | num = (num - i) // 10
18 |
19 | N, C = map(int, input().split())
20 | K = list(map(int, input().split()))
21 | ans = 0
22 | backTracking(0)
23 | print(ans)
24 |
--------------------------------------------------------------------------------
/solution/brute_force/19532/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/605780919d644059a745b5ae43d221be
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | vector V(6);
13 | for(int i=0;i<6;i++) cin >> V[i];
14 |
15 | for(int x = -1000; x <= 1000; x++) {
16 | for(int y = -1000; y <= 1000; y++) {
17 | int F = V[0] * x + V[1] * y - V[2];
18 | int S = V[3] * x + V[4] * y - V[5];
19 | if(F == 0 && S == 0) {
20 | cout << x << ' ' << y;
21 | return 0;
22 | }
23 | }
24 | }
25 |
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/solution/brute_force/1969/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/c08f03602f524dc3822344da70739929
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N, M = map(int, input().split())
11 | arr = []
12 | word = ''
13 | dist = 0
14 |
15 | for i in range(N):
16 | arr.append(input())
17 |
18 | for i in range(M):
19 | dic = {}
20 | for j in range(N):
21 | if arr[j][i] not in dic:
22 | dic[arr[j][i]] = 1
23 |
24 | else:
25 | dic[arr[j][i]] += 1
26 |
27 | ans = list(dic.items())
28 | ans.sort(key = lambda x : (-x[1], x[0]))
29 | word += ans[0][0]
30 | dist += N - ans[0][1]
31 |
32 | print(word)
33 | print(dist)
--------------------------------------------------------------------------------
/solution/brute_force/2231/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/19379bdb38884854badc892db21a8d6b
4 | #include
5 |
6 | using namespace std;
7 |
8 | int cal(int x) {
9 | int tmp = x;
10 | while(x) {
11 | tmp += x % 10;
12 | x /= 10;
13 | }
14 | return tmp;
15 | }
16 |
17 | int main(){
18 | ios::sync_with_stdio(false);
19 | cin.tie(0);
20 |
21 | int N; cin >> N;
22 | int cur = 1;
23 | while(cur < N + 100 && N != cal(cur)) cur++;
24 | if(cur == N + 100) cur = 0;
25 | cout << cur;
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/brute_force/2309/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/be3b4fb036844cb390e1f7a818471cde
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | def findIndex(ans):
11 |
12 | for i in range(9):
13 | for j in range(i+1,9):
14 | if arr[i] + arr[j] == ans:
15 | return (arr[i], arr[j])
16 |
17 | arr = []
18 | for i in range(9):
19 | arr.append(int(input()))
20 |
21 | ans = sum(arr) - 100
22 | first, second = findIndex(ans)
23 | arr.remove(first)
24 | arr.remove(second)
25 | arr.sort()
26 |
27 | for i in arr:
28 | print(i)
--------------------------------------------------------------------------------
/solution/brute_force/2422/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/514a7a75b2eb4a2eaf862e8227682be7
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool chk[222][222];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N, M; cin >> N >> M;
15 | for(int i=0;i> a >> b;
17 | chk[a][b] = chk[b][a] = true;
18 | }
19 | int answer = 0;
20 | for(int i=1;i<=N;i++) {
21 | for(int j=i+1;j<=N;j++) {
22 | for(int k=j+1;k<=N;k++) {
23 | if(chk[i][j] || chk[j][k] || chk[i][k])continue;
24 | answer++;
25 | }
26 | }
27 | }
28 | cout << answer;
29 |
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/solution/brute_force/2798/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/dc356e3adafd4e158b47dce9a9acd59b
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N, M; cin >> N >> M;
13 | int answer = 0;
14 | vector V(N);
15 | for(int i=0;i> V[i];
16 | for(int i=0;i M) continue;
21 | answer = max(answer, value);
22 | }
23 | }
24 | }
25 | cout << answer;
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/brute_force/2798/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/29a558ec778348f589fad5c627187638
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | MAX = 0
11 | arr = list(map(int, input().split()))
12 | for i in range(len(arr)):
13 | for j in range(i+1,len(arr)):
14 | for z in range(j+1,len(arr)):
15 | if arr[i] + arr[j] + arr[z] <= M:
16 | MAX = max(MAX, arr[i] + arr[j] + arr[z])
17 | print(MAX)
--------------------------------------------------------------------------------
/solution/brute_force/4096/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/b554cc74d1a24c9e9306775641121065
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | def isPalindrome(s):
9 | return s == s[::-1]
10 |
11 | while True:
12 | N = input()
13 | length = len(N)
14 | ct = 0
15 | if N == '0':
16 | break
17 | elif isPalindrome(N):
18 | print(0)
19 | else:
20 | while True:
21 | if isPalindrome(N):
22 | print(ct)
23 | break
24 | N = str(int(N)+1)
25 | N = '0' * (length - len(N)) + N
26 | ct += 1
--------------------------------------------------------------------------------
/solution/brute_force/4690/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/fb355cbf95324465b430f4423567a2c4
4 | for a in range(2, 100 + 1):
5 | for b in range(2, a):
6 | for c in range(b, a):
7 | for d in range(c, a):
8 | if a ** 3 == b ** 3 + c ** 3 + d ** 3:
9 | print(f'Cube = {a}, Triple = ({b},{c},{d})')
--------------------------------------------------------------------------------
/solution/brute_force/5568/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/0dd94001579e4341b3ea06ed523d2cba
4 | import sys
5 | from itertools import permutations
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | n = int(input())
11 | arr = []
12 | ans = set()
13 | k = int(input())
14 | for i in range(n):
15 | arr.append(input())
16 | per = list(permutations(arr, k))
17 | for i in range(len(per)):
18 | st = ''
19 | for j in range(len(per[i])):
20 | st += per[i][j]
21 | ans.add(st)
22 | ans = list(ans)
23 | print(len(ans))
--------------------------------------------------------------------------------
/solution/brute_force/7568/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/5540a61d3d364466bbe47e8c62beaedb
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | arr = []
11 |
12 | for i in range(N):
13 | weight, height = map(int, input().split())
14 | arr.append([weight,height,1])
15 |
16 | for i in range(N):
17 | for j in range(N):
18 | if arr[i][0] < arr[j][0] and arr[i][1] < arr[j][1]:
19 | arr[i][2] += 1
20 |
21 | for i in range(len(arr)):
22 | print(arr[i][2], end=' ')
--------------------------------------------------------------------------------
/solution/brute_force/9094/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : tony9402
3 | # Link : http://boj.kr/ea96af96027540dcb1daa3b65e849eab
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | T = int(input())
10 | for i in range(T):
11 | n,m = map(int, input().split())
12 | ct = 0
13 | for a in range(1,n):
14 | for b in range(a+1,n):
15 | if (a * a + b * b + m) % (a*b) == 0:
16 | ct += 1
17 |
18 | print(ct)
19 |
--------------------------------------------------------------------------------
/solution/data_structure/1021/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/71a5091026cf4a1b836dc46c812aeaca
4 | import sys
5 | from collections import deque
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = list(map(int, input().split()))
11 | queue = deque([i for i in range(1,N+1)])
12 | ans = 0
13 | for i in arr:
14 | idx = queue.index(i)
15 | if idx == 0:
16 | queue.popleft()
17 | else:
18 | if idx <= len(queue)//2:
19 | queue.rotate(-idx)
20 | queue.popleft()
21 | ans += idx
22 | else:
23 | queue.rotate(len(queue) - idx)
24 | ans += len(queue) - idx
25 | queue.popleft()
26 | print(ans)
--------------------------------------------------------------------------------
/solution/data_structure/10828/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/ab416f0794fc41cabc3d9ed46db29f60
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 |
11 | stack = []
12 |
13 | for i in range(N):
14 | cmd = input().split()
15 | X = 0
16 | if len(cmd) == 2:
17 | X = cmd[1]
18 | cmd = cmd[0]
19 |
20 | if cmd == "push":
21 | stack.append(X)
22 | elif cmd == "pop":
23 | if len(stack) == 0:
24 | print(-1)
25 | else:
26 | print(stack[-1])
27 | stack.pop(-1)
28 | elif cmd == "size":
29 | print(len(stack))
30 | elif cmd == "empty":
31 | print(0 if len(stack) else 1)
32 | elif cmd == "top":
33 | if len(stack) == 0:
34 | print(-1)
35 | else:
36 | print(stack[-1])
37 |
--------------------------------------------------------------------------------
/solution/data_structure/1158/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/d29720a15b49470bb1ec3f405e4decdb
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N, K; cin >> N >> K;
13 | queue Q;
14 | for(int i=1;i<=N;i++) Q.push(i);
15 | cout << "<";
16 | while(true) {
17 | for(int i=1;i";
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/data_structure/1158/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : klm03025
2 | # Co-authored by : -
3 | # Link : http://boj.kr/1306fa8a1c5a4f4cab631f833d92636a
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N, K = map(int, input().split())
11 | _list = []
12 | q = deque([i + 1 for i in range(N)])
13 |
14 | while len(q) != 0:
15 | q.rotate(-K)
16 | _list.append(q.pop())
17 |
18 | print('<' + ', '.join(map(str, _list)) + '>')
--------------------------------------------------------------------------------
/solution/data_structure/18115/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : r4pidstart
2 | // Co-authored by : -
3 | // Link : https://www.acmicpc.net/source/share/380f9c5d8ce848cf972fc30b9c8eaa93
4 | #include
5 | using namespace std;
6 |
7 | int main(void){
8 | ios::sync_with_stdio(false); cin.tie(nullptr);
9 | int n; cin >> n;
10 |
11 | vector cmd(n);
12 | for(int i=0; i> cmd[i];
13 |
14 | // 앞에서도, 뒤에서도 카드를 다루기에 덱이 적합하다
15 | deque dq;
16 |
17 | // 마지막으로 내려놓은 카드부터, 차례대로 처리한다
18 | for(int i=n-1; i>=0; i--){
19 | // i번째에 내려놓은 카드는 n-i번 카드이다.
20 | if(cmd[i] == 1) dq.push_front(n-i);
21 | else if(cmd[i] == 2) dq.insert(dq.begin()+1, n-i);
22 | else dq.push_back(n-i); // if(cmd[1] == 3)
23 | }
24 |
25 | // dq에 정리된 카드를 순서대로 출력한다
26 | for(auto i : dq) cout << i << ' ';
27 | }
28 |
--------------------------------------------------------------------------------
/solution/data_structure/18258/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/6bf4491116ff480fab750a65591c134e
4 |
5 | import sys
6 | from collections import deque
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | queue = deque()
11 | N = int(input())
12 | for i in range(N):
13 | com = input().split()
14 | if com[0] == 'push':
15 | queue.append(com[1])
16 | elif com[0] == 'pop':
17 | if queue:
18 | print(queue.popleft())
19 | else:
20 | print(-1)
21 | elif com[0] == 'size':
22 | print(len(queue))
23 | elif com[0] == 'front':
24 | if queue:
25 | print(queue[0])
26 | else:
27 | print(-1)
28 | elif com[0] == 'back':
29 | if queue:
30 | print(queue[-1])
31 | else:
32 | print(-1)
33 | elif com[0] == 'empty':
34 | if not queue:
35 | print(1)
36 | else:
37 | print(0)
--------------------------------------------------------------------------------
/solution/data_structure/1966/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/74b1e7adb56b425aa6644b3d2ea726e0
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | T = int(input())
11 | for i in range(T):
12 | queue = deque()
13 | queue2 = deque()
14 | ct = 1
15 | N,M = map(int, input().split())
16 | arr = list(map(int, input().split()))
17 | for j in range(len(arr)):
18 | queue.append(arr[j])
19 | queue2.append(j)
20 | while True:
21 | if queue[0] == max(queue):
22 | if queue2[0] == M:
23 | print(ct)
24 | break
25 | else:
26 | queue.popleft()
27 | queue2.popleft()
28 | ct += 1
29 | else:
30 | queue.rotate(-1)
31 | queue2.rotate(-1)
--------------------------------------------------------------------------------
/solution/data_structure/2164/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/cb5c5740111745b0b9f0fb6582da5bdd
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | queue Q;
14 | for(int i=1;i<=N;i++) Q.push(i);
15 | while((int)Q.size() > 1) {
16 | Q.pop();
17 | Q.push(Q.front());
18 | Q.pop();
19 | }
20 | cout << Q.front();
21 |
22 | return 0;
23 | }
24 |
--------------------------------------------------------------------------------
/solution/data_structure/2164/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/3ad1b45ad3db4bf7b1bfc5227e1acd12
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | queue = deque()
11 | N = int(input())
12 | for i in range(1,N+1):
13 | queue.append(i)
14 |
15 | while True:
16 | a = queue.popleft()
17 | if not queue:
18 | print(a)
19 | break
20 | b = queue.popleft()
21 | queue.append(b)
--------------------------------------------------------------------------------
/solution/data_structure/2346/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/71af42c3664749e48f8f0272c3c04fd2
4 | import sys
5 | from collections import deque
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | queue = deque(list(map(int, input().split())))
11 | queue2 = deque([i for i in range(1,N+1)])
12 | while queue:
13 | q = queue[0]
14 | if q > 0:
15 | queue.popleft()
16 | queue.rotate(-q+1)
17 | print(queue2.popleft())
18 | queue2.rotate(-q+1)
19 | else:
20 | queue.popleft()
21 | queue.rotate(-q)
22 | print(queue2.popleft())
23 | queue2.rotate(-q)
--------------------------------------------------------------------------------
/solution/data_structure/2493/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/b2e3a3c79d3f4e219827c39174c3238f
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | stack> st;
14 |
15 | st.emplace(100000005, 0);
16 | for(int i=1;i<=N;i++) {
17 | int x; cin >> x;
18 | while(!st.empty() && st.top().first < x) st.pop();
19 | cout << st.top().second << ' ';
20 | st.emplace(x, i);
21 | }
22 |
23 | return 0;
24 | }
25 |
--------------------------------------------------------------------------------
/solution/data_structure/3986/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/25fcff12eee34fb6a0f0d4023cac4e4d
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | ans = 0
11 | for i in range(N):
12 | stack = []
13 | a = input()
14 | for i in a:
15 | if not stack:
16 | stack.append(i)
17 | else:
18 | if stack[-1] == i:
19 | stack.pop()
20 | else:
21 | stack.append(i)
22 | if not stack:
23 | ans += 1
24 | print(ans)
--------------------------------------------------------------------------------
/solution/data_structure/5397/main.py:
--------------------------------------------------------------------------------
1 | # // Authored by : chj3748
2 | # // Co-authored by : -
3 | # // Link : http://boj.kr/471d69f455a544769c6c2fa7199442d1
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | T = int(input())
11 | for test in range(T):
12 | answer_l = deque()
13 | answer_r = deque()
14 | for string in input():
15 | if string == '<':
16 | if answer_l:
17 | temp = answer_l.pop()
18 | answer_r.appendleft(temp)
19 | elif string == '>':
20 | if answer_r:
21 | temp = answer_r.popleft()
22 | answer_l.append(temp)
23 | elif string == '-':
24 | if answer_l:
25 | answer_l.pop()
26 | else:
27 | answer_l.append(string)
28 | print(''.join(answer_l + answer_r))
29 |
--------------------------------------------------------------------------------
/solution/data_structure/9012/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/553d0936c1674183a249a58070467343
4 | #include
5 |
6 | using namespace std;
7 |
8 | string solve(string str) {
9 | stack st;
10 | for(int i=0;i<(int)str.size();i++){
11 | if(str[i] == '(') st.push('(');
12 | else {
13 | if(st.empty()) return "NO";
14 | st.pop();
15 | }
16 | }
17 |
18 | if(st.empty()) return "YES";
19 | return "NO";
20 | }
21 |
22 | int main(){
23 | ios::sync_with_stdio(false);
24 | cin.tie(0);
25 |
26 | int T; cin >> T;
27 | while(T--) {
28 | string s; cin >> s;
29 | cout << solve(s) << '\n';
30 | }
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/solution/data_structure/9012/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : klm03025
2 | # Co-authored by : -
3 | # Link : http://boj.kr/b57f1dad96a946e48e0679872d97d024
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | result = ""
11 |
12 | for i in range(N):
13 | testcase = input()
14 | cnt = 0
15 | for c in testcase:
16 | cnt += 1 if c == '(' else -1
17 | if cnt < 0:
18 | result += "NO\n"
19 | break
20 | else:
21 | result += "YES\n" if cnt == 0 else "NO\n"
22 |
23 | print(result)
--------------------------------------------------------------------------------
/solution/data_structure2/10546/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/bb59bafb8ef04d69a5ab5ade6d7cfeb9
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | dic = {}
11 | for i in range(N):
12 | name = input()
13 | dic[name] = dic.get(name, 0) + 1
14 |
15 | for i in range(N-1):
16 | name = input()
17 | dic[name] -= 1
18 |
19 | dic_list = list(dic.items())
20 | dic_list.sort(key = lambda x : (-x[1], x[0]))
21 | print(dic_list[0][0])
22 |
--------------------------------------------------------------------------------
/solution/data_structure2/11279/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/53d07a6885b1412d93b53a1fe211e153
4 | #include
5 |
6 | using namespace std;
7 |
8 | priority_queue pq;
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N; cin >> N;
15 | pq.push(0);
16 | for(int i=0;i> x;
18 | if(x == 0) {
19 | if(pq.empty()) cout << 0 << '\n';
20 | else {
21 | cout << pq.top() << '\n';
22 | pq.pop();
23 | }
24 | }
25 | else pq.push(x);
26 | }
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/solution/data_structure2/11279/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/2b2600bda1c447b7b7f2c2399b167716
4 | import sys
5 | import heapq
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | heap = []
12 | for i in range(N):
13 | a = int(input())
14 | if a == 0:
15 | if heap:
16 | print(-heapq.heappop(heap))
17 | else:
18 | print(0)
19 | heapq.heappush(heap, -a)
--------------------------------------------------------------------------------
/solution/data_structure2/11286/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/3866a743ca954b6fa27922ed08da2b5e
4 | #include
5 |
6 | using namespace std;
7 |
8 | priority_queue, vector>, greater>> pq;
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N; cin >> N;
15 | for(int i=0;i> x;
17 | if(x == 0) {
18 | if(pq.empty()) cout << 0 << '\n';
19 | else {
20 | cout << pq.top().second << '\n';
21 | pq.pop();
22 | }
23 | }
24 | else pq.emplace(abs(x), x);
25 | }
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/data_structure2/11286/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/29a558ec778348f589fad5c627187638
4 | import sys
5 | import heapq
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | heap = []
12 | for i in range(N):
13 | x = int(input())
14 | if x == 0:
15 | if heap:
16 | print(heapq.heappop(heap)[1])
17 | else:
18 | print(0)
19 | else:
20 | heapq.heappush(heap, [abs(x), x])
--------------------------------------------------------------------------------
/solution/data_structure2/1269/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/73b320e09628425c8d29b3c81287a865
4 | import sys
5 | from collections import defaultdict
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | len_a, len_b = map(int, input().split())
11 | dict_a = defaultdict(int)
12 |
13 | for number in map(int, input().split()):
14 | dict_a[number] = 1
15 |
16 | cnt = 0
17 |
18 | for number in map(int, input().split()):
19 | if dict_a[number]:
20 | cnt += 1
21 |
22 | answer = (len_a - cnt) + (len_b - cnt)
23 | print(answer)
--------------------------------------------------------------------------------
/solution/data_structure2/1302/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/8adc986ae26b461eadd65abdff3cfba9
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | book = {}
11 | for i in range(N):
12 | name = input()
13 | if name not in book:
14 | book[name] = 1
15 | else:
16 | book[name] += 1
17 |
18 | book = list(book.items())
19 | book.sort(key = lambda x : (-x[1],x[0]))
20 | print(book[0][0])
21 |
--------------------------------------------------------------------------------
/solution/data_structure2/14425/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/c8831b4a36e741ddb4ae083cc32902ef
4 | #include
5 |
6 | using namespace std;
7 |
8 | unordered_set st;
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N, M; cin >> N >> M;
15 | for(int i=0;i> s;
17 | st.insert(s);
18 | }
19 |
20 | int answer = 0;
21 | for(int i=0;i> s;
23 | if(st.find(s) != st.end()) answer ++;
24 | }
25 | cout << answer;
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/data_structure2/14425/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/efd53a96cfd04ca9ab21018162c3084d
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N, M = map(int, input().split())
11 | ans = 0
12 | dic = {}
13 | for i in range(N):
14 | a = input()
15 | dic[a] = 1
16 | for i in range(M):
17 | a = input()
18 | if a in dic:
19 | ans += 1
20 | print(ans)
--------------------------------------------------------------------------------
/solution/data_structure2/1620/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/9289c0621c6f41dabd5bfc2ba190a2ce
4 | #include
5 |
6 | using namespace std;
7 |
8 | unordered_map pokemon;
9 | unordered_map pokemonSeq;
10 |
11 | int main(){
12 | ios::sync_with_stdio(false);
13 | cin.tie(0);
14 |
15 | int N, Q; cin >> N >> Q;
16 | for(int i=1;i<=N;i++){
17 | string s; cin >> s;
18 | string idx = to_string(i);
19 | pokemon[s] = idx;
20 | pokemonSeq[idx] = s;
21 | }
22 |
23 | while(Q--) {
24 | string s; cin >> s;
25 | if(isdigit(s[0])) {
26 | cout << pokemonSeq[s] << '\n';
27 | }
28 | else {
29 | cout << pokemon[s] << '\n';
30 | }
31 | }
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/solution/data_structure2/1927/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/5d73f6bad59b42a58688435c855b9580
4 | import sys
5 | import heapq
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | heap = []
12 |
13 | for i in range(N):
14 | x = int(input())
15 | if x == 0:
16 | if not heap:
17 | print(0)
18 | else:
19 | print(heapq.heappop(heap))
20 | else:
21 | heapq.heappush(heap,x)
22 |
--------------------------------------------------------------------------------
/solution/data_structure2/2075/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/9d0a68ae0d4349238f376383cbaf13e0
4 | #include
5 |
6 | using namespace std;
7 |
8 | priority_queue, greater> pq;
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N; cin >> N;
15 | for(int i=0;i> x;
18 | pq.push(x);
19 | if((int)pq.size() > N) pq.pop();
20 | }
21 | }
22 | cout << pq.top();
23 |
24 | return 0;
25 | }
26 |
--------------------------------------------------------------------------------
/solution/data_structure2/4358/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/8af3200059e848aaa9c06901b34bcef7
4 | #include
5 |
6 | using namespace std;
7 |
8 | map mp;
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | string s;
15 | int total = 0;
16 | while(getline(cin, s)) {
17 | mp[s] += 1;
18 | total += 1;
19 | }
20 |
21 | cout.precision(4);
22 | cout << fixed;
23 | for(auto &[k, v]: mp) {
24 | cout << k << ' ' << v * 100.0 / total << '\n';
25 | }
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/disjoint_set/10775/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/f71cbcd7b21940189fe6db7f99852f09
4 | #include
5 |
6 | using namespace std;
7 |
8 | constexpr int MAXN = 100005;
9 | int uf[MAXN];
10 |
11 | int find(int x) {
12 | if(uf[x] < 0) return x;
13 | return uf[x] = find(uf[x]);
14 | }
15 |
16 | bool merge(int a, int b){
17 | a = find(a);
18 | b = find(b);
19 | if(a == b)return false;
20 | uf[b] = a;
21 | return true;
22 | }
23 |
24 | int main(){
25 | ios::sync_with_stdio(false);
26 | cin.tie(0);
27 |
28 | int N, M; cin >> N >> M;
29 | for(int i=0;i<=N;i++) uf[i] = -1;
30 | int answer = 0;
31 | for(int i=0;i> x;
33 | int cur = find(x);
34 | if(cur == 0) break;
35 | merge(cur - 1, cur);
36 | answer ++;
37 | }
38 | cout << answer;
39 |
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/solution/disjoint_set/10775/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/30c745d5f3c547c28e95b5c9a76647c1
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | G = int(input())
10 | P = int(input())
11 | gates = list(range(G + 1))
12 |
13 | def find_max(x):
14 | if gates[x] != x:
15 | gates[x] = find_max(gates[x])
16 | return gates[x]
17 |
18 | answer = 0
19 | for _ in range(P):
20 | plane = int(input())
21 | gate_n = find_max(plane)
22 | if gate_n == 0:
23 | print(answer)
24 | sys.exit(0)
25 | answer += 1
26 | gates[gate_n] = gates[gate_n - 1]
27 | print(answer)
28 |
--------------------------------------------------------------------------------
/solution/disjoint_set/1717/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/681761d952b14fd58d55c93eaf11c0ef
4 | #include
5 |
6 | using namespace std;
7 |
8 | int uf[1000005];
9 |
10 | int find(int x) {
11 | if(uf[x] < 0) return x;
12 | return uf[x] = find(uf[x]);
13 | }
14 |
15 | bool merge(int a, int b) {
16 | a = find(a);
17 | b = find(b);
18 | if(a == b)return false;
19 | uf[b] = a;
20 | return true;
21 | }
22 |
23 | int main(){
24 | ios::sync_with_stdio(false);
25 | cin.tie(0);
26 |
27 | int N, M; cin >> N >> M;
28 | for(int i=0;i<=N;i++) uf[i] = -1;
29 | for(int i=0;i> t >> a >> b;
31 | if(t == 1) {
32 | if(find(a) == find(b)) cout << "YES\n";
33 | else cout << "NO\n";
34 | }
35 | else merge(a, b);
36 | }
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/solution/divide_and_conquer/1074/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/fba04fa859d540fab6cad5266780f137
4 | #include
5 |
6 | using namespace std;
7 |
8 | int solve(int y, int x, int s) {
9 | if(s == 2) return 2 * y + x;
10 | s >>= 1;
11 | int ny = y / s, nx = x / s;
12 | int nxt = ny * 2 + nx;
13 | return s * s * nxt + solve(y - ny * s, x - nx * s, s);
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int N, R, C; cin >> N >> R >> C;
21 | cout << solve(R, C, 1 << N);
22 |
23 | return 0;
24 | }
25 |
--------------------------------------------------------------------------------
/solution/divide_and_conquer/1802/main.py:
--------------------------------------------------------------------------------
1 | # // Authored by : chj3748
2 | # // Co-authored by : -
3 | # // Link : http://boj.kr/28603d67d3014c79af724768c75865af
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def origami(start, end):
10 | if start == end:
11 | return True
12 | mid = (start + end) // 2
13 | sign = True
14 | for i in range(start,mid):
15 | if status[i] == status[end-i]:
16 | sign = False
17 | break
18 | if sign:
19 | return origami(start, mid - 1) and origami(mid + 1, end)
20 | else:
21 | return False
22 |
23 | for T in range(int(input())):
24 | status = list(map(int, input()))
25 | if origami(0, len(status) - 1):
26 | answer = 'YES'
27 | else:
28 | answer = 'NO'
29 | print(answer)
--------------------------------------------------------------------------------
/solution/divide_and_conquer/18222/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/9573c1f4e8924a1b8f19ce128d132a68
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | int main(){
10 | ios::sync_with_stdio(false);
11 | cin.tie(0);
12 |
13 | ll N; cin >> N;
14 | ll cnt = 0;
15 | while(N){
16 | ll cur = 1;
17 | while(cur * 2 < N) cur <<= 1;
18 | N -= cur;
19 | cnt ++;
20 | }
21 | ll ans = ~cnt & 1;
22 | cout << ans;
23 |
24 | return 0;
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/solution/divide_and_conquer/1992/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/f4a1319fd1af4655b19f5c7cd7872b92
4 | #include
5 |
6 | using namespace std;
7 |
8 | char arr[66][66];
9 | string answer;
10 |
11 | void solve(int y, int x, int s) {
12 | bool flag = true;
13 | for(int i=0;i> N;
35 | for(int i=0;i> arr[i];
36 | solve(0, 0, N);
37 | cout << answer;
38 |
39 | return 0;
40 | }
41 |
--------------------------------------------------------------------------------
/solution/divide_and_conquer/4256/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/320c53a7494341c0b867034b59ec4377
4 | #include
5 |
6 | using namespace std;
7 |
8 | vector preorder, inorder;
9 | void solve(int L, int R, int L2, int R2) {
10 | if(L > R || L2 > R2) return;
11 | int root = preorder[L];
12 | int idx = L2;
13 | while(inorder[idx] != root) idx ++;
14 | solve(L + 1, L + idx - L2, L2, idx - 1);
15 | solve(L + idx - L2 + 1, R, idx + 1, R2);
16 | cout << root << ' ';
17 | }
18 |
19 | int main(){
20 | ios::sync_with_stdio(false);
21 | cin.tie(0);
22 |
23 | int t; cin >> t;
24 | while(t--) {
25 | int n; cin >> n;
26 | preorder.clear(); preorder.resize(n);
27 | inorder.clear(); inorder.resize(n);
28 | for(int i=0;i> preorder[i];
29 | for(int i=0;i> inorder[i];
30 | solve(0, n - 1, 0, n - 1);
31 | cout << '\n';
32 | }
33 |
34 | return 0;
35 | }
36 |
--------------------------------------------------------------------------------
/solution/divide_and_conquer/4779/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/5aee7052f3ee45d5b0294edfb54c92f1
4 | #include
5 |
6 | using namespace std;
7 |
8 | string answer;
9 |
10 | void dfs(int L, int dis) {
11 | if(dis == 1) {
12 | answer[L] = '-';
13 | return;
14 | }
15 |
16 | dis /= 3;
17 | dfs(L, dis);
18 | dfs(L + 2 * dis, dis);
19 | }
20 |
21 | int main(){
22 | ios::sync_with_stdio(false);
23 | cin.tie(0);
24 |
25 | int k;
26 | while(cin >> k) {
27 | int N = 1;
28 | for(int i = 0; i < k; i++) N *= 3;
29 | answer = string(N, ' ');
30 | dfs(0, N);
31 | cout << answer << '\n';
32 | }
33 |
34 | return 0;
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/1010/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/4a2e9c2a7d9a44e7b0c4eda2c2cae10b
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | ll DP[33][33];
10 |
11 | int main(){
12 | ios::sync_with_stdio(false);
13 | cin.tie(0);
14 |
15 | int T; cin >> T;
16 | DP[0][0] = 1;
17 | for(int i=1;i<=30;i++) {
18 | DP[i][0] = 1;
19 | for(int j=1;j<=30;j++) {
20 | DP[i][j] = DP[i - 1][j] + DP[i - 1][j - 1];
21 | }
22 | }
23 | while(T--) {
24 | int N, M; cin >> N >> M;
25 | cout << DP[M][N] << '\n';
26 | }
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/1010/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gkgg123
2 | # Co-authored by : -
3 | # Link : https://www.acmicpc.net/source/share/6c285d8c7b9c44af971645f45c06d848
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | T = int(input())
9 |
10 | dp = [[0 for _ in range(31)] for _ in range(31)]
11 | dp[0][0] = 1
12 |
13 | for num in range(1,31):
14 | dp[num][0] = 1
15 | for pick in range(1,31):
16 | dp[num][pick] = dp[num-1][pick] + dp[num-1][pick-1]
17 |
18 | for _ in range(T):
19 | N, M = map(int,input().split())
20 | print(dp[M][N])
21 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/10164/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/17e87614b6d540ca996295b147a740d5
4 | #include
5 |
6 | using namespace std;
7 |
8 | int N, M, K;
9 | int DP[22][22];
10 |
11 | int main(){
12 | ios::sync_with_stdio(false);
13 | cin.tie(0);
14 |
15 | cin >> N >> M >> K;
16 | int y = N, x = M;
17 | if(K > 0) {
18 | --K;
19 | y = K / M + 1, x = K % M + 1; // 1 - based
20 | }
21 | DP[1][1] = 1;
22 | for(int i=1;i<=y; i++) {
23 | for(int j=1;j<=x;j++) {
24 | if(i == 1 && j == 1) continue;
25 | DP[i][j] = DP[i][j - 1] + DP[i - 1][j];
26 | }
27 | }
28 | for(int i=y;i<=N;i++){
29 | for(int j=x;j<=M;j++){
30 | if(i == y && j == x) continue;
31 | DP[i][j] = DP[i][j - 1] + DP[i - 1][j];
32 | }
33 | }
34 | cout << DP[N][M];
35 |
36 | return 0;
37 | }
38 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/10870/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/4c48767c6ca14f2fbef014b726303f33
4 | #include
5 |
6 | using namespace std;
7 |
8 | int DP[25];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | DP[0] = 0;
15 | DP[1] = 1;
16 | for(int i=2;i<=20;i++){
17 | DP[i] = DP[i - 1] + DP[i - 2];
18 | }
19 |
20 | int N; cin >> N;
21 | cout << DP[N];
22 | }
23 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/11048/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/4ff69fc575384408beee739a7904de45
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N, M; cin >> N >> M;
13 | vector> DP(N + 1, vector(M + 1));
14 | for(int i=1;i<=N;i++) {
15 | for(int j=1;j<=M;j++) {
16 | int x; cin >> x;
17 | DP[i][j] = max({DP[i-1][j], DP[i][j-1], DP[i-1][j-1]}) + x;
18 | }
19 | }
20 | cout << DP[N][M];
21 |
22 | return 0;
23 | }
24 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/11053/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/67f06494af394db6958644f6cb20e8fe
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | vector V(N), DP(N);
14 | for(auto &i: V) cin >> i;
15 | for(int i=0;i V[j]) {
19 | DP[i] = max(DP[i], DP[j] + 1);
20 | }
21 | }
22 | }
23 | cout << *max_element(DP.begin(), DP.end());
24 |
25 | return 0;
26 | }
27 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/11055/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/038f38ed8c6f41149b051350cb3cabd1
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | vector V(N), DP(N);
14 | for(auto &i: V) cin >> i;
15 | for(int i=0;i V[j]) {
19 | DP[i] = max(DP[i], DP[j] + V[i]);
20 | }
21 | }
22 | }
23 | cout << *max_element(DP.begin(), DP.end());
24 |
25 | return 0;
26 | }
27 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/11660/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/c28a9148cd334f45b6edcbc40ae7ec15
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[1033][1033], dp[1033][1033];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N, Q; cin >> N >> Q;
15 | for(int i=1;i<=N;i++) {
16 | for(int j=1;j<=N;j++) {
17 | cin >> arr[i][j];
18 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + arr[i][j];
19 | }
20 | }
21 |
22 | for(int i=0;i> x1 >> y1 >> x2 >> y2;
24 | cout << dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1] << '\n';
25 | }
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/11722/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/6b74718335d14780a6213de8a6b33e42
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | arr = list(map(int, input().split()))
11 | dp = [1] * (N+1)
12 | for i in range(N):
13 | for j in range(i+1,N):
14 | if arr[i] > arr[j]:
15 | dp[j] = max(dp[i]+1, dp[j])
16 | print(max(dp))
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/11726/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/120bee5ac7444d88ad6686eecd957cd2
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | MOD = 10007
10 | N = int(input())
11 | dp = [0] * (N+2)
12 | dp[1] = 1
13 | dp[2] = 2
14 |
15 | for i in range(3,N+1):
16 | dp[i] = (dp[i-1] + dp[i-2]) % MOD
17 |
18 | print(dp[N] % MOD)
19 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/11727/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/db4e4b38ee3c4264b672837fa8fff893
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | MOD = 10007
10 | N = int(input())
11 | dp = [0] * (N+2)
12 | dp[1] = 1
13 | dp[2] = 3
14 |
15 | for i in range(3,N+1):
16 | dp[i] = (dp[i-1] + dp[i-2] * 2) % MOD
17 |
18 | print(dp[N] % MOD)
19 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/13699/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/84dabc0ec83940a9b2ee36e163f602c2
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n = int(input())
10 | t = [0] * (n+4)
11 | t[0] = 1
12 | for i in range(1,n+1):
13 | for j in range(i):
14 | t[i] += t[j] * t[i-1-j]
15 | print(t[n])
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/1463/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tallua_y
2 | // Co-authored by : -
3 | // Link : http://boj.kr/96a171e5da7746c7a51d5d348bc759a8
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(int argc, char** argv)
9 | {
10 | ios::sync_with_stdio(false);
11 | cin.tie(nullptr);
12 |
13 | size_t N;
14 | cin >> N;
15 |
16 | vector count(N + 1);
17 | count[1] = 0;
18 |
19 | for(int i = 2; i < N + 1; ++i) {
20 | count[i] = count[i - 1] + 1;
21 | if (i % 2 == 0) {
22 | count[i] = min(count[i], count[i / 2] + 1);
23 | }
24 | if (i % 3 == 0) {
25 | count[i] = min(count[i], count[i / 3] + 1);
26 | }
27 | }
28 |
29 | cout << count[N] << '\n';
30 |
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/15989/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/7363aaeb37fd489aaa5c0ad142ce8a7a
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | ll DP[10005][4];
10 |
11 | int main(){
12 | ios::sync_with_stdio(false);
13 | cin.tie(0);
14 |
15 | for(int i=1;i<=3;i++) {
16 | for(int j=1;j<=i;j++) {
17 | DP[i][j] = 1;
18 | }
19 | }
20 |
21 | for(int i=4;i<=10000;i++) {
22 | for(int j=1;j<=3;j++) {
23 | for(int k=1;k<=j;k++) {
24 | DP[i][j] += DP[i - j][k];
25 | }
26 | }
27 | }
28 |
29 | int T; cin >> T;
30 | while(T--){
31 | int n; cin >> n;
32 | ll answer = 0;
33 | for(int i=1;i<=3;i++) answer += DP[n][i];
34 | cout << answer << '\n';
35 | }
36 |
37 | return 0;
38 | }
39 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/15990/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/7a8607af45e042df9e44b48962548e1a
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | ll DP[100005][4];
10 | const ll MOD = 1e9 + 9;
11 | const int MAXN = 100000;
12 |
13 | int main(){
14 | ios::sync_with_stdio(false);
15 | cin.tie(0);
16 |
17 | for(int i=1;i<=3;i++) DP[i][i] = DP[3][i] = 1;
18 | for(int i=4;i<=MAXN;i++){
19 | for(int j=1;j<=3;j++){
20 | for(int k=1;k<=3;k++){
21 | if(j == k)continue;
22 | DP[i][j] = (DP[i][j] + DP[i - j][k]) % MOD;
23 | }
24 | }
25 | }
26 | int T; cin >> T;
27 | while(T--){
28 | int n; cin >> n;
29 | cout << (DP[n][1] + DP[n][2] + DP[n][3]) % MOD << '\n';
30 | }
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/17626/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : osy0056
2 | # Co-authored by : -
3 | # Link : http://boj.kr/49cc21bd78694086b7b2864e8bc6fa69
4 | import math
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | n = int(input())
11 | dp = [0] * 50001
12 | INF = float('inf')
13 |
14 | dp[0] = 0
15 | dp[1] = 1
16 | for i in range(2, n+1):
17 | min_value = INF
18 | for k in range(1, math.floor(math.sqrt(i)) + 1):
19 | min_value = min(min_value, dp[i - k * k])
20 | dp[i] = min_value + 1
21 | print(dp[n])
22 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/1890/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/73b9e168131842dd90aa2b02380cb0fa
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n = int(input())
10 | lst = []
11 | for _ in range(n):
12 | lst.append(list(map(int, input().split())))
13 |
14 | dp = [[0]*n for _ in range(n)]
15 | dp[0][0] = 1
16 |
17 | for i in range(n):
18 | for j in range(n):
19 | if lst[i][j] == 0:
20 | continue
21 | jump = lst[j][i]
22 |
23 | if j+jump < n:
24 | dp[j+jump][i] += dp[j][i]
25 | if i+jump < n:
26 | dp[j][i+jump] += dp[j][i]
27 |
28 | print(dp[-1][-1])
29 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/19622/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/25c02a4ea33e493f9f1320165f1a5f95
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | meetings = []
11 | for _ in range(N):
12 | meetings.append(list(map(int, input().split())))
13 | dp = [0] * N
14 |
15 | for i in range(N):
16 | if i == 0:
17 | dp[0] = meetings[0][2]
18 | elif i == 1:
19 | dp[1] = max(meetings[1][2], dp[0])
20 | else:
21 | dp[i] = max(dp[i-2] + meetings[i][2], dp[i-1])
22 |
23 | print(dp[N - 1])
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/21317/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/7b74ffa836cb49029ffc4ee08767c9db
4 | #include
5 |
6 | using namespace std;
7 |
8 | int DP[105][2], arr[105][2];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N; cin >> N;
15 | for(int i=1;i> arr[i][0] >> arr[i][1];
16 | int k; cin >> k;
17 | for(int i=2;i<105;i++) DP[i][0] = DP[i][1] = INT_MAX;
18 | for(int i=1;ik:
16 | continue
17 | for j in range(i+1, n):
18 | need = (j-i) * (1 + abs(rocks[i]-rocks[j]))
19 | dp[j] = min(dp[j], need)
20 |
21 | if dp[n-1]>k:
22 | print('NO')
23 | else:
24 | print('YES')
25 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/2294/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/4c2449f200e440f39d4ba8e1e71601b6
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n, k = map(int, input().split())
10 | coin = set([int(input()) for _ in range(n)]) #중복 동전 제거
11 | dp = [0]*(k+1)
12 |
13 | for i in range(1, k+1):
14 | possible = []
15 | for c in coin:
16 | if i-c >= 0 and dp[i-c] >= 0: # i-c원 경우에 c원 동전을 추가해서 i원을 만들 수 있는 경우
17 | possible.append(dp[i-c])
18 | if possible:
19 | dp[i] = min(possible) + 1 # optimal 값 + 1
20 | else:
21 | dp[i] = -1 #불가능!
22 |
23 | print(dp[k])
24 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/2579/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/3d9ff337b9bc420a975cf6783b298212
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n = int(input())
10 | lst = [int(input()) for _ in range(n)]
11 |
12 | if n > 3:
13 | dp = [lst[0], lst[0]+lst[1], max(lst[0], lst[1])+lst[2]] # 3번째 칸까지는 먼저 구하기
14 | for i in range(3, n):
15 | next_max = max(dp[i-2], dp[i-3]+lst[i-1]) # max(2칸 전에서 i번째 경우, 3칸-1칸 전 밟고 i번째 오는 경우)
16 | dp.append(next_max+lst[i])
17 | print(dp[-1])
18 | else:
19 | print(sum(lst)) #2칸 이하면 모두 밟는게 최대
20 |
21 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/2748/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/b5fdc8a3c2c84784a6b308c98e4aa7e9
4 | #include
5 |
6 | using namespace std;
7 |
8 | long long DP[99];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | DP[1] = 1;
15 | for(int i=2;i<=90;i++)DP[i] = DP[i-1] + DP[i-2];
16 |
17 | int N; cin >> N;
18 | cout << DP[N];
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/2839/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/48cb7b8049444c7eb5cdc5752dd5eb66
4 | #include
5 |
6 | using namespace std;
7 |
8 | int DP[5005];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | const int INF = 50000;
15 | for(int i=1;i<=5000;i++) DP[i] = INF;
16 | DP[3] = 1;
17 |
18 | for(int i=5;i<=5000;i++) DP[i] = min({ DP[i], DP[i - 3] + 1, DP[i - 5] + 1});
19 |
20 | int N; cin >> N;
21 | if(DP[N] == INF)DP[N] = -1;
22 | cout << DP[N];
23 | }
24 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/9095/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : osy0056
2 | # Co-authored by : -
3 | # Link : http://boj.kr/da3d10e3ebc945349d778cb06444ffb1
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | tc = int(input())
10 |
11 | dp = [0] * 15
12 | dp[1] = 1
13 | dp[2] = 2
14 | dp[3] = 4
15 | for i in range(4, 12):
16 | dp[i] = dp[i - 3] + dp[i - 2] + dp[i - 1]
17 |
18 | for _ in range(tc):
19 | n = int(input())
20 |
21 | print(dp[n])
22 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/9461/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/369118cac2b14c75866029d361c95152
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def recursion(x):
10 | if x == 1 or x == 2 or x == 3:
11 | return 1
12 | if dp[x] == 0:
13 | dp[x] = recursion(x-2) + recursion(x-3)
14 | return dp[x]
15 |
16 | T = int(input())
17 | dp = [0] * 101
18 | for i in range(T):
19 | N = int(input())
20 | print(recursion(N))
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/9465/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/39643867e537451a9ae49e16ec4f2f3f
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | t = int(input())
10 | for _ in range(t):
11 | n = int(input())
12 | lst = [list(map(int, input().split())) for i in range(2)]
13 | # 새로 dp 행렬 만들 필요 없이 스티커 값이 저장된 lst 배열에서 dp 수행
14 |
15 | if n > 1:
16 | # 경우의 수 오직 1개
17 | lst[0][1] += lst[1][0]
18 | lst[1][1] += lst[0][0]
19 |
20 | if n > 2:
21 | for i in range(2, n):
22 | #대각선에 있는 스티커를 고를 지, 거기서 한 칸 더 떨어져 있는 스티커를 고를 지 선택
23 | lst[0][i] += max(lst[1][i-1], lst[1][i-2])
24 | lst[1][i] += max(lst[0][i-1], lst[0][i-2])
25 |
26 | print(max(lst[0][-1], lst[1][-1]))
27 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_1/9655/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : yj2221
2 | # Co-authored by : -
3 | # Link : http://boj.kr/f4bdc1e4d3f34003a1080645069b0ee7
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | dp = ['' for _ in range(1001)]
10 | dp[1] = 'SK'
11 |
12 | for i in range(1,1000):
13 | if dp[i]=="SK":
14 | if dp[i+1]=='':
15 | dp[i+1] = 'CY'
16 | if i+3<1000:
17 | dp[i+3] = 'CY'
18 | elif dp[i]=='CY':
19 | if dp[i+1]=='':
20 | dp[i+1] = 'SK'
21 | if i+3<1000:
22 | dp[i+3] = 'SK'
23 |
24 | print(dp[int(input())])
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/11909/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/ae5c8a4d54074ca1a2af053ecbad14a1
4 | #include
5 |
6 | using namespace std;
7 |
8 | const int INF = 0x3f3f3f3f;
9 |
10 | int main() {
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N; cin >> N;
15 | vector> V(N, vector(N));
16 | vector> DP(N, vector(N, INF));
17 | DP[0][0]=0;
18 | for(int i=0;i> V[i][j];
21 | if(j > 0){
22 | int d = max(0, V[i][j] - V[i][j-1] + 1);
23 | DP[i][j]=min(DP[i][j], DP[i][j-1] + d);
24 | }
25 | if(i > 0){
26 | int d = max(0, V[i][j] - V[i-1][j] + 1);
27 | DP[i][j] = min(DP[i][j], DP[i-1][j] + d);
28 | }
29 | }
30 | }
31 | cout << DP[N-1][N-1];
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/11909/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/a7949813f6e74a918f1f18b458ea5f8c
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | inf = float('inf')
11 | n = int(input())
12 | prison = [list(map(int, input().split())) for _ in range(n)]
13 | dp = [[inf] * n for _ in range(n)]
14 | dp[0][0] = 0
15 | dirx = [1, 0]
16 | diry = [0, 1]
17 |
18 | q = deque()
19 | q.append((0, 0))
20 | while q:
21 | x, y = q.popleft()
22 | for dx, dy in zip(dirx, diry):
23 | nx, ny = x + dx, y + dy
24 | if 0 <= nx < n and 0 <= ny < n:
25 | cost = prison[nx][ny] - prison[x][y] + 1
26 | if cost < 1:
27 | cost = 0
28 | if dp[nx][ny] > dp[x][y] + cost:
29 | q.append((nx, ny))
30 | dp[nx][ny] = dp[x][y] + cost
31 |
32 | print(dp[n - 1][n - 1])
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/12015/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/c74669b0329d4d59ad5145504275e7ac
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | vector V(N), DP;
14 | for(auto &i: V) cin >> i;
15 | for(int i=0;i= N or dy < 0 or dy >= M:
19 | continue
20 | if arr[dx][dy] < arr[x][y]:
21 | dp[x][y] += DFS(dx,dy)
22 | return dp[x][y]
23 |
24 | N, M = map(int, input().split())
25 | arr = []
26 | nx = [-1, 0, 1, 0]
27 | ny = [0, -1, 0, 1]
28 | dp = [[-1 for i in range(M)] for j in range(N)]
29 | for i in range(N):
30 | arr.append(list(map(int, input().split())))
31 | print(DFS(0,0))
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/17485/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : yj2221
2 | # Co-authored by : tony9402
3 | # Link : http://boj.kr/1b3b764580774cf799a9a9606d12fec0
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | INF = 99999999
10 | n, m = map(int,input().split())
11 | board = [list(map(int,input().split())) for i in range(n)]
12 | dp = [[[INF] * m for _ in range(n)] for _ in range(3)]
13 |
14 | for i in range(m):
15 | dp[0][0][i] = board[0][i]
16 | dp[1][0][i] = board[0][i]
17 | dp[2][0][i] = board[0][i]
18 |
19 | for i in range(1, n):
20 | for j in range(m):
21 | dp[0][i][j] = min(dp[1][i-1][j], dp[2][i-1][j]) + board[i][j]
22 | if j!=m-1:
23 | dp[1][i][j] = min(dp[0][i-1][j+1], dp[2][i-1][j+1]) + board[i][j]
24 | if j!=0:
25 | dp[2][i][j] = min(dp[1][i-1][j-1], dp[0][i-1][j-1]) + board[i][j]
26 |
27 | print(min(min(dp[0][n-1]),min(dp[1][n-1]), min(dp[2][n-1])))
28 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/18427/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : yj2221
2 | # Co-authored by : tony9402
3 | # Link : http://boj.kr/069cb935ee08462280976f02691b40ca
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N, M, H = map(int, input().split())
11 |
12 | students = []
13 | for _ in range(N):
14 | students.append(list(map(int, input().split())))
15 |
16 | dp = [1] + [0] * (H+1)
17 |
18 | for i in range(N):
19 | for j in range(H, -1, -1):
20 | for k in students[i]:
21 | if j - k >= 0:
22 | dp[j] = (dp[j] + dp[j - k]) % 10007
23 |
24 | print(dp[H])
25 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/5557/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/23af4ad30fe24df4a71cdd3db925087c
4 | import sys
5 | import heapq
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | numbers = list(map(int, input().split()))
12 | dp = [[0] * 21 for _ in range(N - 1)]
13 | dp[0][numbers[0]] = 1
14 | for i in range(1, N - 1):
15 | for j in range(21):
16 | if dp[i - 1][j] <= 0:
17 | continue
18 | for oper in (1, -1):
19 | temp = j + oper * numbers[i]
20 | if 0 <= temp < 21:
21 | dp[i][temp] += dp[i - 1][j]
22 | answer = dp[N - 2][numbers[N - 1]]
23 | print(answer)
24 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/7579/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/7cc3cfd9bda64cfe8af9ff0a99c789a9
4 | #include
5 |
6 | using namespace std;
7 |
8 | int DP[10001], N, M, mem[101], cost[101];
9 | int sum_cost;
10 |
11 | int main(){
12 | ios::sync_with_stdio(false);
13 | cin.tie(0);
14 |
15 | cin >> N >> M;
16 | for(int i=0;i> mem[i];
17 | for(int i=0;i> cost[i];
19 | sum_cost += cost[i];
20 | }
21 | for(int i=0;i=0;j--){
23 | if(j - cost[i] >= 0){
24 | DP[j] = max(DP[j], DP[j - cost[i]] + mem[i]);
25 | }
26 | }
27 | }
28 | int answer = 0;
29 | for(int i=0;i<=sum_cost;i++) {
30 | if(DP[i] >= M) {
31 | answer = i;
32 | break;
33 | }
34 | }
35 | cout << answer;
36 |
37 | return 0;
38 | }
39 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_2/9251/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/fa5cc7635c6b497495804944b5696e12
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | string1 = '_' + input()
10 | string2 = '_' + input()
11 | len1 = len(string1)
12 | len2 = len(string2)
13 | dp = [[0] * len2 for _ in range(len1)]
14 |
15 | for i in range(1, len1):
16 | for j in range(1, len2):
17 | if string1[i] == string2[j]:
18 | dp[i][j] = dp[i - 1][j - 1] + 1
19 | else:
20 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
21 |
22 | print(dp[len1 - 1][len2 - 1])
23 |
24 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_on_trees/15681/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/acc4b75def8e48d995163a01bcc23068
4 | #include
5 |
6 | using namespace std;
7 |
8 | vector> graph;
9 | vector siz;
10 |
11 | void dfs(int cur, int prv) {
12 | siz[cur] = 1;
13 |
14 | for(auto &nxt: graph[cur]) {
15 | if(nxt == prv) continue;
16 | dfs(nxt, cur);
17 | siz[cur] += siz[nxt];
18 | }
19 | }
20 |
21 | int main(){
22 | ios::sync_with_stdio(false);
23 | cin.tie(0);
24 |
25 | int N, R, Q; cin >> N >> R >> Q;
26 | graph.resize(N + 1);
27 | siz.resize(N + 1);
28 | for(int i=1;i> a >> b;
30 | graph[a].push_back(b);
31 | graph[b].push_back(a);
32 | }
33 | dfs(R, R);
34 |
35 | while(Q--) {
36 | int x;cin >> x;
37 | cout << siz[x] << '\n';
38 | }
39 |
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/solution/dynamic_programming_on_trees/1949/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/627e3feffb1842298d6d7ce3093e807b
4 | #include
5 |
6 | using namespace std;
7 |
8 | const int MAXN = 10005;
9 | vector graph[MAXN];
10 | int cost[MAXN], DP[MAXN][2];
11 |
12 | void dfs(int cur, int prv) {
13 | for(auto &nxt: graph[cur]) {
14 | if(nxt == prv) continue;
15 | dfs(nxt, cur);
16 | DP[cur][0] += max(DP[nxt][0], DP[nxt][1]);
17 | DP[cur][1] += DP[nxt][0];
18 | }
19 | DP[cur][1] += cost[cur];
20 | }
21 |
22 | int main(){
23 | ios::sync_with_stdio(false);
24 | cin.tie(0);
25 |
26 | int n; cin >> n;
27 | for(int i=1;i<=n;i++) cin >> cost[i];
28 | for(int i=1;i< n;i++) {
29 | int a, b; cin >> a >> b;
30 | graph[a].push_back(b);
31 | graph[b].push_back(a);
32 | }
33 |
34 | dfs(1, 1);
35 | cout << max(DP[1][0], DP[1][1]) << '\n';
36 |
37 | return 0;
38 | }
39 |
40 |
--------------------------------------------------------------------------------
/solution/graph_traversal/1058/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gkgg123
2 | # Co-authored by : -
3 | # Link : https://www.acmicpc.net/source/share/a369084903d348d2966ab752ccb745ec
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | def dfs(idx):
9 | global N
10 | visited = [ True for _ in range(N) ]
11 | visited[idx] = False
12 | stack = [ (idx, 0) ]
13 | total = 0
14 | while stack:
15 | curent_x, p_cnt = stack.pop()
16 | for either_x in range(N):
17 | if arr[curent_x][either_x] == 'Y':
18 | if visited[either_x] and p_cnt <= 1:
19 | visited[either_x] = False
20 | stack.append((either_x, p_cnt+1))
21 | total += 1
22 | return total
23 |
24 | N = int(input())
25 | arr = [list(input()) for _ in range(N)]
26 | max_number = 0
27 |
28 | for i in range(N):
29 | cu_cnt = dfs(i)
30 | max_number = max(max_number,cu_cnt)
31 |
32 | print(max_number)
33 |
--------------------------------------------------------------------------------
/solution/graph_traversal/11724/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/e86763e30a974134ade5e3bdf6b7936d
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | def DFS(now):
11 | visited[now] = 1
12 | for i in arr[now]:
13 | if visited[i] == 0:
14 | DFS(i)
15 |
16 | N, M = map(int, input().split())
17 | arr = [[] for i in range(N+1)]
18 | visited = [0] * (N+1)
19 | ans = 0
20 |
21 | for i in range(M):
22 | u,v = map(int, input().split())
23 | arr[u].append(v)
24 | arr[v].append(u)
25 |
26 | for i in range(1,N+1):
27 | if visited[i] == 0:
28 | DFS(i)
29 | ans += 1
30 |
31 | print(ans)
--------------------------------------------------------------------------------
/solution/graph_traversal/11725/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : seastar105
2 | // Co-authored by : -
3 | // Link : http://boj.kr/c28e084e8adc4eb78851cc603e7f5b37
4 | #include
5 | using namespace std;
6 | int N;
7 | vector> G(100005);
8 | int parent[100005];
9 |
10 | void dfs(int cur) {
11 | for (const int &nxt : G[cur]) {
12 | if (parent[cur] == nxt) continue;
13 | parent[nxt] = cur;
14 | dfs(nxt);
15 | }
16 | }
17 |
18 | int main() {
19 | cin.tie(nullptr);
20 | ios::sync_with_stdio(false);
21 | cin >> N;
22 | for (int i = 0; i < N - 1; ++i) {
23 | int u, v;
24 | cin >> u >> v;
25 | G[u].push_back(v);
26 | G[v].push_back(u);
27 | }
28 | dfs(1);
29 | for (int i = 2; i <= N; ++i) cout << parent[i] << '\n';
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/solution/graph_traversal/14716/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : tony9402
3 | # Link : http://boj.kr/b5d154fcfb1c46e6a99e702c35f4eb26
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | M, N = map(int, input().split())
10 | banner = [list(map(int, input().split())) for _ in range(M)]
11 | dirx = (1, 0, -1, 0, 1, 1, -1, -1)
12 | diry = (0, 1, 0, -1, 1, -1, 1, -1)
13 |
14 | def find_string(row, col):
15 | stack = [(row, col)]
16 | banner[row][col] = 0
17 | while stack:
18 | x, y = stack.pop()
19 | for dx, dy in zip(dirx, diry):
20 | nx, ny = x + dx, y + dy
21 | if 0 <= nx < M and 0 <= ny < N and banner[nx][ny]:
22 | stack.append((nx, ny))
23 | banner[nx][ny] = 0
24 | return 1
25 |
26 | string_cnt = 0
27 | for i in range(M):
28 | for j in range(N):
29 | if banner[i][j]:
30 | string_cnt += find_string(i, j)
31 |
32 | print(string_cnt)
33 |
--------------------------------------------------------------------------------
/solution/graph_traversal/16174/main.py:
--------------------------------------------------------------------------------
1 | # // Authored by : chj3748
2 | # // Co-authored by : tony9402
3 | # // Link : http://boj.kr/4d17bddef7af409ebdb384693b3f96d9
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | dirx = (0, 1)
11 | diry = (1, 0)
12 | N = int(input())
13 | maps = [list(map(int, input().split())) for _ in range(N)]
14 | inf = float('inf')
15 |
16 | q = deque()
17 | q.append((0, 0))
18 | while q:
19 | x, y = q.popleft()
20 | if x == N - 1 and y == N - 1:
21 | print('HaruHaru')
22 | exit(0)
23 |
24 | for dx, dy in zip(dirx, diry):
25 | nx, ny = x + dx * maps[x][y], y + dy * maps[x][y]
26 | if 0 <= nx < N and 0 <= ny < N:
27 | q.append((nx, ny))
28 | maps[x][y] = inf
29 |
30 | print('Hing')
31 |
--------------------------------------------------------------------------------
/solution/graph_traversal/16953/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/e6b9e893d80948c5b739caaf3030e55f
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def DFS(x,ct):
10 |
11 | if x == B:
12 | ans.append(ct)
13 | return
14 |
15 | if x * 10 + 1 <= B:
16 | DFS(x * 10 + 1,ct+1)
17 |
18 | if x * 2 <= B:
19 | DFS(x*2,ct+1)
20 |
21 | A, B = map(int, input().split())
22 | ans = []
23 | DFS(A,1)
24 |
25 | if ans:
26 | print(ans[0])
27 |
28 | else:
29 | print(-1)
--------------------------------------------------------------------------------
/solution/graph_traversal/2606/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : seastar105
2 | // Co-authored by : -
3 | // Link : http://boj.kr/f4a2942a69264111a18e39d0c384209e
4 | #include
5 | using namespace std;
6 | vector> G(105);
7 | bool vis[105];
8 | int N, M;
9 |
10 | void dfs(int cur) {
11 | vis[cur] = true;
12 | for (const int &nxt : G[cur]) {
13 | if (!vis[nxt]) dfs(nxt);
14 | }
15 | }
16 |
17 | int main() {
18 | cin.tie(nullptr);
19 | ios::sync_with_stdio(false);
20 | cin >> N >> M;
21 | for (int i = 0; i < M; ++i) {
22 | int u, v;
23 | cin >> u >> v;
24 | G[u].push_back(v);
25 | G[v].push_back(u);
26 | }
27 | dfs(1);
28 | int ans = 0;
29 | for (int i = 1; i <= N; ++i) ans += vis[i];
30 | cout << ans - 1 << '\n'; // except vertex 1
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/solution/graph_traversal/4963/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/27c8f362bf2c4defbe77f079e9eaa89e
4 | import sys
5 | sys.setrecursionlimit(10000)
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | def DFS(x, y):
11 | arr[x][y] = 0
12 | for i in range(8):
13 | dx = nx[i] + x
14 | dy = ny[i] + y
15 | if dx < 0 or dx >= h or dy < 0 or dy >= w:
16 | continue
17 | if arr[dx][dy] == 1:
18 | DFS(dx,dy)
19 |
20 | nx = [-1,-1,-1,0,1,1,1,0]
21 | ny = [-1,0,1,1,1,0,-1,-1]
22 |
23 | while True:
24 | w, h = map(int, input().split())
25 | if w == 0 and h == 0:
26 | break
27 | arr = []
28 | ct = 0
29 | for i in range(h):
30 | arr.append(list(map(int, input().split())))
31 | for i in range(h):
32 | for j in range(w):
33 | if arr[i][j] == 1:
34 | DFS(i,j)
35 | ct += 1
36 | print(ct)
37 |
--------------------------------------------------------------------------------
/solution/greedy/11000/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/c2436fde865f49fda490c2dcf1c6dba1
4 | import sys
5 | import heapq
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | lecture = []
12 | for _ in range(N):
13 | heapq.heappush(lecture, list(map(int, input().split())))
14 |
15 | end_points = []
16 | while lecture:
17 | l = heapq.heappop(lecture)
18 |
19 | if end_points:
20 | if l[0] >= end_points[0]:
21 | heapq.heappop(end_points)
22 | heapq.heappush(end_points, l[1])
23 |
24 | print(len(end_points))
25 |
--------------------------------------------------------------------------------
/solution/greedy/11399/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/f1c91eae2a2245c599c5d1bbc9cde9f5
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | arr = list(map(int, input().split()))
11 | arr.sort()
12 | total = 0
13 | waiting = 0
14 |
15 | for i in range(len(arr)):
16 | total += arr[i] + waiting
17 | waiting += arr[i]
18 |
19 | print(total)
--------------------------------------------------------------------------------
/solution/greedy/11508/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/09487b6e14e044f8a477a743e37bce94
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | arr = []
11 | total = 0
12 |
13 | for i in range(N):
14 | arr.append(int(input()))
15 |
16 | arr.sort(reverse = True)
17 |
18 | ct = 1
19 |
20 | for i in range(N):
21 | if ct % 3 == 0:
22 | ct = 1
23 | continue
24 |
25 | total += arr[i]
26 | ct += 1
27 |
28 | print(total)
--------------------------------------------------------------------------------
/solution/greedy/13164/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/cfb369b6a5134cfa9c0859eab5464c47
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | n, k = map(int, input().split())
9 | lst = list(map(int, input().split())) # 정렬된 상태로 들어옴
10 | sub = sorted([lst[i+1] - lst[i] for i in range(n-1)]) # 원생 간 키 차이 정렬
11 | print(sum(sub[:(n-k)])) # Greedy하게 n-k개만 선택
--------------------------------------------------------------------------------
/solution/greedy/13305/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/bdcb1993d67a400c8293683e5c7dbf7d
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | vector cost, dis, prefix;
10 |
11 | int main(){
12 | ios::sync_with_stdio(false);
13 | cin.tie(0);
14 |
15 | int N; cin >> N;
16 | cost.resize(N);
17 | dis.resize(N - 1);
18 | prefix.resize(N);
19 | for(int i=0;i> dis[i];
20 | for(int i=0;i> cost[i];
23 | }
24 | int R = 0;
25 | ll answer = 0;
26 | for(int i=0;i= i:
15 | L += 1
16 | print(L)
17 |
--------------------------------------------------------------------------------
/solution/greedy/1758/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/c3b5f907db554df093b6d9ea748fed35
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | arr = []
12 | ans = 0
13 | for i in range(N):
14 | arr.append(int(input()))
15 | arr.sort(key = lambda x : -x)
16 | for i in range(len(arr)):
17 | tip = arr[i] - i
18 | if tip > 0:
19 | ans += tip
20 | print(ans)
--------------------------------------------------------------------------------
/solution/greedy/1817/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/b60f3d6809514134b0740ee31eeb763a
4 | import sys
5 | from collections import deque
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N, M = map(int, input().split())
11 | ans = 0
12 | if N == 0:
13 | print(ans)
14 | else:
15 | queue = deque(list(map(int, input().split())))
16 | while queue:
17 | weight = 0
18 | while queue and weight + queue[0] <= M:
19 | weight += queue.popleft()
20 | ans += 1
21 | print(ans)
22 |
--------------------------------------------------------------------------------
/solution/greedy/1931/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/7150be3ff485402ebb4c5011fa88ac7d
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | arr = []
11 | total = 1
12 |
13 | for i in range(N):
14 | a,b = map(int, input().split())
15 | arr.append((a,b))
16 |
17 | arr.sort(key = lambda x : (x[1],x[0]))
18 | end_time = arr[0][1]
19 |
20 | for i in range(1,N):
21 | if arr[i][0] >= end_time:
22 | end_time = arr[i][1]
23 | total += 1
24 |
25 | print(total)
--------------------------------------------------------------------------------
/solution/greedy/19598/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/2b9516c8789c4a42849250efcef845c7
4 | import heapq
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n = int(input())
10 | lst = sorted([list(map(int, input().split())) for _ in range(n)])
11 | heap = [lst[0][1]]
12 | for i in range(1, n):
13 | if heap[0] > lst[i][0]: # 새로운 회의실 필요
14 | heapq.heappush(heap, lst[i][1])
15 | else: # 기존 회의실 이어서 사용 가능
16 | heapq.heappop(heap)
17 | heapq.heappush(heap, lst[i][1])
18 |
19 | print(len(heap))
--------------------------------------------------------------------------------
/solution/greedy/20115/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/cd5989ea170a46c9aba0b049b70f4ee6
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | arr = list(map(int, input().split()))
12 | arr.sort()
13 | for i in range(len(arr)-1):
14 | arr[-1] += arr[i] / 2
15 | print(arr[-1])
--------------------------------------------------------------------------------
/solution/greedy/20300/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : dart
2 | // Co-authored by : -
3 | // Link : http://boj.kr/50c45e2707d946d98571bbeb52348d60
4 | #include
5 | using namespace std;
6 | typedef long long ll;
7 |
8 | int n;
9 | ll arr[10005];
10 | ll ans = 0;
11 |
12 | int main() {
13 | ios::sync_with_stdio(false);
14 | cin.tie(nullptr);
15 | cout.tie(nullptr);
16 |
17 | cin >> n;
18 | for (int i = 0; i < n; i++) {
19 | cin >> arr[i];
20 | }
21 | sort(arr, arr + n);
22 | //최대 원소와 최소 원소가 합쳐지는 게 근손실이 최소이다
23 | if (n % 2 == 0) {
24 | for (int i = 0; i < n / 2; i++) {
25 | ans = max(ans, arr[i] + arr[n - i - 1]);
26 | }
27 | }
28 | //홀수일 때는 마지막 하나의 운동기구가 남는다
29 | else {
30 | for (int i = 0; i < n / 2; i++) {
31 | ans = max(ans, arr[i] + arr[n - i - 2]);
32 | }
33 | ans = max(ans, arr[n - 1]);
34 | }
35 | cout << ans << "\n";
36 |
37 |
38 | return 0;
39 | }
--------------------------------------------------------------------------------
/solution/greedy/20365/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : dart
2 | // Co-authored by : -
3 | // Link : http://boj.kr/50c45e2707d946d98571bbeb52348d60
4 | #include
5 | using namespace std;
6 | typedef long long ll;
7 |
8 | int n;
9 | string word;
10 | int red = 0, blue = 0;
11 |
12 | int main() {
13 | ios::sync_with_stdio(false);
14 | cin.tie(nullptr);
15 | cout.tie(nullptr);
16 |
17 | cin >> n >> word;
18 |
19 | for (int i = 0; i < n; i++) {
20 | if (i == 0) {
21 | if (word[i] == 'B') { blue++; }
22 | if (word[i] == 'R') { red++; }
23 | }
24 | else {
25 | if (word[i] == 'B' && word[i - 1] == 'R') { blue++; }
26 | if (word[i] == 'R' && word[i - 1] == 'B') { red++; }
27 | }
28 | }
29 | cout << min(red, blue) + 1 << "\n";
30 |
31 |
32 | return 0;
33 | }
--------------------------------------------------------------------------------
/solution/greedy/2141/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/8be8b254ebe24830805a22953bdb3fcc
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | n = int(input())
9 | lst = [list(map(int, input().split())) for _ in range(n)]
10 | lst.sort() # 마을 위치 따라 먼저 정렬
11 |
12 | dist = 0
13 | r_man, l_man = 0, 0
14 | for i in range(n):
15 | tmp = lst[i][0] - lst[0][0]
16 | dist += tmp*lst[i][1] # (최초 마을 ~ i번째 마을 거리) * 사람 수
17 | r_man += lst[i][1] #현재 위치보다 오른똑에 있는 사람 수
18 |
19 | min_dist = dist
20 | sol = lst[0][0]
21 | for i in range(1, n):
22 | tmp = lst[i][0] - lst[i-1][0]
23 | l_man += lst[i-1][1] # 이동 시 왼쪽에 있게 될 사람 추가
24 | r_man -= lst[i-1][1] # 이동 시 더 이상 오른쪽에 있지 않는 사람 제거
25 | dist = dist + (l_man - r_man)*tmp # 거리 계산
26 |
27 | if min_dist > dist: #업데이트
28 | min_dist = dist
29 | sol = lst[i][0]
30 |
31 | print(sol)
32 |
--------------------------------------------------------------------------------
/solution/greedy/21758/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/ba24cf477f634be3b516d7475296bf67
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | const int MAXN = 100000;
10 | ll arr[MAXN + 5], prefix[MAXN + 5];
11 |
12 | ll psum(int l, int r) {
13 | return prefix[r] - prefix[l - 1];
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int N; cin >> N;
21 | for(int i = 1; i <= N; i++) {
22 | cin >> arr[i];
23 | prefix[i] = prefix[i - 1] + arr[i];
24 | }
25 |
26 | ll answer = 0;
27 | for(int i = 2; i < N; i++ ){
28 | answer = max(answer, psum(2, i) + psum(i, N - 1));
29 | answer = max(answer, psum(2, N) + psum(i + 1, N) - arr[i]);
30 | answer = max(answer, psum(1, N - 1) + psum(1, i - 1) - arr[i]);
31 | }
32 | cout << answer;
33 |
34 | return 0;
35 | }
36 |
--------------------------------------------------------------------------------
/solution/greedy/2212/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/2b4e1797973e4e16a3b38ff778023e3e
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | n = int(input())
9 | k = int(input())
10 | lst = sorted(list(set(map(int, input().split())))) # 중복 제거 후 정렬
11 | distance = sorted([lst[i]-lst[i-1] for i in range(1, len(lst))]) # 센서 사이 거리 계산 후 정렬
12 | print(sum(distance[:(len(lst)-k)])) # 그리디하게 최장 거리 k개 제외한 게 정답
13 |
--------------------------------------------------------------------------------
/solution/greedy/2812/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/632e2f63fb4a44caacbddb53207f2279
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | n, k = map(int, input().split())
9 | t = k # 미래를 위해 저장
10 | num = list(input())
11 | stack = [] # 정답 저장 및 지울 후보 저장
12 |
13 | for i in range(n):
14 | while k>0 and stack: # 지울 수 있는 숫자가 있을 때
15 | if stack[-1] < num[i]: # 지우는 게 이득이면
16 | stack.pop()
17 | k -= 1
18 | else:
19 | break
20 | stack.append(num[i])
21 |
22 | print(*stack[:(n-t)], sep='') #남아있는 숫자는 n-(최초 k) => t를 여기서 사용
--------------------------------------------------------------------------------
/solution/implementation/10994/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/67887b7c21504d718bfffa859b209589
4 | #include
5 |
6 | using namespace std;
7 |
8 | char stars[444][444];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int n; cin >> n;
15 | int N = 4 * (n - 1) + 1;
16 | for(int k=0;k<=N/2;k+=2){
17 | int L = N - k * 2;
18 | for(int i=0;i
5 |
6 | using namespace std;
7 |
8 | string convert(char s, bool fillzero=false){
9 | int n = s - 48;
10 | string ret = "";
11 | while(n){
12 | ret += (n % 2) + '0';
13 | n /= 2;
14 | }
15 | while(fillzero && (int)ret.size() < 3) ret += '0';
16 | reverse(ret.begin(), ret.end());
17 | return ret;
18 | }
19 |
20 | int main(){
21 | ios::sync_with_stdio(false);
22 | cin.tie(0);
23 |
24 | string s;cin >> s;
25 | if(s == "0") {
26 | cout << 0;
27 | return 0;
28 | }
29 | string ans="";
30 | for(int i=0;i<(int)s.size();i++){
31 | ans += convert(s[i], i != 0);
32 | }
33 | cout << ans;
34 |
35 | return 0;
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/solution/implementation/1212/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/7d0d2bcb568d4c3d86770e520838ef65
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def change(num, first = False):
10 | ret = ''
11 | while num:
12 | ret += chr(num % 2 + 48)
13 | num //= 2
14 | while len(ret) < 3:
15 | ret += '0'
16 |
17 | idx = 3
18 | if first:
19 | while idx > 1 and ret[idx - 1] == '0':
20 | idx -= 1
21 | return ret[:idx][::-1]
22 |
23 | N = input()
24 | isFirst = True
25 | for i in range(len(N)):
26 | print(change(int(N[i]), isFirst),end='')
27 | isFirst = False
28 |
--------------------------------------------------------------------------------
/solution/implementation/1244/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/f13978d7e3b8455c846f5825c6eca041
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[105];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int n; cin >> n;
15 | for(int i=1;i<=n;i++) cin >> arr[i];
16 | int query; cin >> query;
17 | while(query--){
18 | int type, idx; cin >> type >> idx;
19 | if(type == 1){
20 | for(int i=idx;i<=n;i+=idx) arr[i] ^= 1;
21 | }
22 | else {
23 | int w = 0;
24 | while(1 <= idx - w - 1 && idx + w + 1 <= n && arr[idx - w - 1] == arr[idx + w + 1]) w++;
25 | for(int i=idx-w;i<=idx+w;i++) arr[i] ^= 1;
26 | }
27 | }
28 | for(int i=1;i<=n;i++) {
29 | cout << arr[i];
30 | if(i % 20 == 0) cout << '\n';
31 | else cout << " ";
32 | }
33 |
34 | return 0;
35 | }
36 |
--------------------------------------------------------------------------------
/solution/implementation/14467/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/88f56047b85749c39427398cd19c31e9
4 | #include
5 |
6 | using namespace std;
7 |
8 | int used[105];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | for(int i=0;i<=100;i++) used[i] = -1;
15 |
16 | int n; cin >> n;
17 | int answer = 0;
18 | for(int i=0;i> num >> loc;
20 |
21 | if(used[num] < 0) used[num] = loc;
22 | else if(used[num] != loc){
23 | used[num] = loc;
24 | answer ++;
25 | }
26 | }
27 | cout << answer;
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/solution/implementation/14719/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/5af0f7388d84485db221c368ce306c4b
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | h, w = map(int, input().split())
10 | wall = list(map(int, input().split()))
11 |
12 | # Numpy 없이 argmax
13 | def argmax(a):
14 | return max(range(len(a)), key=lambda x: a[x])
15 | max_idx = argmax(wall)
16 |
17 | sol, tmp_max = 0, 0
18 | # 가장 높은 벽 기준 왼쪽부터 벽+채울 수 있는 물 합한 면적 더하기
19 | for i in range(max_idx+1):
20 | tmp_max = max(tmp_max, wall[i])
21 | sol += tmp_max
22 |
23 | tmp_max = 0
24 | # 가장 높은 벽 기준 오른쪽부터 벽+채울 수 있는 물 합한 면적 더하기
25 | for i in range(w-1, max_idx, -1):
26 | tmp_max = max(tmp_max, wall[i])
27 | sol += tmp_max
28 |
29 | #벽이 차지하는 면적 빼주면 정답
30 | print(sol-sum(wall))
--------------------------------------------------------------------------------
/solution/implementation/15787/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : yj2221
2 | # Co-authored by : -
3 | # Link : http://boj.kr/a636cadadf8d444180ee022a5af9353a
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | n, m = map(int,input().split())
11 | trains= [0] * (n+1)
12 | commands = [list(map(int, input().split())) for _ in range(m)]
13 |
14 | for command in commands:
15 | if command[0] == 1:
16 | _, i, x = command
17 | trains[i] |= (1 << (x-1))
18 | elif command[0] == 2:
19 | _, i, x = command
20 | trains[i] &= ~(1 << (x-1))
21 | elif command[0] == 3:
22 | _, i = command
23 | trains[i] = trains[i] << 1
24 | trains[i] &= ((1 << 20) - 1)
25 | elif command[0] == 4:
26 | _, i = command
27 | trains[i] = trains[i] >> 1
28 |
29 | result = set(trains[1:])
30 | print(len(result))
31 |
--------------------------------------------------------------------------------
/solution/implementation/17413/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/fe8c559fa57643e4a5b072a35a619f89
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | string s;
13 | getline(cin, s);
14 | int n = s.size();
15 |
16 | for(int i=0;i') j++;
24 | i = j + 1;
25 | }
26 | int j=i;
27 | while(j
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int t; cin >> t;
13 | while(t--){
14 | int n; cin >> n;
15 | int max_value = -1000001, min_value = 1000001;
16 | for(int i=0;i> x;
18 | if(max_value < x) max_value = x;
19 | if(min_value > x) min_value = x;
20 | }
21 | cout << min_value << ' ' << max_value << '\n';
22 | }
23 |
24 | return 0;
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/solution/implementation/20207/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/495000f668564c6c9a71fa59b0380aae
4 | #include
5 |
6 | using namespace std;
7 | int prefix[444];
8 |
9 | int main(){
10 | ios::sync_with_stdio(false); cin.tie(0);
11 |
12 | int n; cin >> n;
13 | for(int i=1;i<=n;i++){
14 | int a, b; cin >> a >> b;
15 | prefix[a]++; prefix[b+1]--;
16 | }
17 | int mx = 0, ans = 0, prv = -1;
18 | for(int i=1;i<=366;i++){
19 | prefix[i]+=prefix[i-1];
20 | mx=max(mx,prefix[i]);
21 | if(!prefix[i]){
22 | ans += (i - prv) * mx;
23 | mx = 0; prv = -1;
24 | }
25 | else if(!~prv)prv = i;
26 | }
27 | cout << ans;
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/solution/implementation/20291/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/76369f4f8225439084adb5f553cecae8
4 | #include
5 |
6 | using namespace std;
7 |
8 | vector file;
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int n; cin >> n;
15 | for(int i=0;i> s;
17 | file.emplace_back(s.substr(s.find(".") + 1));
18 | }
19 | sort(file.begin(), file.end());
20 | for(int i=0;i
5 |
6 | using namespace std;
7 |
8 | int arr[105], tmp[105];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N; cin >> N;
15 | for(int i=1;i<=N;i++) arr[i] = i;
16 |
17 | while(N != 1) {
18 | int idx = 1;
19 | for(int i=2;i<=N;i+=2) tmp[idx++] = arr[i];
20 | N = idx - 1;
21 | for(int i=1;i<=N;i++) arr[i] = tmp[i];
22 | }
23 | cout << arr[1];
24 |
25 | return 0;
26 | }
27 |
--------------------------------------------------------------------------------
/solution/implementation/21918/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/04ffe7467c0542c386a2ff3fc95f0b6b
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N, M = map(int, input().split())
10 | arr = list(map(int, input().split()))
11 |
12 | for i in range(M):
13 | a,b,c = map(int, input().split())
14 |
15 | if a == 1:
16 | arr[b-1] = c
17 |
18 | elif a == 2:
19 | for j in range(b-1,c):
20 | if arr[j] == 0:
21 | arr[j] = 1
22 | else:
23 | arr[j] = 0
24 |
25 | elif a == 3:
26 | for j in range(b-1,c):
27 | arr[j] = 0
28 |
29 | else:
30 | for j in range(b-1,c):
31 | arr[j] = 1
32 |
33 | print(*arr)
--------------------------------------------------------------------------------
/solution/implementation/2729/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/fd10df28858644eebc6b4ffd042c2d83
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | T = int(input())
9 | for i in range(T):
10 | a,b = input().split()
11 | total = int(a,2) + int(b,2)
12 | print(bin(total)[2:])
--------------------------------------------------------------------------------
/solution/implementation/2753/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/a5fabcbae0604daabb17802f9323c69f
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int n; cin >> n;
13 | if(n % 4 == 0 && (n % 100 != 0 || n % 400 == 0)){
14 | cout << 1;
15 | }
16 | else {
17 | cout << 0;
18 | }
19 |
20 | return 0;
21 | }
22 |
--------------------------------------------------------------------------------
/solution/implementation/5597/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/2fdc4dd328f9417b8b9f64e2ed0640be
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool chk[33];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | for(int i=0;i<28;i++){
15 | int x;cin >> x;
16 | chk[x] = true;
17 | }
18 | for(int i=1;i<=30;i++){
19 | if(chk[i] == false)cout << i << '\n';
20 | }
21 |
22 | return 0;
23 | }
24 |
25 |
--------------------------------------------------------------------------------
/solution/implementation/5597/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : samuel95
2 | # Co-authored by : -
3 | # Link : http://boj.kr/5229a16593bd45beb68eefd6224ef63b
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | numbers = [False for _ in range(31)]
10 |
11 | for _ in range(28):
12 | n = int(input())
13 | numbers[n] = True
14 |
15 | ans = []
16 |
17 | for i in range(1, 31):
18 | if not numbers[i]:
19 | ans.append(i)
20 |
21 | print(ans[0])
22 | print(ans[1])
23 |
--------------------------------------------------------------------------------
/solution/math/1110/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/29790baf467142729acef15a2815b478
4 | #include
5 |
6 | using namespace std;
7 |
8 | int getNext(int x) {
9 | int tmp = x, ret = 0;
10 | while(x) {
11 | ret += x % 10;
12 | x /= 10;
13 | }
14 | return tmp % 10 * 10 + ret % 10;
15 | }
16 |
17 | int main(){
18 | ios::sync_with_stdio(false);
19 | cin.tie(0);
20 |
21 | int N; cin >> N;
22 | int start = N;
23 | int cur = start;
24 | int ans = 0;
25 |
26 | while(true) {
27 | cur = getNext(cur);
28 | ans ++;
29 | if(start == cur) break;
30 | }
31 | cout << ans;
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/solution/math/11653/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/ee2e1b1327414b35ad963676aae23a8b
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int N; cin >> N;
13 | int tmp = N;
14 | for(int i=2;i*i<=N;i++){
15 | while(tmp % i == 0) {
16 | cout << i << '\n';
17 | tmp /= i;
18 | }
19 | }
20 | if(tmp != 1) cout << tmp;
21 |
22 | return 0;
23 | }
24 |
--------------------------------------------------------------------------------
/solution/math/11653/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/ebf75b738a784d6dad28494e18113b31
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | X = N
11 | for i in range(2, N + 1):
12 | if X == 1:
13 | break
14 | while X % i == 0:
15 | X //= i
16 | print(i)
17 |
--------------------------------------------------------------------------------
/solution/math/1747/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/b94e3339437045b3adbc71d22ec88114
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool isPrime(int x) {
9 | if(x <= 1) return false;
10 | for(int i=2;i*i<=x;i++){
11 | if(x % i == 0) return false;
12 | }
13 | return true;
14 | }
15 |
16 | bool isPalindrome(int x) {
17 | int origin = x;
18 | int rev = 0;
19 | while(x) {
20 | rev = rev * 10 + x % 10;
21 | x /= 10;
22 | }
23 | return origin == rev;
24 | }
25 |
26 | int main(){
27 | ios::sync_with_stdio(false);
28 | cin.tie(0);
29 |
30 | int n; cin >> n;
31 | while(true) {
32 | if(isPalindrome(n) && isPrime(n)) break;
33 | n ++;
34 | }
35 | cout << n;
36 |
37 | return 0;
38 | }
39 |
--------------------------------------------------------------------------------
/solution/math/1934/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/ad01b7a316c54d36b65058fd4909173b
4 | #include
5 |
6 | using namespace std;
7 |
8 | int gcd(int a, int b) {
9 | if(b == 0)return a;
10 | return gcd(b, a % b);
11 | }
12 | int lcm(int a, int b) {
13 | return a / gcd(a, b) * b;
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int t; cin >> t;
21 | while(t--){
22 | int a, b; cin >> a >> b;
23 | cout << lcm(a, b) << '\n';
24 | }
25 |
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/solution/math/1978/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/0b2168e810154d6e916497d012aa7daa
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool isPrime(int x) {
9 | if(x <= 1) return false;
10 | for(int i=2;i*i<=x;i++) {
11 | if(x % i == 0) return false;
12 | }
13 | return true;
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int n; cin >> n;
21 | int answer = 0;
22 | for(int i=0;i> x;
24 | if(isPrime(x)) answer ++;
25 | }
26 | cout << answer;
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/solution/math/1978/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/fba2f023d5c346aa826b3c6922fb0a27
4 | import sys
5 | from math import sqrt
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | prime = [1] * 1005
11 | prime[0] = 0
12 | prime[1] = 0
13 | ct = 0
14 | N = int(input())
15 | arr = list(map(int, input().split()))
16 | for i in range(2, int(sqrt(1005))+1):
17 | for j in range(i+i, 1005, i):
18 | if prime[j] == 1:
19 | prime[j] = 0
20 | for i in arr:
21 | if prime[i]:
22 | ct += 1
23 | print(ct)
24 |
--------------------------------------------------------------------------------
/solution/math/21312/main.py:
--------------------------------------------------------------------------------
1 | # // Authored by : chj3748
2 | # // Co-authored by : -
3 | # // Link : http://boj.kr/21536cc8d28a4570aca6c9b48fc41e26
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | odd_num = -1
11 | even_num = -1
12 | for number in map(int, input().split()):
13 | if number % 2:
14 | if odd_num == -1:
15 | odd_num = number
16 | else:
17 | odd_num *= number
18 | else:
19 | if even_num == -1:
20 | even_num = number
21 | else:
22 | even_num *= number
23 | if odd_num != -1:
24 | print(odd_num)
25 | else:
26 | print(even_num)
27 |
--------------------------------------------------------------------------------
/solution/math/21919/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/e59d5d5d486b44ba93000175e5118406
4 | import sys
5 | from math import sqrt
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | def GCD(x,y):
11 | if y == 0:
12 | return x
13 | else:
14 | return GCD(y, x%y)
15 |
16 | arr = [0] * 1000003
17 | for i in range(2,int(sqrt(1000003))+1):
18 | for j in range(i+i, 1000003, i):
19 | if arr[j] == 0:
20 | arr[j] = 1
21 | arr[0] = 1
22 | arr[1] = 1
23 | LCM = 1
24 | N = int(input())
25 | A = list(map(int, input().split()))
26 | ans = []
27 | for i in A:
28 | if not arr[i]:
29 | ans.append(i)
30 | if not ans:
31 | print(-1)
32 | else:
33 | LCM = ans[0]
34 | for i in range(1,len(ans)):
35 | LCM = ans[i] * LCM // GCD(ans[i], LCM)
36 | print(LCM)
--------------------------------------------------------------------------------
/solution/math/21920/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/9824ccd807974698b94586b14edbb348
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def GCD(x,y):
10 | if y == 0:
11 | return x
12 | else:
13 | return GCD(y, x%y)
14 |
15 | N = int(input())
16 | A = list(map(int, input().split()))
17 | X = int(input())
18 | ans = []
19 | for i in A:
20 | if GCD(X,i) == 1:
21 | ans.append(i)
22 | print(sum(ans)/len(ans))
23 |
--------------------------------------------------------------------------------
/solution/math/2581/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/f3c6407dc6ab4ad38ebb74b1ea3a9c8e
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool isPrime(int x) {
9 | if(x <= 1) return false;
10 | for(int i=2;i*i<=x;i++) {
11 | if(x % i == 0) return false;
12 | }
13 | return true;
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int sum = 0, minimum = 10001;
21 | int l, r; cin >> l >> r;
22 | for(int i=l;i<=r;i++){
23 | if(isPrime(i)) {
24 | sum += i;
25 | minimum = min(minimum, i);
26 | }
27 | }
28 | if(sum) {
29 | cout << sum << '\n';
30 | cout << minimum;
31 | }
32 | else {
33 | cout << -1;
34 | }
35 | return 0;
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/solution/math/2609/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/054bab418a2c441a99982acfa1ba2b27
4 | #include
5 |
6 | using namespace std;
7 |
8 | int gcd(int a, int b) {
9 | if(b == 0)return a;
10 | return gcd(b, a % b);
11 | }
12 | int lcm(int a, int b){
13 | return a / gcd(a, b) * b;
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int a, b; cin >> a >> b;
21 | cout << gcd(a, b) << '\n' << lcm(a, b);
22 |
23 | return 0;
24 | }
25 |
--------------------------------------------------------------------------------
/solution/math/2745/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/6f8324f145664e3890bd12301389737d
4 | #include
5 |
6 | using namespace std;
7 |
8 | int getValue(char x) {
9 | if(x <= '9') return x - '0';
10 | return x - 'A' + 10;
11 | }
12 |
13 | int main(){
14 | ios::sync_with_stdio(false);
15 | cin.tie(0);
16 |
17 | string s; cin >> s;
18 | int base; cin >> base;
19 |
20 | int answer = 0;
21 | for(int i=0;i<(int)s.size();i++) {
22 | answer = answer * base + getValue(s[i]);
23 | }
24 | cout << answer;
25 |
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/solution/math/2753/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/8321cffd959748c981d1cb359cccee9d
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | year = int(input())
10 |
11 | print(1 if not year % 4 and (year % 100 or not year % 400) else 0)
12 |
--------------------------------------------------------------------------------
/solution/math/2960/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/bf53232ebf90497bbcdea9716eade1e8
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool erased[1005];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int K, N; cin >> N >> K;
15 | for(int i=2;K && i<=N;i++){
16 | if(erased[i])continue;
17 | for(int j=i;K && j<=N;j+=i){
18 | if(erased[j])continue;
19 | erased[j] = true;
20 | if(--K == 0) cout << j;
21 | }
22 | }
23 |
24 | return 0;
25 | }
26 |
--------------------------------------------------------------------------------
/solution/math/4134/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/7269680b194f4af3a6a05b0f88161fb2
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | bool isPrime(ll x) {
10 | if(x < 2) return false;
11 | for(ll i=2;i*i<=x;i++){
12 | if(x % i == 0) return false;
13 | }
14 | return true;
15 | }
16 |
17 | int main(){
18 | ios::sync_with_stdio(false);
19 | cin.tie(0);
20 |
21 | int t; cin >> t;
22 | while(t--){
23 | ll x; cin >> x;
24 | while(!isPrime(x))x++;
25 | cout << x << '\n';
26 | }
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/solution/math/5347/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/3e2183112f164d1f98320d51f1ce2642
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | ll gcd(ll a, ll b){
10 | return b ? gcd(b, a % b) : a;
11 | }
12 | ll lcm(ll a, ll b) {
13 | return a / gcd(a, b) * b;
14 | }
15 |
16 | int main(){
17 | ios::sync_with_stdio(false);
18 | cin.tie(0);
19 |
20 | int t; cin >> t;
21 | while(t--){
22 | ll a, b; cin >> a >> b;
23 | cout << lcm(a, b) << '\n';
24 | }
25 |
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/solution/math/5618/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/1df904530afc47edbe9a9983b130ce19
4 | #include
5 |
6 | using namespace std;
7 |
8 | int gcd(int a, int b) {
9 | if(b == 0) return a;
10 | return gcd(b, a%b);
11 | }
12 |
13 | int main(){
14 | ios::sync_with_stdio(false);
15 | cin.tie(0);
16 |
17 | int n; cin >> n;
18 | int a, b; cin >> a >> b;
19 | a = gcd(a, b);
20 | if(n == 3) {
21 | int c; cin >> c;
22 | a = gcd(a, c);
23 | }
24 |
25 | vector ans;
26 | for(int i=1;i*i<=a;i++){
27 | if(a % i != 0) continue;
28 | ans.push_back(i);
29 | if(i * i != a) ans.push_back(a / i);
30 | }
31 | sort(ans.begin(), ans.end());
32 | for(int i=0;i<(int)ans.size();i++){
33 | cout << ans[i] << '\n';
34 | }
35 |
36 | return 0;
37 | }
38 |
--------------------------------------------------------------------------------
/solution/math/5618/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : tony9402
3 | # Link : http://boj.kr/aef57ade1e5c4c6e90f08a159fe96ca2
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def GCD(x,y):
10 | if y == 0:
11 | return x
12 | else:
13 | return GCD(y, x%y)
14 |
15 | n = int(input())
16 | arr = list(map(int, input().split()))
17 | outputs = list()
18 |
19 | gcd = arr[0]
20 | for i in range(1, n):
21 | gcd = GCD(gcd, arr[i])
22 |
23 | x = 1
24 | while x * x <= gcd:
25 | if gcd % x == 0:
26 | outputs.append(x)
27 | if x * x != gcd:
28 | outputs.append(gcd // x)
29 | x += 1
30 |
31 | outputs.sort()
32 | print(*outputs)
33 |
--------------------------------------------------------------------------------
/solution/math/9613/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/81fae9eae6b04d77bb40a1d3fb340b3c
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | int gcd(int a, int b){
10 | if(b == 0)return a;
11 | return gcd(b, a % b);
12 | }
13 |
14 | int main(){
15 | ios::sync_with_stdio(false);
16 | cin.tie(0);
17 |
18 | int t; cin >> t;
19 | while(t--){
20 | int n;cin >> n;
21 | vector v(n);
22 | for(int i=0;i> v[i];
23 | ll ans = 0;
24 | for(int i=0;i
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | ll mod[1000]; // 0 ~ 999
10 |
11 | int main(){
12 | ios::sync_with_stdio(false);
13 | cin.tie(0);
14 |
15 | int N, M; cin >> N >> M;
16 | ll sum =0;
17 | mod[0] = 1;
18 | for(int i=1;i<=N;i++) {
19 | int x; cin >> x;
20 | x %= M;
21 | sum = (sum + x) % M;
22 | mod[sum] ++;
23 | }
24 | ll answer = 0;
25 | for(int i=0;i
6 |
7 | using namespace std;
8 |
9 | int num[100010], sum[100010];
10 |
11 | int main() {
12 | ios::sync_with_stdio(false);
13 | cin.tie(nullptr);
14 |
15 | int N, M; cin >> N >> M;
16 |
17 | // 입력과 동시에 누적합을 구해놓습니다.
18 | for(int i = 1;i <= N;i++) {
19 | cin >> num[i];
20 | sum[i] = sum[i - 1] + num[i];
21 | }
22 |
23 | // end까지의 합 - begin전 까지의 합 = begin ~ end사이 합
24 | for(int i = 0;i < M;i++) {
25 | int begin, end;
26 | cin >> begin >> end;
27 | cout << sum[end] - sum[begin - 1] << '\n';
28 | }
29 |
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/solution/prefix_sum/11659/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/6ada03e306dd451fbea38bfb38db2a74
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n, m = map(int, input().split())
10 | lst = list(map(int, input().split()))
11 | prefix_sum = [lst[0]]
12 | for i in range(1, n): # 누적 합 구해두기
13 | prefix_sum.append(prefix_sum[i-1] + lst[i])
14 |
15 | for _ in range(m):
16 | x, y = map(int, input().split())
17 | x -= 1
18 | if x: print(prefix_sum[y-1] - prefix_sum[x-1])
19 | else: print(prefix_sum[y-1])
20 |
--------------------------------------------------------------------------------
/solution/prefix_sum/11660/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/c28a9148cd334f45b6edcbc40ae7ec15
4 | #include
5 |
6 | using namespace std;
7 |
8 | int prefix[1030][1030];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N, Q; cin >> N >> Q;
15 | for(int i=1;i<=N;i++) {
16 | for(int j=1;j<=N;j++) {
17 | cin >> prefix[i][j];
18 | prefix[i][j] = prefix[i][j] + prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1];
19 | }
20 | }
21 |
22 | while(Q--){
23 | int a, b, c, d; cin >> a >> b>> c>>d;
24 | a--; b--;
25 | cout << prefix[c][d] - prefix[a][d] - prefix[c][b] + prefix[a][b] << '\n';
26 | }
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/solution/prefix_sum/14929/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/0351fb8839dd449cbdc10b9e78c47410
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | int main(){
10 | ios::sync_with_stdio(false);
11 | cin.tie(0);
12 |
13 | int N; cin >> N;
14 | ll sum = 0, square_sum = 0;
15 | for(int i=0;i> x;
17 | sum += x;
18 | square_sum += x * x;
19 | }
20 | cout << (sum * sum - square_sum) / 2;
21 |
22 | return 0;
23 | }
24 |
--------------------------------------------------------------------------------
/solution/prefix_sum/1749/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/67eb3ee9ba7649f29c3542bb2b034e08
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[222][222];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N, M; cin >> N >> M;
15 | for(int i=1;i<=N;i++) {
16 | for(int j=1;j<=M;j++) {
17 | cin >> arr[i][j];
18 | arr[i][j] += arr[i-1][j];
19 | }
20 | }
21 |
22 | int answer = INT_MIN;
23 | for(int i=1;i<=N;i++){
24 | for(int j=i;j<=N;j++) {
25 | int sum = 0;
26 | for(int k=1;k<=M;k++){
27 | int cur = arr[j][k] - arr[i - 1][k];
28 | sum += cur;
29 | if(sum < cur) sum = cur;
30 | answer = max(answer, sum);
31 | }
32 | }
33 | }
34 | cout << answer;
35 |
36 | return 0;
37 | }
38 |
--------------------------------------------------------------------------------
/solution/prefix_sum/2015/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/d46daeae8c194cb080f41f07b383b113
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | unordered_map mp;
10 | ll prefix[200002];
11 |
12 | int main(){
13 | ios::sync_with_stdio(false);
14 | cin.tie(0);
15 |
16 | ll N, K; cin >> N >> K;
17 | ll answer = 0;
18 | for(int i=1;i<=N;i++) {
19 | cin >> prefix[i];
20 | prefix[i] += prefix[i - 1];
21 |
22 | if(prefix[i] == K) answer ++;
23 |
24 | if(mp.count(prefix[i] - K)) answer += mp[prefix[i] - K];
25 | mp[prefix[i]] ++;
26 | }
27 | cout << answer;
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/solution/prefix_sum/2015/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/c3cc5338e8fa49ae8136e4af174221df
4 | from collections import defaultdict
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n, k = map(int, input().split())
10 | lst = list(map(int, input().split()))
11 | dic = defaultdict(int) # 어떤 값이 기존에 몇 번 나왔는지 저장
12 |
13 | for i in range(1, n): lst[i] += lst[i-1] # 누적합 inplace로 만들기
14 |
15 | cnt = 0
16 | for num in lst:
17 | if num == k: cnt += 1 # 1부터 더한 값 중 k 있으면 cnt += 1
18 | # O(n) 연산 대신 dictionary에서 찾는 것으로 시간 단축
19 | cnt += dic[num-k] # 이미 본 누적합 값과 num으로 k 만들 수 있으면 추가
20 | dic[num] += 1 # num 만들 수 있는 개수 +1
21 |
22 | print(cnt)
--------------------------------------------------------------------------------
/solution/prefix_sum/20438/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/2d250c49804d4fb6a38cbe6e752cd87d
4 | #include
5 |
6 | using namespace std;
7 |
8 | int prefix[5050], sleep[5050], used[5050];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N, K, Q, M; cin >> N >> K >> Q >> M;
15 | for(int i=1;i<=K;i++) {
16 | int x;cin >> x;
17 | sleep[x] = 1;
18 | }
19 | for(int i=1;i<=Q;i++) {
20 | int x;cin >> x;
21 | if(sleep[x]) continue;
22 | for(int j=x;j<=N+2;j+=x) {
23 | if(sleep[j]) continue;
24 | used[j] = 1;
25 | }
26 | }
27 |
28 | for(int i=3;i<=N+2;i++) {
29 | prefix[i] = prefix[i-1];
30 | if(used[i] == 0) prefix[i] ++;
31 | }
32 |
33 | for(int i=1;i<=M;i++){
34 | int L, R; cin >> L >> R;
35 | cout << prefix[R] - prefix[L - 1] << '\n';
36 | }
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/solution/prefix_sum/21318/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/3aaead48f31d48d19a9884356908d293
4 | #include
5 |
6 | using namespace std;
7 |
8 | int arr[100005], prefix[100005];
9 |
10 | int main(){
11 | ios::sync_with_stdio(false);
12 | cin.tie(0);
13 |
14 | int N; cin >> N;
15 | for(int i=1;i<=N;i++) cin >> arr[i];
16 | for(int i=1;i<=N;i++) {
17 | if(arr[i] > arr[i + 1]) prefix[i] = 1;
18 | prefix[i] += prefix[i - 1];
19 | }
20 |
21 | int Q; cin >> Q;
22 | while(Q--){
23 | int x, y; cin >> x >> y;
24 | --x; --y;
25 | cout << prefix[y] - prefix[x] << '\n';
26 | }
27 |
28 | return 0;
29 | }
30 |
--------------------------------------------------------------------------------
/solution/prefix_sum/2167/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/5182387e6c9745238986dcbe9231c45d
4 | #include
5 |
6 | using namespace std;
7 | int prefix[303][303];
8 |
9 | int main(){
10 | ios::sync_with_stdio(false);
11 | cin.tie(0);
12 |
13 | int N, M; cin >> N >> M;
14 | for(int i=1;i<=N;i++) {
15 | for(int j=1;j<=M;j++) {
16 | cin >> prefix[i][j];
17 | prefix[i][j] = prefix[i][j] + prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1];
18 | }
19 | }
20 | int Q; cin >> Q;
21 | while(Q--) {
22 | int a, b, c, d; cin >> a >> b >> c >> d;
23 | --a; -- b;
24 | cout << prefix[c][d] - prefix[a][d] - prefix[c][b] + prefix[a][b] << '\n';
25 | }
26 |
27 | return 0;
28 | }
29 |
--------------------------------------------------------------------------------
/solution/prefix_sum/21757/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/421ce6bb00ca40709c15dc5df958d5f1
4 | #include
5 |
6 | using namespace std;
7 | typedef long long ll;
8 |
9 | const int MAXN = 100000;
10 | ll prefix[MAXN + 5];
11 | ll L[MAXN + 5], R[MAXN + 5];
12 |
13 | int main(){
14 | ios::sync_with_stdio(false);
15 | cin.tie(0);
16 |
17 | int N; cin >> N;
18 | for(int i=1;i<=N;i++) {
19 | cin >> prefix[i];
20 | prefix[i] += prefix[i - 1];
21 | }
22 |
23 | for(int i = 1; i <= N; i++) {
24 | L[i] = L[i - 1];
25 | if(prefix[N] == 4 * prefix[i]) L[i] ++;
26 | }
27 |
28 | for(int i = N - 1; i >= 1; i--) {
29 | R[i] = R[i + 1];
30 | if(prefix[N] * 3 == 4 * prefix[i]) R[i] ++;
31 | }
32 |
33 | ll answer = 0;
34 | for(int i = 2; i < N - 1; i++) {
35 | if(prefix[N] == prefix[i] * 2) answer += L[i - 1] * R[i + 1];
36 | }
37 | cout << answer;
38 |
39 | return 0;
40 | }
41 |
--------------------------------------------------------------------------------
/solution/string/1032/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/c400c0aa10ea40bc809ad78029ac721d
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | N = int(input())
9 | arr = []
10 | ans = ''
11 | for i in range(N):
12 | arr.append(input())
13 | for i in range(len(arr[0])):
14 | ct = 0
15 | word = arr[0][i]
16 | for j in range(N):
17 | if word == arr[j][i]:
18 | ct += 1
19 | if ct == N:
20 | ans += word
21 | else:
22 | ans += '?'
23 | print(ans)
--------------------------------------------------------------------------------
/solution/string/10798/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/a1c26275e6af44f5a080d54a61b4c677
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | arr = []
10 | ans = ''
11 | max_len = 0
12 | for i in range(5):
13 | a = input()
14 | max_len = max(max_len, len(a))
15 | arr.append(a)
16 |
17 | for i in range(max_len):
18 | for j in range(5):
19 | if len(arr[j]) <= i:
20 | continue
21 | ans += arr[j][i]
22 |
23 | print(ans)
24 |
--------------------------------------------------------------------------------
/solution/string/11365/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/d59f549a75a84dd28c01a0f9c08eb169
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | while True:
11 |
12 | password = input()
13 |
14 | if password == 'END':
15 | break
16 | else:
17 | print(password[::-1])
--------------------------------------------------------------------------------
/solution/string/1152/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/f95f97c2cdd74f5f9b832c2e394d4123
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | word = input().split()
9 | print(len(word))
--------------------------------------------------------------------------------
/solution/string/11720/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/85503261a5b5492ca153ccbe74adf43d
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | num = input()
11 | total = 0
12 | for i in num:
13 | total += int(i)
14 |
15 | print(total)
16 |
--------------------------------------------------------------------------------
/solution/string/1181/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/c540e6d57a39460eb360903623b90eec
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | arr = []
12 |
13 | for i in range(N):
14 | arr.append(input())
15 |
16 | arr = list(set(arr))
17 | arr.sort(key = lambda x : (len(x), x))
18 |
19 | for i in arr:
20 | print(i)
--------------------------------------------------------------------------------
/solution/string/12871/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/7d4da3364b4d4735b5f99d946b785fff
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | def GCD(x,y):
9 | if y == 0:
10 | return x
11 | else:
12 | return GCD(y, x%y)
13 |
14 | s = input()
15 | t = input()
16 | gcd = GCD(len(s),len(t))
17 | lcm = len(s) * len(t) // gcd
18 | s = s * (lcm // len(s))
19 | t = t * (lcm // len(t))
20 | if s == t:
21 | print(1)
22 | else:
23 | print(0)
24 |
--------------------------------------------------------------------------------
/solution/string/1316/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/1649bc736d5e4692b067d78aa43b2edc
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | N = int(input())
10 | ans = N
11 | for i in range(N):
12 | a = input()
13 | dic = {}
14 | while True:
15 | flag = 0
16 | for j in range(len(a)-1):
17 | if a[j] == a[j+1]:
18 | a = a[:j] + a[j+1:]
19 | flag = 1
20 | break
21 | if flag == 0:
22 | break
23 | for j in a:
24 | if j not in dic:
25 | dic[j] = 1
26 | else:
27 | ans -=1
28 | break
29 | print(ans)
--------------------------------------------------------------------------------
/solution/string/16171/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : rlawngus0910
2 | // Co-authored by : tony9402
3 | // Link : http://boj.kr/cddf85db4e694c0d8e9a20dddd32e653
4 | #include
5 | using namespace std;
6 | string input, s, k;
7 | int main() {
8 | cin.sync_with_stdio(0);
9 | cin.tie(0);
10 | cin >> input >> k;
11 |
12 | for (int i = 0; i < (int)input.length(); i++) {
13 | if('0' <= input[i] && input[i] <= '9') continue;
14 | s += input[i];
15 | }
16 |
17 | if (s.find(k) != string::npos) cout << "1";
18 | else cout << "0";
19 | }
20 |
--------------------------------------------------------------------------------
/solution/string/17609/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : chj3748
2 | # Co-authored by : -
3 | # Link : http://boj.kr/35708a61bb1d4149952929e8154ba152
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | def palin(start, end, del_cnt):
10 | if del_cnt == 2:
11 | return del_cnt
12 | while start <= end:
13 | if string[start] != string[end]:
14 | a = palin(start + 1, end, del_cnt + 1)
15 | b = palin(start, end - 1, del_cnt + 1)
16 | return a if a <= b else b
17 | start += 1
18 | end -= 1
19 | else:
20 | return del_cnt
21 |
22 | for T in range(int(input())):
23 | string = input()
24 | print(palin(0, len(string) - 1, 0))
25 |
--------------------------------------------------------------------------------
/solution/string/20154/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/ff79a90b0e024c4eb328a8595918b096
4 | import sys
5 |
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | DB = [3, 2, 1, 2, 3, 3, 3, 3, 1, 1, 3, 1, 3, 3, 1, 2, 2, 2, 1, 2, 1, 1, 2, 2, 2, 1]
10 | dic = {}
11 | for idx, data in enumerate(DB):
12 | dic[chr(idx+65)] = DB[idx] # 'A' : 65
13 |
14 | total = 0
15 | a = input()
16 | for i in a:
17 | total += dic[i]
18 | total = total % 10
19 | if total % 2 == 1:
20 | print("I'm a winner!")
21 | else:
22 | print("You're the winner?")
23 |
--------------------------------------------------------------------------------
/solution/string/2204/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/685c4c3dc1c24c118d3c89ffdefa6679
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | while True:
9 | n = int(input())
10 | if n == 0:
11 | break
12 | arr = []
13 | for i in range(n):
14 | a = input()
15 | a_lower = a.lower()
16 | arr.append((a,a_lower))
17 | arr.sort(key = lambda x : x[1])
18 | print(arr[0][0])
--------------------------------------------------------------------------------
/solution/string/2744/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/39641966833845ec931ab403fc15026d
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | word = input()
9 | ans = ''
10 | for i in word:
11 | if i.islower():
12 | ans += i.upper()
13 | else:
14 | ans += i.lower()
15 | print(ans)
--------------------------------------------------------------------------------
/solution/string/9046/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/ecd357e546d24b059db0160435e99364
4 | import sys
5 | def input():
6 | return sys.stdin.readline().rstrip()
7 |
8 | N = int(input())
9 | for i in range(N):
10 | a = input()
11 | total = {}
12 | for j in a:
13 | if j == ' ':
14 | continue
15 | if j not in total:
16 | total[j] = 1
17 | else:
18 | total[j] += 1
19 | new_total = list(total.items())
20 | if len(new_total) == 1:
21 | print(new_total[0][0])
22 | else:
23 | new_total.sort(key = lambda x : -x[1])
24 | if new_total[0][1] == new_total[1][1]:
25 | print('?')
26 | else:
27 | print(new_total[0][0])
--------------------------------------------------------------------------------
/solution/string/9342/main.cpp:
--------------------------------------------------------------------------------
1 | // Authored by : tony9402
2 | // Co-authored by : -
3 | // Link : http://boj.kr/1df192eb8ca749ec8a568e9189e4ef5d
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main(){
9 | ios::sync_with_stdio(false);
10 | cin.tie(0);
11 |
12 | int T; cin >> T;
13 | while(T--) {
14 | string s; cin >> s;
15 | bool flag = true;
16 | int idx = 0, N = (int)s.size();
17 | if(idx < N && 'B' <= s[idx] && s[idx] <= 'F') idx++;
18 | for(char cur : {'A', 'F', 'C'}) {
19 | int cnt = 0;
20 | while(idx < N && s[idx] == cur) idx++, cnt++;
21 | if(cnt == 0) flag = false;
22 | }
23 | if(idx < N && 'A' <= s[idx] && s[idx] <= 'F') idx++;
24 | if(idx < N) flag = false;
25 | if(flag) cout << "Infected!\n";
26 | else cout << "Good\n";
27 | }
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/solution/string/9342/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : tony9402
2 | # Co-authored by : -
3 | # Link : http://boj.kr/4fcec3ea35e4472bbca8beeebd2fe065
4 | import sys
5 | import re
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | regex = re.compile('^[A-F]?A+F+C+[A-F]?$')
11 | N = int(input())
12 | for testcase in range(N):
13 | line = input()
14 | print("Infected!" if regex.match(line) else "Good")
15 |
16 |
--------------------------------------------------------------------------------
/solution/two_pointer/1806/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : cieske
2 | # Co-authored by : -
3 | # Link : http://boj.kr/a020564994b8493a92b189861e35636c
4 |
5 | import sys
6 | def input():
7 | return sys.stdin.readline().rstrip()
8 |
9 | n, s = map(int, input().split())
10 | lst = list(map(int, input().split()))
11 |
12 | l_p, r_p, cur_sum, min_len = 0, 0, 0, 100002
13 | # cur_sum에 저장되는 구간합: sum(lst[l_p:r_p])
14 |
15 | while l_p < n:
16 | if cur_sum >= s: # 현재 구간이 주어진 s보다 크다면
17 | min_len = min(min_len, r_p-l_p) #현재 구간 길이(r_p-l_p)가 최소라면 업데이트
18 | cur_sum -= lst[l_p] #왼쪽 포인터를 한 칸 오른쪽으로 옮기고 구간 합 업데이트
19 | l_p += 1
20 |
21 | elif r_p == n: #오른쪽 끝에 도달했다면?
22 | break
23 |
24 | else:
25 | cur_sum += lst[r_p] #오른쪽 구간을 움직여서 구간 합 업데이트
26 | r_p += 1
27 |
28 | print(min_len if min_len < 100001 else 0)
29 |
--------------------------------------------------------------------------------
/solution/two_pointer/1940/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/901436e12d914d5e9effcd67494bbb44
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | M = int(input())
12 | arr = list(map(int, input().split()))
13 | arr.sort()
14 | ans = 0
15 | s, e = 0, len(arr) - 1
16 | while s < e:
17 | if arr[s] + arr[e] > M:
18 | e -= 1
19 | elif arr[s] + arr[e] < M:
20 | s += 1
21 | else:
22 | s += 1
23 | e -= 1
24 | ans += 1
25 | print(ans)
--------------------------------------------------------------------------------
/solution/two_pointer/2018/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/2b640a2f6eb74e08aae6e351cfcbf537
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N = int(input())
11 | arr = [i for i in range(1, N+1)]
12 | s, e = 0, 0
13 | ans = 0
14 | total = 0
15 | while True:
16 | if total < N:
17 | total += arr[e]
18 | e += 1
19 | elif e == len(arr):
20 | break
21 | elif total >= N:
22 | if total == N:
23 | ans += 1
24 | total -= arr[s]
25 | s += 1
26 | print(ans+1)
--------------------------------------------------------------------------------
/solution/two_pointer/20922/main.cpp:
--------------------------------------------------------------------------------
1 | //Authored by : suin8
2 | //Co-authored by : tony9402
3 | //Link : http://boj.kr/bf868d6487744424a792c188673bf339
4 |
5 | #include
6 |
7 | using namespace std;
8 |
9 | int num[200010], cnt[100010];
10 |
11 | int main() {
12 | ios::sync_with_stdio(false);
13 | cin.tie(nullptr);
14 |
15 | int N, K; cin >> N >> K;
16 |
17 | for(int i = 1;i <= N;i++)
18 | cin >> num[i];
19 |
20 | // start와 end사이에 겹치는 수가 K개 이하일 때는 end를 늘리며
21 | // 최대 길이를 늘리고
22 | // K개 초과일 때는 begin를 늘리며 겹치는 수가 빠질때까지 begin을 증가
23 | int ans = 0;
24 | for(int begin = 1, end = 1; begin <= N; begin ++) {
25 | while(end <= N && cnt[num[end]] < K) ++cnt[num[end++]];
26 | ans = max(ans, end - begin);
27 | cnt[num[begin]]--;
28 | }
29 |
30 | cout << ans;
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/solution/two_pointer/2559/main.py:
--------------------------------------------------------------------------------
1 | # Authored by : gusdn3477
2 | # Co-authored by : -
3 | # Link : http://boj.kr/1d6b566a715d4126b3cd42cc39521ac0
4 |
5 | import sys
6 |
7 | def input():
8 | return sys.stdin.readline().rstrip()
9 |
10 | N, K = map(int, input().split())
11 | arr = list(map(int, input().split()))
12 | total = 0
13 | MAX = -1e9
14 | s, e = 0, K-1
15 | if K == 1:
16 | print(max(arr))
17 | else:
18 | total = sum(arr[:K])
19 | while True:
20 | MAX = max(MAX, total)
21 | total -= arr[s]
22 | s += 1
23 | e += 1
24 | if e == N:
25 | break
26 | total += arr[e]
27 | print(MAX)
--------------------------------------------------------------------------------
/update_log.md:
--------------------------------------------------------------------------------
1 | 01:54:17 06/03/25 KST
--------------------------------------------------------------------------------