├── .idea
├── .gitignore
├── misc.xml
├── modules.xml
└── vcs.xml
├── AdvancedAlgorithm.iml
├── README.md
├── TestCase
└── FirstExercise
│ ├── 1-10
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-13
│ ├── 1.in
│ └── 1.out
│ ├── 1-14
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-15
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-16
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-17
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-21
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ └── 3.out
│ ├── 1-22
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-23
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ └── 3.out
│ ├── 1-25
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-5
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-6
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ └── 3.out
│ ├── 1-7
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ ├── 1-8
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ ├── 3.out
│ ├── 4.in
│ ├── 4.out
│ ├── 5.in
│ └── 5.out
│ └── 1-9
│ ├── 1.in
│ ├── 1.out
│ ├── 2.in
│ ├── 2.out
│ ├── 3.in
│ └── 3.out
├── out
└── production
│ └── AdvancedAlgorithm
│ ├── FirstExercise
│ ├── Point.class
│ ├── Problem0105.class
│ ├── Problem0106.class
│ ├── Problem0107.class
│ ├── Problem0108.class
│ ├── Problem0109.class
│ ├── Problem0110.class
│ ├── Problem0112.class
│ ├── Problem0113.class
│ ├── Problem0114.class
│ ├── Problem0115.class
│ ├── Problem0116.class
│ ├── Problem0117.class
│ ├── Problem0118.class
│ ├── Problem0119.class
│ ├── Problem0120.class
│ ├── Problem0121.class
│ ├── Problem0122.class
│ ├── Problem0123.class
│ ├── Problem0124$1.class
│ ├── Problem0124.class
│ ├── Problem0125$1.class
│ └── Problem0125.class
│ └── Test
│ ├── Problem0101.class
│ ├── Problem0102.class
│ ├── Problem0103.class
│ ├── Problem0104.class
│ ├── Problem0105.class
│ ├── Problem0106.class
│ ├── Problem0107.class
│ └── Problem0108.class
└── src
├── FifthExercise
├── Problem0501.java
├── Problem0502.java
├── Problem0503.java
├── Problem0504.java
├── Problem0505.java
├── Problem0506.java
├── Problem0507.java
├── Problem0508.java
├── Problem0509.java
├── Problem0510.java
└── Problem0511.java
├── FirstExercise
├── Problem0105.java
├── Problem0106.java
├── Problem0107.java
├── Problem0108.java
├── Problem0109.java
├── Problem0110.java
├── Problem0112.java
├── Problem0113.java
├── Problem0114.java
├── Problem0115.java
├── Problem0116.java
├── Problem0117.java
├── Problem0118.java
├── Problem0119.java
├── Problem0120.java
├── Problem0121.java
├── Problem0122.java
├── Problem0123.java
├── Problem0124.java
└── Problem0125.java
├── FourthExercise
├── Problem0401.java
├── Problem0402.java
├── Problem0403.java
├── Problem0404.java
├── Problem0405.java
├── Problem0406.java
├── Problem0407.java
└── Problem0408.java
├── SecondExercise
├── Problem0201.java
├── Problem0202.java
├── Problem0203.java
├── Problem0204.java
├── Problem0205.java
├── Problem0206.java
├── Problem0207.java
├── Problem0208.java
└── Problem0209.java
├── Test
├── Problem0101.java
├── Problem0102.java
├── Problem0103.java
├── Problem0104.java
├── Problem0105.java
├── Problem0106.java
├── Problem0107.java
└── Problem0108.java
└── ThirdExercise
├── Problem0301.java
├── Problem0302.java
├── Problem0303.java
├── Problem0304.java
├── Problem0305.java
├── Problem0306.java
├── Problem0307.java
├── Problem0308.java
├── Problem0309.java
├── Problem0310.java
└── Problem0311.java
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Datasource local storage ignored files
5 | /../../../:\AdvancedAlgorithm\.idea/dataSources/
6 | /dataSources.local.xml
7 | # Editor-based HTTP Client requests
8 | /httpRequests/
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/AdvancedAlgorithm.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AdvancedAlgorithm
2 | NJU SE 高级算法课oj题目记录
3 |
4 | ## Test
5 | 测试用,熟悉oj使用,不重要
6 |
7 | ## FirstExercise
8 | 第一次课堂练习(排序和分治),题目列表如下
9 |
10 | - 1-05 宠物屋涂色
11 | - 1-06 序号乘方
12 | - 1-07 二叉树
13 | - 1-08 数学公式
14 | - 1-09 点的凸包
15 | - 1-10 书本分发
16 | - 1-12 客流量
17 | - 1-13 和为定值的子数组
18 | - 1-14 合并数组
19 | - 1-15 最小交换次数
20 | - 1-16 插入排序
21 | - 1-17 冒泡排序
22 | - 1-18 计数排序
23 | - 1-19 非递归快排
24 | - 1-20 非递归合并排序
25 | - 1-21 倒置个数
26 | - 1-22 实现Shell排序
27 | - 1-23 棋盘覆盖问题
28 | - 1-24 分治法解最近对问题
29 | - 1-25 按照数值个数排序
30 |
31 | ## SecondExercise
32 | 第二次课堂练习(查找和图),题目列表如下
33 |
34 | - 2-01 拓扑排序解的个数
35 | - 2-02 广度优先遍历图
36 | - 2-03 深度优先遍历
37 | - 2-04 分配问题
38 | - 2-05 Searching_4
39 | - 2-06 按照另一个数组排序
40 | - 2-07 Searching_3
41 | - 2-08 有9个因数的数
42 | - 2-09 漆狗屋
43 |
44 | ## ThirdExercise
45 | 第三次课堂练习(字符串和数学),题目列表如下
46 |
47 | - 3-01 最长公共子序列
48 | - 3-02 区间第k最小
49 | - 3-03 子矩阵问题
50 | - 3-04 能否成环
51 | - 3-05 数字重组整除问题
52 | - 3-06 牛的繁殖问题
53 | - 3-07 和最大的连续降序字符
54 | - 3-08 距离问题
55 | - 3-09 字符串匹配问题
56 | - 3-10 整除查询
57 | - 3-11 对称子字符串
58 |
59 | ## FourthExercise
60 | 第四次课堂练习(动态规划),题目列表如下
61 | - 4-01 最小化初始点
62 | - 4-02 矩阵计算
63 | - 4-03 无重复字符子集问题
64 | - 4-04 是否能通过考试
65 | - 4-05 如何花最少的钱购买蔬菜
66 | - 4-06 按照要求保留数组元素使得和最大
67 | - 4-07 数组查询
68 | - 4-08 订单问题
69 |
70 | ## FifthExercise
71 | 第五次课堂练习(贪婪和数组),题目列表如下
72 | - 5-01 格子里的整数
73 | - 5-02 路上的球
74 | - 5-03 硬币最小数量
75 | - 5-04 时间与收益
76 | - 5-05 时间分隔
77 | - 5-06 管道网络
78 | - 5-07 调整数组使差最小
79 | - 5-08 先升后降
80 | - 5-09 固定和的元素对
81 | - 5-10 数组和窗口
82 | - 5-11 子数组的取值范围
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/1.in:
--------------------------------------------------------------------------------
1 | 1
2 | 4
3 | 12 34 67 90
4 | 2
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/1.out:
--------------------------------------------------------------------------------
1 | 113
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/2.in:
--------------------------------------------------------------------------------
1 | 1
2 | 3
3 | 10 10 10
4 | 4
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/2.out:
--------------------------------------------------------------------------------
1 | -1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/3.in:
--------------------------------------------------------------------------------
1 | 5
2 | 37
3 | 194 113 23 176 226 190 91 14 204 120 155 215 241 147 93 132 48 30 190 192 26 192 238 95 106 179 183 208 240 137 24 30 154 11 9 16 18
4 | 48
5 | 7
6 | 44 55 107 4 128 185 217
7 | 3
8 | 5
9 | 147 1 175 235 18
10 | 3
11 | 10
12 | 41 44 83 218 11 232 238 99 49 245
13 | 5
14 | 39
15 | 160 219 234 104 69 45 103 227 107 203 203 62 212 43 98 33 24 34 56 166 226 99 98 43 188 153 160 246 17 43 131 43 236 230 12 83 23 164 68
16 | 20
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/3.out:
--------------------------------------------------------------------------------
1 | -1
2 | 313
3 | 253
4 | 337
5 | 321
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/4.in:
--------------------------------------------------------------------------------
1 | 1
2 | 10
3 | 10 9 8 7 6 5 4 3 2 1
4 | 10
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/4.out:
--------------------------------------------------------------------------------
1 | 10
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/5.in:
--------------------------------------------------------------------------------
1 | 3
2 | 29
3 | 149 225 174 36 64 145 97 15 145 10 87 96 67 213 59 244 37 161 68 9 149 129 26 218 118 35 155 48 217
4 | 4
5 | 7
6 | 39 84 115 11 186 67 93
7 | 2
8 | 12
9 | 117 81 25 183 0 101 102 25 238 86 240 242
10 | 13
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-10/5.out:
--------------------------------------------------------------------------------
1 | 817
2 | 346
3 | -1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-13/1.in:
--------------------------------------------------------------------------------
1 | 2
2 | 5 3
3 | 0 0 2 1 1
4 | 7 23
5 | 10 2 3 4 5 7 8
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-13/1.out:
--------------------------------------------------------------------------------
1 | 0 0 1 2 $
2 | 2 3 8 10 $2 4 7 10 $3 5 7 8 $
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/1.in:
--------------------------------------------------------------------------------
1 | 1
2 | 3
3 | 1 2 3 4 5 6 7 8 9
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/1.out:
--------------------------------------------------------------------------------
1 | 1 2 3 4 5 6 7 8 9
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/2.in:
--------------------------------------------------------------------------------
1 | 1
2 | 3
3 | 2147483647 2147483647 2147483647 -2147483648 0 2147483647 -10000 1000000 100000000
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/2.out:
--------------------------------------------------------------------------------
1 | -2147483648 -10000 0 1000000 100000000 2147483647 2147483647 2147483647 2147483647
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/3.in:
--------------------------------------------------------------------------------
1 | 1
2 | 5
3 | -100000 -9999 -999 -99 0 -10000 -8000 -6000 -4000 -2000 -8500 -7500 -6500 -5500 -4500 -50000 -5000 -3000 -2000 -1000 -2147483648 -2147 -147 -47 -7
4 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/3.out:
--------------------------------------------------------------------------------
1 | -2147483648 -100000 -50000 -10000 -9999 -8500 -8000 -7500 -6500 -6000 -5500 -5000 -4500 -4000 -3000 -2147 -2000 -2000 -1000 -999 -147 -99 -47 -7 0
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/4.in:
--------------------------------------------------------------------------------
1 | 4
2 | 5
3 | 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
4 | 3
5 | 1 10 20 5 6 7 4 6 8
6 | 4
7 | 10 20 30 40 50 60 70 80 5 15 25 35 4 14 24 34
8 | 1
9 | 10
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/4.out:
--------------------------------------------------------------------------------
1 | 1 2 2 3 3 4 4 4 5 5 6 6 8 8 9 10 10 12 12 15 15 16 20 20 25
2 | 1 4 5 6 6 7 8 10 20
3 | 4 5 10 14 15 20 24 25 30 34 35 40 50 60 70 80
4 | 10
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/5.in:
--------------------------------------------------------------------------------
1 | 1
2 | 10
3 | -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 0 0 0 0 0 0 0 0 0 -200 -199 -198 -197 -196 -195 -194 -193 -192 -191 -200 -120 -80 -40 0 40 80 120 160 200 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 0 0 0 0 0 0 0 0 0 -200 -199 -198 -197 -196 -195 -194 -193 -192 -191 -200 -120 -80 -40 0 40 80 120 160 200
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-14/5.out:
--------------------------------------------------------------------------------
1 | -200 -200 -200 -200 -199 -199 -198 -198 -197 -197 -196 -196 -195 -195 -194 -194 -193 -193 -192 -192 -191 -191 -120 -120 -80 -80 -40 -40 -20 -20 -18 -18 -16 -16 -14 -14 -12 -12 -10 -10 -10 -10 -9 -9 -8 -8 -8 -8 -7 -7 -6 -6 -6 -6 -5 -5 -4 -4 -4 -4 -3 -3 -2 -2 -2 -2 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 40 80 80 120 120 160 160 200 200
2 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/1.in:
--------------------------------------------------------------------------------
1 | 2
2 | 4
3 | 4 3 2 1
4 | 5
5 | 1 5 4 3 2
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/1.out:
--------------------------------------------------------------------------------
1 | 2
2 | 2
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/2.in:
--------------------------------------------------------------------------------
1 | 3
2 | 3
3 | 599 574 367
4 | 5
5 | 12 718 296 96 532
6 | 5
7 | 225 714 577 41 845
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/2.out:
--------------------------------------------------------------------------------
1 | 1
2 | 2
3 | 2
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/3.in:
--------------------------------------------------------------------------------
1 | 1
2 | 10
3 | 10 9 8 7 6 5 4 3 2 1
4 | 5
5 | 1 2 3 4 5
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/3.out:
--------------------------------------------------------------------------------
1 | 5
2 | 0
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/4.in:
--------------------------------------------------------------------------------
1 | 5
2 | 37
3 | 34 574 595 912 727 459 181 160 420 883 779 89 284 339 322 129 222 135 112 397 592 456 12 870 411 958 934 408 402 472 170 600 908 511 997 793 17
4 | 23
5 | 964 524 191 487 460 802 331 283 488 476 927 30 825 270 469 58 14 935 842 8 820 712 387
6 | 51
7 | 424 657 883 262 666 227 408 173 247 308 539 400 746 213 475 362 322 699 608 654 295 256 998 599 485 720 928 257 338 815 477 903 350 339 598 323 246 533 705 824 717 639 200 496 220 784 673 95 529 77 728
8 | 10
9 | 1 7 2 4 6 8 10 3 5 9
10 | 11
11 | 107 35 363 507 549 568 791 523 340 487 848
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/4.out:
--------------------------------------------------------------------------------
1 | 32
2 | 20
3 | 46
4 | 7
5 | 7
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/5.in:
--------------------------------------------------------------------------------
1 | 2
2 | 15
3 | 708 120 389 537 495 593 522 374 859 109 962 686 668 559 992
4 | 82
5 | 846 800 68 43 214 682 680 972 704 987 523 322 907 488 233 370 298 490 998 116 382 166 351 754 427 711 558 84 423 841 90 723 385 441 816 572 155 245 730 779 416 253 182 577 692 238 859 759 765 91 218 61 930 528 949 246 906 466 128 481 343 629 413 545 418 869 411 286 871 315 677 371 184 99 93 978 904 288 297 257 882 728
6 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-15/5.out:
--------------------------------------------------------------------------------
1 | 11
2 | 77
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/1.in:
--------------------------------------------------------------------------------
1 | 1
2 | 13 24 3 56 34 3 78 12 29 49 84 51 9 100
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/1.out:
--------------------------------------------------------------------------------
1 | 3 3 9 12 13 24 29 34 49 51 56 78 84 100
2 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/2.in:
--------------------------------------------------------------------------------
1 | 2
2 | 10
3 | -2147483648 -2147483648 2147483647 0 2147483641 2147483646 -2147483648 -21474836 47483647
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/2.out:
--------------------------------------------------------------------------------
1 | 10
2 | -2147483648 -2147483648 -2147483648 -21474836 0 47483647 2147483641 2147483646 2147483647
3 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/3.in:
--------------------------------------------------------------------------------
1 | 3
2 | 94 79 92 75 47 46 60 84 45 30 5 4 81 74 26 12 2 30 65 56 27 9 3 67 12 91 15 68 43 21 88 44 92 75 89 56 7 94 31 28 73 40 3 59 88 15 35 89 24 33 75 39 64 71 73 19 4 17 94 13 77 72 33 98 4 11 3 59 56 64 72 55 74 70 47 42 73 19 41 53 49 58 8 43 11 1 27 30 25 72 34 81 32 49 10 68 44 22 12 69
3 | 72 81 51 53 3 56 2 45 71 57 65 32 19 51 4 33 48 10 12 14 36 27 8 43 13 84 99 23 27 54 2 29 49 63 34 52 12 38 53 82 18 48 5 81 32 91 88 38 48 38 29 99 33 3 50 66 49 25 51 46 1 81 85 38 85 70 49 87 10 99 57 30 91 24 54 86 59 61 93 10 2 84 16 58 4 67 18 31 59 30 64 0 34 49 22 7 82 74 28 36
4 | 74 6 58 8 70 70 18 33 22 35 35 92 73 10 21 89 4 50 63 83 82 30 57 68 9 30 82 68 2 68 36 30 40 84 74 43 66 75 3 11 60 21 35 73 16 16 37 64 58 69 68 34 41 36 48 78 54 61 63 69 45 81 6 56 41 44 79 61 4 79 46 18 17 68 37 96 36 19 63 71 71 62 82 71 1 74 87 57 12 6 82 83 93 68 80 78 69 65 68 72
5 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/3.out:
--------------------------------------------------------------------------------
1 | 1 2 3 3 3 4 4 4 5 7 8 9 10 11 11 12 12 12 13 15 15 17 19 19 21 22 24 25 26 27 27 28 30 30 30 31 32 33 33 34 35 39 40 41 42 43 43 44 44 45 46 47 47 49 49 53 55 56 56 56 58 59 59 60 64 64 65 67 68 68 69 70 71 72 72 72 73 73 73 74 74 75 75 75 77 79 81 81 84 88 88 89 89 91 92 92 94 94 94 98
2 | 0 1 2 2 2 3 3 4 4 5 7 8 10 10 10 12 12 13 14 16 18 18 19 22 23 24 25 27 27 28 29 29 30 30 31 32 32 33 33 34 34 36 36 38 38 38 38 43 45 46 48 48 48 49 49 49 49 50 51 51 51 52 53 53 54 54 56 57 57 58 59 59 61 63 64 65 66 67 70 71 72 74 81 81 81 82 82 84 84 85 85 86 87 88 91 91 93 99 99 99
3 | 1 2 3 4 4 6 6 6 8 9 10 11 12 16 16 17 18 18 19 21 21 22 30 30 30 33 34 35 35 35 36 36 36 37 37 40 41 41 43 44 45 46 48 50 54 56 57 57 58 58 60 61 61 62 63 63 63 64 65 66 68 68 68 68 68 68 68 69 69 69 70 70 71 71 71 72 73 73 74 74 74 75 78 78 79 79 80 81 82 82 82 82 83 83 84 87 89 92 93 96
4 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/4.in:
--------------------------------------------------------------------------------
1 | 2
2 | -477 -333 -245 -434 -57 -277 -464 -405 -126 82 -68 -370 -126 342 -134 148 -308 313 -247 127 -130 79 87 466 93 -193 237 466 104 130 -443 -454 -306 30 -46 307 -198 176 288 -390 47 -108 -476 263 36 29 396 -416 -310 255 484 69 408 469 -88 150 362 330 -349 22 294 176 285 349 291 -80 386 -232 -342 -133 108 -117 -412 -205 470 -250 -354 24 -169 -407 146 -310 426 -113 428 -146 -488 -211 -377 -322 -482 284 24 327 -101 376 334 -69 -213 72
3 | -2541 -773 2804 453 -4185 1042 -2807 3290 2808 -2081 960 529 -1952 -3598 4402 3369 2841 1698 -2145 -1456 4276 -2663 4366 2638 193 3328 560 265 4893 3752 -145 -4719 2325 2565 -3229 -3941 -1837 4662 -1569 2951 4720 -2245 -534 -1514 2970 -561 -417 -1409 4054 3009 -1211 -3762 -1380 -4993 3913 -2813 -3893 -2709 1448 1282 -2362 -3499 -4021 1670 -4410 4164 514 -1333 83 1444 4877 4594 -2325 3522 963 859 325 -34 -795 -1242 3389 -933 -1196 1080 -235 -3883 -4542 2451 3661 -2162 4166 2500 1482 -2191 1501 -2516 -804 577 1176 2754
4 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/4.out:
--------------------------------------------------------------------------------
1 | -488 -482 -477 -476 -464 -454 -443 -434 -416 -412 -407 -405 -390 -377 -370 -354 -349 -342 -333 -322 -310 -310 -308 -306 -277 -250 -247 -245 -232 -213 -211 -205 -198 -193 -169 -146 -134 -133 -130 -126 -126 -117 -113 -108 -101 -88 -80 -69 -68 -57 -46 22 24 24 29 30 36 47 69 72 79 82 87 93 104 108 127 130 146 148 150 176 176 237 255 263 284 285 288 291 294 307 313 327 330 334 342 349 362 376 386 396 408 426 428 466 466 469 470 484
2 | -4993 -4719 -4542 -4410 -4185 -4021 -3941 -3893 -3883 -3762 -3598 -3499 -3229 -2813 -2807 -2709 -2663 -2541 -2516 -2362 -2325 -2245 -2191 -2162 -2145 -2081 -1952 -1837 -1569 -1514 -1456 -1409 -1380 -1333 -1242 -1211 -1196 -933 -804 -795 -773 -561 -534 -417 -235 -145 -34 83 193 265 325 453 514 529 560 577 859 960 963 1042 1080 1176 1282 1444 1448 1482 1501 1670 1698 2325 2451 2500 2565 2638 2754 2804 2808 2841 2951 2970 3009 3290 3328 3369 3389 3522 3661 3752 3913 4054 4164 4166 4276 4366 4402 4594 4662 4720 4877 4893
3 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/5.in:
--------------------------------------------------------------------------------
1 | 2
2 | 985 1889 1687 -2182 -1489 -3636 920 -4233 2111 2517 4253 -4986 -4038 -3845 -4628 -2202 4087 -2361 1402 -3778 -4242 -651 -2469 1345 -2036 3714 -2663 4156 -1798 4382 -1671 4344 2743 2385 4330 -1469 462 3798 -3605 -4410 -36 -4708 1741 3080 -1791 -747 -4150 -4568 2157 682 -3723 1489 1013 3955 -1956 -3675 -4400 -1278 -501 -237 3129 692 4448 2024 139 4902 1047 -4934 3741 -3887 449 -3281 1512 3491 -1454 3601 3287 4428 2240 -2109 3959 -4596 -2324 2528 4957 -3799 1569 -805 3503 -2561 -1600 3487 2977 375 -4442 2132 4757 2280 -4203 -2238
3 | 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
4 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-16/5.out:
--------------------------------------------------------------------------------
1 | -4986 -4934 -4708 -4628 -4596 -4568 -4442 -4410 -4400 -4242 -4233 -4203 -4150 -4038 -3887 -3845 -3799 -3778 -3723 -3675 -3636 -3605 -3281 -2663 -2561 -2469 -2361 -2324 -2238 -2202 -2182 -2109 -2036 -1956 -1798 -1791 -1671 -1600 -1489 -1469 -1454 -1278 -805 -747 -651 -501 -237 -36 139 375 449 462 682 692 920 985 1013 1047 1345 1402 1489 1512 1569 1687 1741 1889 2024 2111 2132 2157 2240 2280 2385 2517 2528 2743 2977 3080 3129 3287 3487 3491 3503 3601 3714 3741 3798 3955 3959 4087 4156 4253 4330 4344 4382 4428 4448 4757 4902 4957
2 | 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52 53 53 54 54 55 55 56 56 57 57 58 58 59 59 60 60 61 61 62 62 63 63 64 64 65 65 66 66 67 67 68 68 69 69 70 70 71 71 72 72 73 73 74 74 75 75 76 76 77 77 78 78 79 79 80 80 81 81 82 82 83 83 84 84 85 85 86 86 87 87 88 88 89 89 90 90 91 91 92 92 93 93 94 94 95 95 96 96 97 97 98 98 99 99 100
3 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/1.in:
--------------------------------------------------------------------------------
1 | 1
2 | 13 24 3 56 34 3 78 12 29 49 84 51 9 100
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/1.out:
--------------------------------------------------------------------------------
1 | 3 3 9 12 13 24 29 34 49 51 56 78 84 100
2 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/2.in:
--------------------------------------------------------------------------------
1 | 2
2 | 10
3 | -2147483648 -2147483648 2147483647 0 2147483641 2147483646 -2147483648 -21474836 47483647
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/2.out:
--------------------------------------------------------------------------------
1 | 10
2 | -2147483648 -2147483648 -2147483648 -21474836 0 47483647 2147483641 2147483646 2147483647
3 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/3.in:
--------------------------------------------------------------------------------
1 | 3
2 | 94 79 92 75 47 46 60 84 45 30 5 4 81 74 26 12 2 30 65 56 27 9 3 67 12 91 15 68 43 21 88 44 92 75 89 56 7 94 31 28 73 40 3 59 88 15 35 89 24 33 75 39 64 71 73 19 4 17 94 13 77 72 33 98 4 11 3 59 56 64 72 55 74 70 47 42 73 19 41 53 49 58 8 43 11 1 27 30 25 72 34 81 32 49 10 68 44 22 12 69
3 | 72 81 51 53 3 56 2 45 71 57 65 32 19 51 4 33 48 10 12 14 36 27 8 43 13 84 99 23 27 54 2 29 49 63 34 52 12 38 53 82 18 48 5 81 32 91 88 38 48 38 29 99 33 3 50 66 49 25 51 46 1 81 85 38 85 70 49 87 10 99 57 30 91 24 54 86 59 61 93 10 2 84 16 58 4 67 18 31 59 30 64 0 34 49 22 7 82 74 28 36
4 | 74 6 58 8 70 70 18 33 22 35 35 92 73 10 21 89 4 50 63 83 82 30 57 68 9 30 82 68 2 68 36 30 40 84 74 43 66 75 3 11 60 21 35 73 16 16 37 64 58 69 68 34 41 36 48 78 54 61 63 69 45 81 6 56 41 44 79 61 4 79 46 18 17 68 37 96 36 19 63 71 71 62 82 71 1 74 87 57 12 6 82 83 93 68 80 78 69 65 68 72
5 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/3.out:
--------------------------------------------------------------------------------
1 | 1 2 3 3 3 4 4 4 5 7 8 9 10 11 11 12 12 12 13 15 15 17 19 19 21 22 24 25 26 27 27 28 30 30 30 31 32 33 33 34 35 39 40 41 42 43 43 44 44 45 46 47 47 49 49 53 55 56 56 56 58 59 59 60 64 64 65 67 68 68 69 70 71 72 72 72 73 73 73 74 74 75 75 75 77 79 81 81 84 88 88 89 89 91 92 92 94 94 94 98
2 | 0 1 2 2 2 3 3 4 4 5 7 8 10 10 10 12 12 13 14 16 18 18 19 22 23 24 25 27 27 28 29 29 30 30 31 32 32 33 33 34 34 36 36 38 38 38 38 43 45 46 48 48 48 49 49 49 49 50 51 51 51 52 53 53 54 54 56 57 57 58 59 59 61 63 64 65 66 67 70 71 72 74 81 81 81 82 82 84 84 85 85 86 87 88 91 91 93 99 99 99
3 | 1 2 3 4 4 6 6 6 8 9 10 11 12 16 16 17 18 18 19 21 21 22 30 30 30 33 34 35 35 35 36 36 36 37 37 40 41 41 43 44 45 46 48 50 54 56 57 57 58 58 60 61 61 62 63 63 63 64 65 66 68 68 68 68 68 68 68 69 69 69 70 70 71 71 71 72 73 73 74 74 74 75 78 78 79 79 80 81 82 82 82 82 83 83 84 87 89 92 93 96
4 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/4.in:
--------------------------------------------------------------------------------
1 | 2
2 | -477 -333 -245 -434 -57 -277 -464 -405 -126 82 -68 -370 -126 342 -134 148 -308 313 -247 127 -130 79 87 466 93 -193 237 466 104 130 -443 -454 -306 30 -46 307 -198 176 288 -390 47 -108 -476 263 36 29 396 -416 -310 255 484 69 408 469 -88 150 362 330 -349 22 294 176 285 349 291 -80 386 -232 -342 -133 108 -117 -412 -205 470 -250 -354 24 -169 -407 146 -310 426 -113 428 -146 -488 -211 -377 -322 -482 284 24 327 -101 376 334 -69 -213 72
3 | -2541 -773 2804 453 -4185 1042 -2807 3290 2808 -2081 960 529 -1952 -3598 4402 3369 2841 1698 -2145 -1456 4276 -2663 4366 2638 193 3328 560 265 4893 3752 -145 -4719 2325 2565 -3229 -3941 -1837 4662 -1569 2951 4720 -2245 -534 -1514 2970 -561 -417 -1409 4054 3009 -1211 -3762 -1380 -4993 3913 -2813 -3893 -2709 1448 1282 -2362 -3499 -4021 1670 -4410 4164 514 -1333 83 1444 4877 4594 -2325 3522 963 859 325 -34 -795 -1242 3389 -933 -1196 1080 -235 -3883 -4542 2451 3661 -2162 4166 2500 1482 -2191 1501 -2516 -804 577 1176 2754
4 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/4.out:
--------------------------------------------------------------------------------
1 | -488 -482 -477 -476 -464 -454 -443 -434 -416 -412 -407 -405 -390 -377 -370 -354 -349 -342 -333 -322 -310 -310 -308 -306 -277 -250 -247 -245 -232 -213 -211 -205 -198 -193 -169 -146 -134 -133 -130 -126 -126 -117 -113 -108 -101 -88 -80 -69 -68 -57 -46 22 24 24 29 30 36 47 69 72 79 82 87 93 104 108 127 130 146 148 150 176 176 237 255 263 284 285 288 291 294 307 313 327 330 334 342 349 362 376 386 396 408 426 428 466 466 469 470 484
2 | -4993 -4719 -4542 -4410 -4185 -4021 -3941 -3893 -3883 -3762 -3598 -3499 -3229 -2813 -2807 -2709 -2663 -2541 -2516 -2362 -2325 -2245 -2191 -2162 -2145 -2081 -1952 -1837 -1569 -1514 -1456 -1409 -1380 -1333 -1242 -1211 -1196 -933 -804 -795 -773 -561 -534 -417 -235 -145 -34 83 193 265 325 453 514 529 560 577 859 960 963 1042 1080 1176 1282 1444 1448 1482 1501 1670 1698 2325 2451 2500 2565 2638 2754 2804 2808 2841 2951 2970 3009 3290 3328 3369 3389 3522 3661 3752 3913 4054 4164 4166 4276 4366 4402 4594 4662 4720 4877 4893
3 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/5.in:
--------------------------------------------------------------------------------
1 | 2
2 | 985 1889 1687 -2182 -1489 -3636 920 -4233 2111 2517 4253 -4986 -4038 -3845 -4628 -2202 4087 -2361 1402 -3778 -4242 -651 -2469 1345 -2036 3714 -2663 4156 -1798 4382 -1671 4344 2743 2385 4330 -1469 462 3798 -3605 -4410 -36 -4708 1741 3080 -1791 -747 -4150 -4568 2157 682 -3723 1489 1013 3955 -1956 -3675 -4400 -1278 -501 -237 3129 692 4448 2024 139 4902 1047 -4934 3741 -3887 449 -3281 1512 3491 -1454 3601 3287 4428 2240 -2109 3959 -4596 -2324 2528 4957 -3799 1569 -805 3503 -2561 -1600 3487 2977 375 -4442 2132 4757 2280 -4203 -2238
3 | 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
4 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-17/5.out:
--------------------------------------------------------------------------------
1 | -4986 -4934 -4708 -4628 -4596 -4568 -4442 -4410 -4400 -4242 -4233 -4203 -4150 -4038 -3887 -3845 -3799 -3778 -3723 -3675 -3636 -3605 -3281 -2663 -2561 -2469 -2361 -2324 -2238 -2202 -2182 -2109 -2036 -1956 -1798 -1791 -1671 -1600 -1489 -1469 -1454 -1278 -805 -747 -651 -501 -237 -36 139 375 449 462 682 692 920 985 1013 1047 1345 1402 1489 1512 1569 1687 1741 1889 2024 2111 2132 2157 2240 2280 2385 2517 2528 2743 2977 3080 3129 3287 3487 3491 3503 3601 3714 3741 3798 3955 3959 4087 4156 4253 4330 4344 4382 4428 4448 4757 4902 4957
2 | 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52 53 53 54 54 55 55 56 56 57 57 58 58 59 59 60 60 61 61 62 62 63 63 64 64 65 65 66 66 67 67 68 68 69 69 70 70 71 71 72 72 73 73 74 74 75 75 76 76 77 77 78 78 79 79 80 80 81 81 82 82 83 83 84 84 85 85 86 86 87 87 88 88 89 89 90 90 91 91 92 92 93 93 94 94 95 95 96 96 97 97 98 98 99 99 100
3 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-21/1.in:
--------------------------------------------------------------------------------
1 | 5
2 | 0
3 |
4 | 1
5 | 1
6 | 2
7 | 1 2
8 | 2
9 | 2 1
10 | 2
11 | 1 1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-21/1.out:
--------------------------------------------------------------------------------
1 | 0
2 | 0
3 | 0
4 | 1
5 | 0
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-21/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 3
3 | -1 0 1
4 | 3
5 | 1 0 1
6 | 3
7 | -1 0 -1
8 | 11
9 | -20 -2 -17 4 -22 -10 -30 45 -48 17 -49
10 | 17
11 | -26 -37 -3 -18 42 -44 -4 39 -24 30 48 6 31 -33 -44 -23 7
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-21/2.out:
--------------------------------------------------------------------------------
1 | 0
2 | 1
3 | 1
4 | 32
5 | 62
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-21/3.in:
--------------------------------------------------------------------------------
1 | 5
2 | 18
3 | 39 -25 43 -19 -9 41 8 -33 -45 9 30 -11 37 2 49 43 -31 -28
4 | 15
5 | -22 17 -42 44 5 -40 43 -14 -11 -26 -8 16 -19 -8 -26
6 | 10
7 | 0 -47 13 -31 -16 -44 15 5 -6 -27
8 | 19
9 | -25 25 -15 25 17 -38 -3 -10 18 -14 -20 40 -21 -44 41 23 9 9 35
10 | 13
11 | 31 -38 -28 41 -24 -39 49 7 48 -48 25 46 -42
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-21/3.out:
--------------------------------------------------------------------------------
1 | 76
2 | 55
3 | 21
4 | 74
5 | 39
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/1.in:
--------------------------------------------------------------------------------
1 | 1
2 | 49 38 65 97 76 13 27 49 55 4
3 | 5 3
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/1.out:
--------------------------------------------------------------------------------
1 | 13 4 49 38 27 49 55 65 97 76
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 42 82 37 49 36 73 11 76 10 11 60 27 20 4 80 74 35 37 53 5 46 95 25 72 78 17 84 76 85 85 29 65 46 89 35 88 59 87 29 43 21 39 21 60 2 66 22 26 41 75
3 | 5 4 3 2
4 | 95 63 23 51 4 31 87 69 54 80 78 98 75 93 56 48 50
5 | 100
6 | 52 91 1 9 76 24 64 42 43 27 0 95 63 23 51 4 31 87 69 54 80 78 98 75 93 56 48 50
7 | 1
8 | 35 92 59 62 2 72 67 52 91 1 9 76 24 6
9 | 9 6 3
10 | 48 54 15 88 90 4 51 55 36 17 95 82 21 12 60 14 50 48 39 65 97 52 37 74 95 38 78 94 58 33 40 91 49 58 49 82 1 99 88 34 62 46 83 5 39 78 2 18 67 9
11 | 2 3 4 5 6 7 8 9 10
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/2.out:
--------------------------------------------------------------------------------
1 | 2 4 10 5 11 11 17 21 20 21 22 26 25 27 29 29 35 35 36 37 37 39 41 42 46 43 46 49 53 59 60 65 60 66 72 73 74 76 75 76 78 82 80 85 84 85 88 87 89 95
2 | 95 63 23 51 4 31 87 69 54 80 78 98 75 93 56 48 50
3 | 0 1 4 9 23 24 27 31 42 43 48 50 51 52 54 56 63 64 69 75 76 78 80 87 91 93 95 98
4 | 1 2 59 24 6 72 35 9 76 62 52 91 67 92
5 | 1 4 2 5 12 9 14 15 17 21 18 34 33 37 36 39 38 39 46 40 48 48 49 49 50 52 51 54 58 55 60 58 62 65 67 74 78 78 82 82 88 83 90 88 94 91 95 95 97 99
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/3.in:
--------------------------------------------------------------------------------
1 | 1
2 | 0 -35 -30 32 -26 -38 19 5 7 -10 -43 36 20 6 -43 -26 -32 5 -46 1 47 39 9 -40 31 -46 -34 25 -8 38 0 40 20 -45 31 -46 -47 -44 30 18 -4 -19 49 -3 24 26 -6 42 -8 -48
3 | 3 5 7 9
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/3.out:
--------------------------------------------------------------------------------
1 | -47 -48 -46 -46 -46 -43 -45 -44 -40 -34 -43 -38 -30 -35 -26 -19 -32 -10 -6 -26 -8 -3 -8 0 1 -4 0 5 5 18 6 7 19 9 20 20 26 24 25 31 30 31 32 36 40 39 38 47 49 42
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/4.in:
--------------------------------------------------------------------------------
1 | 1
2 | 10 9 8 7 6 5 4 3 2 1
3 | 3
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/4.out:
--------------------------------------------------------------------------------
1 | 1 3 2 4 6 5 7 9 8 10
2 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/5.in:
--------------------------------------------------------------------------------
1 | 1
2 | -18 -40 -27 -42 -7 -35 18 35 -6 36 49 -16 8 -42 -28 -16 8 -17 37 9 11 33 -43 2 -15 15 -3 37 -1 -38 36 27 9 19 26 -3 49 16 -48 -20 -21 -10 29 -41 -22 -20 23 5 -25 42
3 | 11 9 0 7 1 11 9 9 1 5
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-22/5.out:
--------------------------------------------------------------------------------
1 | -48 -43 -42 -42 -41 -40 -38 -35 -28 -27 -25 -22 -21 -20 -20 -18 -17 -16 -16 -15 -10 -7 -6 -3 -3 -1 2 5 8 8 9 9 11 15 16 18 19 23 26 27 29 33 35 36 36 37 37 42 49 49
2 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-23/1.in:
--------------------------------------------------------------------------------
1 | 5
2 | 1 0 0
3 | 0 1
4 | 1 0 0
5 | 1 0
6 | 1 0 1
7 | 0 0
8 | 1 1 0
9 | 0 0
10 | 1 1 1
11 | 0 0
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-23/1.out:
--------------------------------------------------------------------------------
1 | 1 0,1 1
2 | 0 1,1 1
3 | 1 0,1 1
4 | 0 1,1 1
5 | 0 1,1 0
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-23/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 2 1 1
3 | 0 1
4 | 2 1 1
5 | 2 1
6 | 2 1 1
7 | 1 2
8 | 2 1 1
9 | 3 3
10 | 4 1 1
11 | 4 5
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-23/2.out:
--------------------------------------------------------------------------------
1 | 0 0,1 0
2 | 1 2,2 2
3 | 2 1,2 2
4 | 2 3,3 2
5 | 5 4,5 5
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-23/3.in:
--------------------------------------------------------------------------------
1 | 5
2 | 5 12 11
3 | 0 3
4 | 5 9 10
5 | 2 0
6 | 4 3 7
7 | 0 4
8 | 4 2 0
9 | 10 0
10 | 5 21 1
11 | 15 14
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-23/3.out:
--------------------------------------------------------------------------------
1 | 0 2,1 3
2 | 3 0,3 1
3 | 0 5,1 4
4 | 11 0,11 1
5 | 14 14,14 15
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/1.in:
--------------------------------------------------------------------------------
1 | 1
2 | 4
3 | 2 3 2 5
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/1.out:
--------------------------------------------------------------------------------
1 | 2 2 3 5
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 50
3 | 9 2 9 8 9 9 1 7 7 8 4 9 3 2 4 5 0 5 8 8 7 0 2 8 1 2 2 7 5 7 7 6 5 4 3 1 4 6 7 6 2 2 6 9 8 1 7 4 1 0
4 | 50
5 | 5 5 4 8 7 7 6 7 8 0 5 5 6 1 6 5 3 2 0 8 2 9 6 6 3 6 0 6 3 5 2 4 9 6 4 9 1 0 8 8 3 7 0 1 1 5 4 3 4 5
6 | 50
7 | 3 1 7 9 6 8 1 8 8 4 6 2 5 3 9 8 1 2 5 1 4 6 7 0 5 6 1 8 5 6 9 4 7 6 2 2 8 0 5 9 9 9 2 7 9 3 8 0 7 1
8 | 50
9 | 9 0 2 0 5 8 7 6 4 1 3 8 8 7 7 4 1 8 4 9 7 3 1 1 1 4 8 8 3 0 7 8 8 4 0 4 0 3 9 7 5 6 5 6 5 6 0 2 0 5
10 | 50
11 | 1 7 4 4 0 0 4 3 9 2 2 3 8 3 7 7 9 0 1 5 5 6 6 1 4 3 1 0 9 7 6 4 5 8 4 4 1 8 7 1 1 9 2 2 6 7 0 2 0 9
12 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/2.out:
--------------------------------------------------------------------------------
1 | 7 7 7 7 7 7 7 7 2 2 2 2 2 2 2 8 8 8 8 8 8 9 9 9 9 9 9 1 1 1 1 1 4 4 4 4 4 5 5 5 5 6 6 6 6 0 0 0 3 3
2 | 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 0 0 0 0 0 3 3 3 3 3 4 4 4 4 4 8 8 8 8 8 1 1 1 1 7 7 7 7 2 2 2 9 9 9
3 | 8 8 8 8 8 8 8 9 9 9 9 9 9 9 1 1 1 1 1 1 6 6 6 6 6 6 2 2 2 2 2 5 5 5 5 5 7 7 7 7 7 0 0 0 3 3 3 4 4 4
4 | 8 8 8 8 8 8 8 8 0 0 0 0 0 0 0 4 4 4 4 4 4 7 7 7 7 7 7 1 1 1 1 1 5 5 5 5 5 3 3 3 3 6 6 6 6 9 9 9 2 2
5 | 1 1 1 1 1 1 1 4 4 4 4 4 4 4 0 0 0 0 0 0 7 7 7 7 7 7 2 2 2 2 2 9 9 9 9 9 3 3 3 3 6 6 6 6 5 5 5 8 8 8
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/3.in:
--------------------------------------------------------------------------------
1 | 5
2 | 50
3 | 3 0 1 0 0 3 0 7 -2 -9 -6 -1 -9 -5 0 3 1 -2 -1 1 7 -7 0 -4 -3 -8 7 -8 4 2 7 -5 -9 -6 -3 -8 2 7 3 1 4 4 -3 5 -2 9 0 -3 -6 6
4 | 50
5 | 3 -7 0 -1 9 -7 -1 8 3 -7 4 -5 0 -9 -2 -7 9 -3 -8 1 4 -4 1 0 -5 -2 -9 9 -4 -6 6 9 -5 -4 -6 -5 6 0 0 -6 -1 1 -8 4 -1 -6 5 -4 -5 4
6 | 50
7 | 3 8 -7 1 9 -9 -4 8 -8 4 -5 9 2 9 -2 4 -3 6 8 0 7 9 0 -9 -7 -3 -1 4 5 -6 7 7 -5 3 -8 -7 1 5 3 4 2 6 -3 -4 -2 -4 9 8 -7 -3
8 | 50
9 | 7 -6 -9 0 2 0 6 0 5 -5 6 -1 -4 -7 0 -9 0 -5 8 5 8 4 2 1 -1 -5 -8 4 -4 0 9 -4 2 -3 -8 0 2 -5 2 8 0 -4 1 7 -8 3 0 5 -6 -1
10 | 50
11 | 0 -6 0 6 -4 -7 4 3 -1 0 4 1 -6 0 0 3 -9 0 7 -2 9 -5 5 7 8 7 -1 1 -2 7 0 -5 1 3 9 -2 -9 -2 3 -8 9 4 -3 -9 1 7 -6 -1 2 -9
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/3.out:
--------------------------------------------------------------------------------
1 | 0 0 0 0 0 0 0 7 7 7 7 7 -3 -3 -3 -3 1 1 1 1 3 3 3 3 -9 -9 -9 -8 -8 -8 -6 -6 -6 -2 -2 -2 4 4 4 -5 -5 -1 -1 2 2 -7 -4 5 6 9
2 | -5 -5 -5 -5 -5 0 0 0 0 0 -7 -7 -7 -7 -6 -6 -6 -6 -4 -4 -4 -4 -1 -1 -1 -1 4 4 4 4 9 9 9 9 1 1 1 -9 -9 -8 -8 -2 -2 3 3 6 6 -3 5 8
3 | 9 9 9 9 9 -7 -7 -7 -7 -3 -3 -3 -3 4 4 4 4 8 8 8 8 -4 -4 -4 3 3 3 7 7 7 -9 -9 -8 -8 -5 -5 -2 -2 0 0 1 1 2 2 5 5 6 6 -6 -1
4 | 0 0 0 0 0 0 0 0 0 2 2 2 2 2 -5 -5 -5 -5 -4 -4 -4 -4 -8 -8 -8 -1 -1 -1 5 5 5 8 8 8 -9 -9 -6 -6 1 1 4 4 6 6 7 7 -7 -3 3 9
5 | 0 0 0 0 0 0 0 7 7 7 7 7 -9 -9 -9 -9 -2 -2 -2 -2 1 1 1 1 3 3 3 3 -6 -6 -6 -1 -1 -1 4 4 4 9 9 9 -5 -5 -8 -7 -4 -3 2 5 6 8
6 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/4.in:
--------------------------------------------------------------------------------
1 | 1
2 | 5
3 | 2147483647 -2147483648 -2147483648 2147483647 2147483647
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/4.out:
--------------------------------------------------------------------------------
1 | 2147483647 2147483647 2147483647 -2147483648 -2147483648
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/5.in:
--------------------------------------------------------------------------------
1 | 1
2 | 50
3 | 319 99 334 -483 -310 92 -184 -300 286 179 86 -470 243 -290 74 -471 -465 -219 436 -397 327 279 153 163 -260 190 -145 477 -40 496 -69 384 68 458 -142 232 -379 186 -481 -337 173 181 -486 -102 -454 -30 -430 287 485 51
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-25/5.out:
--------------------------------------------------------------------------------
1 | -486 -483 -481 -471 -470 -465 -454 -430 -397 -379 -337 -310 -300 -290 -260 -219 -184 -145 -142 -102 -69 -40 -30 51 68 74 86 92 99 153 163 173 179 181 186 190 232 243 279 286 287 319 327 334 384 436 458 477 485 496
2 |
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/1.in:
--------------------------------------------------------------------------------
1 | 2
2 | 2 4
3 | 10 10 10 10
4 | 2 4
5 | 10 20 30 40
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/1.out:
--------------------------------------------------------------------------------
1 | 20
2 | 60
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 5 10
3 | 1 2 3 4 5 6 7 8 9 10
4 | 30 50
5 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500
6 | 5 5
7 | 1 2 3 4 5
8 | 5 3
9 | 1 2 3
10 | 1 1
11 | 100
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/2.out:
--------------------------------------------------------------------------------
1 | 15
2 | 570
3 | 5
4 | 3
5 | 100
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/3.in:
--------------------------------------------------------------------------------
1 | 1
2 | 3 5
3 | 100 99 98 97 96
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/3.out:
--------------------------------------------------------------------------------
1 | 197
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/4.in:
--------------------------------------------------------------------------------
1 | 2
2 | 10 20
3 | 500 1 499 2 498 3 497 4 496 5 495 6 494 7 493 8 492 9 491 10
4 | 3 20
5 | 2193 1231 3245 2349 5486 9320 5843 1103 1203 4410 1091 1211 1239 2589 4509 9098 89 123 1234 234
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/4.out:
--------------------------------------------------------------------------------
1 | 501
2 | 21879
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/5.in:
--------------------------------------------------------------------------------
1 | 2
2 | 4 10
3 | 1 495 2 395 3 295 4 195 100 50
4 | 3 10
5 | 100 100 100 100 100 100 100 100 100 100
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-5/5.out:
--------------------------------------------------------------------------------
1 | 496
2 | 400
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-6/1.in:
--------------------------------------------------------------------------------
1 | 5
2 | 1
3 | 100000
4 | 100001
5 | 100002
6 | 1000000000000000
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-6/1.out:
--------------------------------------------------------------------------------
1 | 1
2 | 66
3 | 66
4 | 66
5 | 144224
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-6/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 98021
3 | 102510
4 | 107134
5 | 111895
6 | 116795
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-6/2.out:
--------------------------------------------------------------------------------
1 | 66
2 | 67
3 | 68
4 | 69
5 | 70
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-6/3.in:
--------------------------------------------------------------------------------
1 | 5
2 | 98020
3 | 102509
4 | 107133
5 | 111894
6 | 116794
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-6/3.out:
--------------------------------------------------------------------------------
1 | 65
2 | 66
3 | 67
4 | 68
5 | 69
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/1.in:
--------------------------------------------------------------------------------
1 | 2
2 | 7
3 | 7 6 5 4 3 2 1
4 | 6
5 | 5 6 4 9 2 1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/1.out:
--------------------------------------------------------------------------------
1 | 7
2 | 5 6
3 | 1 2 3 4
4 | 5
5 | 4 6
6 | 1 2 9
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 5
3 | 1 1 1 1 1
4 | 10
5 | 100 99 98 97 96 95 94 93 92 91
6 | 100
7 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500
8 | 20
9 | -1 213 -23 485 93 -43 -456 0 -2 -5 987 98 12 -12 -213 -213 -542 -235 -21 -90
10 | 10
11 | -100 -200 -300 -5 -1 -8 -4 0 -2 -2
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/2.out:
--------------------------------------------------------------------------------
1 | 1
2 | 1
3 | 1
4 | 100
5 | 98 99
6 | 94 95 96 97
7 | 91 92 93
8 | 10
9 | 20 30
10 | 40 50 60 70
11 | 80 90 100 110 120 130 140 150
12 | 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310
13 | 10 20 30 40 50 60 70 80 90 100 110 120 130 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500
14 | 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500
15 | -1
16 | -23 213
17 | -456 -43 93 485
18 | -213 -12 -5 -2 0 12 98 987
19 | -542 -235 -213 -90 -21
20 | -100
21 | -300 -200
22 | -8 -5 -4 -1
23 | -2 0
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/3.in:
--------------------------------------------------------------------------------
1 | 1
2 | 1
3 | 1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/3.out:
--------------------------------------------------------------------------------
1 | 1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/4.in:
--------------------------------------------------------------------------------
1 | 1
2 | 11
3 | 2147483647 -2147483648 2147483647 -2147483648 2147483647 -2147483648 2147483647 -2147483648 0 2147483647 -2147483648
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/4.out:
--------------------------------------------------------------------------------
1 | 2147483647
2 | -2147483648 2147483647
3 | -2147483648 2147483647
4 | -2147483648 0 2147483647
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/5.in:
--------------------------------------------------------------------------------
1 | 1
2 | 10
3 | 10 9 8 7 6 5 4 3 2 1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-7/5.out:
--------------------------------------------------------------------------------
1 | 10
2 | 8 9
3 | 4 5 6 7
4 | 1 2 3
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/1.in:
--------------------------------------------------------------------------------
1 | 3
2 | 3 2 4
3 | 10 9 6
4 | 450 768 517
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/1.out:
--------------------------------------------------------------------------------
1 | 1
2 | 4
3 | 34
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/2.in:
--------------------------------------------------------------------------------
1 | 1
2 | 100000 100000 100000
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/2.out:
--------------------------------------------------------------------------------
1 | 0
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/3.in:
--------------------------------------------------------------------------------
1 | 1
2 | 1 9999 9999
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/3.out:
--------------------------------------------------------------------------------
1 | 1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/4.in:
--------------------------------------------------------------------------------
1 | 5
2 | 4652 7979 4256
3 | 7026 7848 3917
4 | 9039 3387 4798
5 | 5826 6872 7018
6 | 705 9422 2988
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/4.out:
--------------------------------------------------------------------------------
1 | 2816
2 | 2707
3 | 1887
4 | 4482
5 | 837
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/5.in:
--------------------------------------------------------------------------------
1 | 5
2 | 2 10 10
3 | 3 10 10
4 | 2 100 100
5 | 2 1000 1000
6 | 3 10000 1000
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-8/5.out:
--------------------------------------------------------------------------------
1 | 4
2 | 9
3 | 76
4 | 376
5 | 1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-9/1.in:
--------------------------------------------------------------------------------
1 | 5
2 | 0
3 |
4 | 1
5 | 1 2
6 | 3
7 | 0 0 0 -1 0 2
8 | 3
9 | 0 0 -1 0 2 0
10 | 5
11 | 0 0 -2 -1 4 2 -6 -3 8 4
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-9/1.out:
--------------------------------------------------------------------------------
1 | -1
2 | -1
3 | -1
4 | -1
5 | -1
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-9/2.in:
--------------------------------------------------------------------------------
1 | 5
2 | 7
3 | 2 2 0 -2 -2 0 0 0 -1 -1 0 1 1 0
4 | 14
5 | -39 15 -43 24 -34 -23 48 33 -9 49 -14 -44 -37 -33 -20 -1 -9 2 10 20 46 6 -11 -42 -41 7 38 -36
6 | 11
7 | 5 49 22 -23 12 16 43 -42 33 44 -1 -40 -45 29 -2 49 29 -28 -43 42 -10 -19
8 | 16
9 | -24 0 -41 26 26 -19 31 -38 -16 20 37 36 32 28 -22 14 31 14 -9 -10 38 -3 -45 33 28 -32 -22 -40 -41 20 -12 42
10 | 15
11 | 19 -47 4 36 -19 9 -4 40 -22 10 -49 -28 -7 -8 22 27 -50 35 17 -12 37 17 -46 29 6 -36 14 -26 -45 -45
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-9/2.out:
--------------------------------------------------------------------------------
1 | -2 0, -1 -1, 0 -2, 0 1, 1 0, 2 2
2 | -43 24, -37 -33, -14 -44, -9 49, 38 -36, 46 6, 48 33
3 | -45 29, -43 42, -2 49, -1 -40, 5 49, 33 44, 43 -42
4 | -45 33, -41 20, -22 -40, -12 42, 31 -38, 37 36, 38 -3
5 | -50 35, -49 -28, -45 -45, -4 40, 4 36, 19 -47, 22 27, 37 17
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-9/3.in:
--------------------------------------------------------------------------------
1 | 5
2 | 19
3 | -47 21 11 -5 27 46 -21 35 9 -40 -1 4 43 16 49 3 3 -2 23 29 38 32 35 13 -4 -10 -16 -47 8 -6 -44 43 26 16 37 -41 -49 -4
4 | 11
5 | 3 -3 28 29 29 15 -34 14 -12 27 35 -43 17 26 27 0 30 14 -5 -16 19 -37
6 | 18
7 | 10 14 -3 -23 15 -15 48 -30 -1 30 -27 -40 25 4 28 0 43 -18 13 21 38 45 15 41 -6 -34 0 27 23 28 -50 -31 42 -4 -18 -46
8 | 11
9 | -47 15 -19 -35 31 -21 12 -5 24 -40 11 -26 30 26 27 1 7 3 33 15 18 0
10 | 12
11 | 1 34 40 41 -21 5 45 10 20 19 25 42 10 34 11 30 3 32 50 -7 2 34 -22 38
--------------------------------------------------------------------------------
/TestCase/FirstExercise/1-9/3.out:
--------------------------------------------------------------------------------
1 | -49 -4, -47 21, -44 43, -16 -47, 27 46, 37 -41, 38 32, 49 3
2 | -34 14, -12 27, -5 -16, 19 -37, 28 29, 30 14, 35 -43
3 | -50 -31, -18 -46, -1 30, 15 41, 38 45, 48 -30
4 | -47 15, -19 -35, 24 -40, 30 26, 31 -21, 33 15
5 | -22 38, -21 5, 25 42, 40 41, 50 -7
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Point.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Point.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0105.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0105.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0106.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0106.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0107.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0107.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0108.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0108.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0109.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0109.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0110.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0110.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0112.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0112.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0113.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0113.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0114.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0114.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0115.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0115.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0116.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0116.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0117.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0117.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0118.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0118.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0119.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0119.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0120.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0120.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0121.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0121.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0122.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0122.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0123.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0123.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0124$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0124$1.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0124.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0124.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0125$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0125$1.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/FirstExercise/Problem0125.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/FirstExercise/Problem0125.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0101.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0101.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0102.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0102.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0103.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0103.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0104.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0104.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0105.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0105.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0106.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0106.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0107.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0107.class
--------------------------------------------------------------------------------
/out/production/AdvancedAlgorithm/Test/Problem0108.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/njuselw/AdvancedAlgorithm/845e9446cadfa2572efe0ec6dec96b00e3e51fb0/out/production/AdvancedAlgorithm/Test/Problem0108.class
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0501.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Problem0501 {
7 | /*
8 | 格子里的整数
9 | Description
10 | Given a square grid of size n, each cell of which contains integer cost which represents a cost to traverse through that cell,
11 | we need to find a path from top left cell to bottom right cell by which total cost incurred is minimum.
12 | Note : It is assumed that negative cost cycles do not exist in input matrix.
13 |
14 | Input
15 | The first line of input will contain number of test cases T. Then T test cases follow .
16 | Each test case contains 2 lines. The first line of each test case contains an integer n denoting the size of the grid.
17 | Next line of each test contains a single line containing N*N space separated integers depecting cost of respective cell from (0,0) to (n,n).
18 | Constraints:1<=T<=50,1<= n<= 50
19 |
20 | Output
21 | For each test case output a single integer depecting the minimum cost to reach the destination.
22 | */
23 |
24 | public static void main(String[] args) {
25 | Scanner scanner = new Scanner(System.in);
26 | int t = scanner.nextInt();
27 | for (int i = 0; i < t; i++) {
28 | int n = scanner.nextInt();
29 | int[][] matrix = new int[n][n];
30 | for (int j = 0; j < n; j++) {
31 | for (int k = 0; k < n; k++) {
32 | matrix[j][k] = scanner.nextInt();
33 | }
34 | }
35 | System.out.println(getMinCost(matrix));
36 | }
37 | }
38 |
39 | public static int getMinCost(int[][] matrix) {
40 | int n = matrix.length;
41 | int[][] minMatrix = new int[n][n];
42 | for (int i = 0; i < n; i++) {
43 | Arrays.fill(minMatrix[i], Integer.MAX_VALUE);
44 | }
45 | dfs(0, 0, minMatrix, matrix, matrix[0][0]);
46 | return minMatrix[n - 1][n - 1];
47 | }
48 |
49 | public static void dfs(int i, int j, int[][] minMatrix, int[][] matrix, int cur) {
50 | int n = matrix.length;
51 | int[][] dir = new int[][]{{i + 1, j}, {i, j + 1}, {i - 1, j}, {i, j - 1}};
52 | for (int[] d: dir) {
53 | int x = d[0], y = d[1];
54 | if (x >= 0 && x < n && y >= 0 && y < n && minMatrix[x][y] > matrix[x][y] + cur) {
55 | minMatrix[x][y] = matrix[x][y] + cur;
56 | dfs(x, y, minMatrix, matrix, minMatrix[x][y]);
57 | }
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0502.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0502 {
6 | /*
7 | 路上的球
8 | Description
9 | There are two parallel roads, each containing N and M buckets, respectively. Each bucket may contain some balls.
10 | The buckets on both roads are kept in such a way that they are sorted according to the number of balls in them.
11 | Geek starts from the end of the road which has the bucket with a lower number of balls
12 | (i.e. if buckets are sorted in increasing order, then geek will start from the left side of the road).
13 | The geek can change the road only at the point of intersection(which means, buckets with the same number of balls on two roads).
14 | Now you need to help Geek to collect the maximum number of balls.
15 |
16 | Input
17 | The first line of input contains T denoting the number of test cases.
18 | The first line of each test case contains two integers N and M, denoting the number of buckets on road1 and road2 respectively.
19 | 2nd line of each test case contains N integers, number of balls in buckets on the first road.
20 | 3rd line of each test case contains M integers, number of balls in buckets on the second road.
21 | Constraints:1<= T <= 1000,1<= N <= 10^3,1<= M <=10^3,0<= A[i],B[i]<=10^6
22 |
23 | Output
24 | For each test case output a single line containing the maximum possible balls that Geek can collect.
25 | */
26 |
27 | public static void main(String[] args) {
28 | Scanner scanner = new Scanner(System.in);
29 | int t = scanner.nextInt();
30 | for (int i = 0; i < t; i++) {
31 | int n = scanner.nextInt();
32 | int m = scanner.nextInt();
33 | int[] a = new int[n];
34 | for (int j = 0; j < n; j++) {
35 | a[j] = scanner.nextInt();
36 | }
37 | int[] b = new int[m];
38 | for (int j = 0; j < m; j++) {
39 | b[j] = scanner.nextInt();
40 | }
41 | System.out.println(getMaxBallCnt(a, b));
42 | }
43 | }
44 |
45 | //a: 1 4 5 6 8 -> 1,4 5,6 8
46 | //b: 2 3 4 6 9 -> 2,3,4 6 9
47 | //相同值将数组分成不同的段,每次选择最大的部分即可
48 | public static int getMaxBallCnt(int[] a, int[] b) {
49 | int n = a.length, m = b.length;
50 | int i = 0, j = 0;
51 | int res = 0;
52 | int sum1 = 0, sum2 = 0;
53 | while (i < n && j < m) {
54 | if (a[i] < b[j]) {
55 | sum1 += a[i];
56 | i++;
57 | } else if (a[i] > b[j]) {
58 | sum2 += b[j];
59 | j++;
60 | } else {
61 | res += Math.max(sum1, sum2) + a[i];
62 | i++;
63 | j++;
64 | sum1 = sum2 = 0;
65 | }
66 | }
67 | while (i < n) {
68 | sum1 += a[i];
69 | i++;
70 | }
71 | while (j < m) {
72 | sum2 += b[j];
73 | j++;
74 | }
75 | res += Math.max(sum1, sum2);
76 | return res;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0503.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Problem0503 {
7 | /*
8 | 硬币最小数量
9 | Description
10 | Given the list of coins of distinct denominations and total amount of money.
11 | Output the minimum number of coins required to make up that amount.
12 | Output -1 if that money cannot be made up using given coins.
13 | You may assume that there are infinite numbers of coins of each type.
14 |
15 | Input
16 | The first line contains 'T' denoting the number of test cases. Then follows description of test cases.
17 | Each cases begins with the two space separated integers 'n' and 'amount' denoting the total number of distinct coins and total amount of money respectively.
18 | The second line contains N space-separated integers A1, A2, ..., AN denoting the values of coins.
19 | Constraints:1<=T<=30,1<=n<=100,1<=Ai<=1000,1<=amount<=100000
20 |
21 | Output
22 | Print the minimum number of coins required to make up that amount or return -1 if it is impossible to make that amount using given coins.
23 | */
24 |
25 | public static void main(String[] args) {
26 | Scanner scanner = new Scanner(System.in);
27 | int t = scanner.nextInt();
28 | for (int i = 0; i < t; i++) {
29 | int n = scanner.nextInt();
30 | int amount = scanner.nextInt();
31 | int[] coins = new int[n];
32 | for (int j = 0; j < n; j++) {
33 | coins[j] = scanner.nextInt();
34 | }
35 | System.out.println(getMinCoinCnt(coins, amount));
36 | }
37 | }
38 |
39 | //这不是无限量的背包问题吗
40 | //dp[i]表示拿i钱所需的最小钞票数
41 | public static int getMinCoinCnt(int[] coins, int amount) {
42 | int[] dp = new int[amount + 1];
43 | Arrays.fill(dp, amount + 1);
44 | dp[0] = 0;
45 | for (int i = 1; i <= amount; i++) {
46 | for (int j = 0; j < coins.length; j++) {
47 | if (coins[j] <= i) { //显然如果这个钞票的面额超过所需钱数,就无法拿来凑
48 | dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
49 | }
50 | }
51 | }
52 | return dp[amount] >= amount + 1 ? -1 : dp[amount];
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0504.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Comparator;
5 | import java.util.Scanner;
6 |
7 | public class Problem0504 {
8 | /*
9 | 时间与收益
10 | Description
11 | Given a set of n jobs where each job i has a deadline and profit associated to it.
12 | Each job takes 1 unit of time to complete and only one job can be scheduled at a time.
13 | We earn the profit if and only if the job is completed by its deadline.
14 | The task is to find the maximum profit and the number of jobs done.
15 |
16 | Input
17 | The first line of input contains an integer T denoting the number of test cases.
18 | Each test case consist of an integer N denoting the number of jobs and the next line consist of Job id, Deadline and the Profit associated to that Job.
19 | Constraints:1<=T<=100,1<=N<=100,1<=Deadline<=100,1<=Profit<=500
20 |
21 | Output
22 | Output the number of jobs done and the maximum profit.
23 | */
24 |
25 | public static void main(String[] args) {
26 | Scanner scanner = new Scanner(System.in);
27 | int t = scanner.nextInt();
28 | for (int i = 0; i < t; i++) {
29 | int n = scanner.nextInt();
30 | int[][] tasks = new int[n][2];
31 | for (int j = 0; j < n; j++) {
32 | int x = scanner.nextInt();
33 | tasks[x - 1][0] = scanner.nextInt();
34 | tasks[x - 1][1] = scanner.nextInt();
35 | }
36 | int[] res = getMaxProfitAndTaskNum(tasks);
37 | System.out.println(res[0] + " " + res[1]);
38 | }
39 | }
40 |
41 | public static int[] getMaxProfitAndTaskNum(int[][] tasks) {
42 | //按照profit进行排序,相同的profit按ddl逆序排序
43 | Arrays.sort(tasks, new Comparator() {
44 | @Override
45 | public int compare(int[] o1, int[] o2) {
46 | if (o1[1] != o2[1]) {
47 | return o2[1] - o1[1];
48 | }
49 | return o2[0] - o1[0];
50 | }
51 | });
52 | int n = tasks.length;
53 | int maxDDL = 0;
54 | for (int i = 0; i < n; i++) {
55 | maxDDL = Math.max(maxDDL, tasks[i][0]);
56 | }
57 | //从最大利润任务开始,将其安排在ddl位置,如果已被占用,则往前放
58 | int[] days = new int[maxDDL + 1];
59 | int cnt = 0;
60 | for (int i = 0; i < n; i++) {
61 | for (int j = tasks[i][0]; j > 0; j--) {
62 | if (days[j] == 0) {
63 | days[j] = tasks[i][1];
64 | cnt++;
65 | break;
66 | }
67 | }
68 | }
69 | int profit = 0;
70 | for (int i = 0; i < days.length; i++) {
71 | profit += days[i];
72 | }
73 | return new int[]{cnt, profit};
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0505.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Comparator;
5 | import java.util.Scanner;
6 |
7 | public class Problem0505 {
8 | /*
9 | 时间分隔
10 | Description
11 | Given arrival and departure times of all trains that reach a railway station.
12 | Your task is to find the minimum number of platforms required for the railway station so that no train waits.
13 | Note: Consider that all the trains arrive on the same day and leave on the same day.
14 | Also, arrival and departure times must not be same for a train.
15 |
16 | Input
17 | The first line of input contains T, the number of test cases.
18 | For each test case, first line will contain an integer N, the number of trains.
19 | Next two lines will consist of N space separated time intervals denoting arrival and departure times respectively.
20 | Note: Time intervals are in the 24-hour format(hhmm), preceding zeros are insignificant. 200 means 2:00.
21 | Consider the example for better understanding of input.
22 | Constraints:1 <= T <= 100,1 <= N <= 1000,1 <= A[i] < D[i] <= 2359
23 |
24 | Output
25 | For each test case, print the minimum number of platforms required for the trains to arrive and depart safely.
26 | */
27 | public static void main(String[] args) {
28 | Scanner scanner = new Scanner(System.in);
29 | int t = scanner.nextInt();
30 | for (int i = 0; i < t; i++) {
31 | int n = scanner.nextInt();
32 | int[] arrive = new int[n];
33 | for (int j = 0; j < n; j++) {
34 | arrive[j] = scanner.nextInt();
35 | }
36 | int[] leave = new int[n];
37 | for (int j = 0; j < n; j++) {
38 | leave[j] = scanner.nextInt();
39 | }
40 | System.out.println(getMinPlatformNum(arrive, leave));
41 | }
42 | }
43 |
44 | public static int getMinPlatformNum(int[] arrive, int[] leave) {
45 | int n = arrive.length;
46 | //将车的到达记为1,离开记为-1
47 | //那么将数组排序后,整个过程中停留的车辆最多数量就是最小所需站台数
48 | int[][] a = new int[2 * n][2];
49 | for (int i = 0; i < n; i++) {
50 | a[i][0] = arrive[i];
51 | a[i][1] = 1;
52 | }
53 | for (int i = 0; i < n; i++) {
54 | a[i + n][0] = leave[i];
55 | a[i + n][1] = -1;
56 | }
57 | Arrays.sort(a, new Comparator() {
58 | @Override
59 | public int compare(int[] o1, int[] o2) {
60 | if (o1[0] != o2[0]) {
61 | return o1[0] - o2[0];
62 | }
63 | return o2[1] - o1[1];
64 | }
65 | });
66 | int res = 0;
67 | int cur = 0;
68 | for (int i = 0; i < 2 * n; i++) {
69 | cur += a[i][1];
70 | res = Math.max(cur, res);
71 | }
72 | return res;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0506.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 |
7 | public class Problem0506 {
8 | /*
9 | 管道网络
10 | Description
11 | Every house in the colony has at most one pipe going into it and at most one pipe going out of it.
12 | Tanks and taps are to be installed in a manner such that every house with one outgoing pipe
13 | but no incoming pipe gets a tank installed on its roof and every house with only an incoming pipe and no outgoing pipe gets a tap.
14 | Find the efficient way for the construction of the network of pipes.
15 |
16 | Input
17 | The first line contains an integer T denoting the number of test cases.
18 | For each test case, the first line contains two integer n & p denoting the number of houses and number of pipes respectively.
19 | Next, p lines contain 3 integer inputs a, b & d, d denoting the diameter of the pipe from the house a to house b.
20 | Constraints:1<=T<=50,1<=n<=20,1<=p<=50,1<=a, b<=20,1<=d<=100
21 |
22 | Output
23 | For each test case, the output is the number of pairs of tanks and taps installed
24 | i.e n followed by n lines that contain three integers: house number of tank, house number of tap and the minimum diameter of pipe between them.
25 | */
26 |
27 | public static void main(String[] args) {
28 | Scanner scanner = new Scanner(System.in);
29 | int t = scanner.nextInt();
30 | for (int i= 0; i < t; i++) {
31 | int n = scanner.nextInt();
32 | int p = scanner.nextInt();
33 | int[] link = new int[n + 1]; //link[i]表示房屋i的管道所连接的房屋
34 | int[] val = new int[n + 1]; //val[i]表示以房屋i为起点的管道长度
35 | int[] degree = new int[n + 1]; //记录入度,目的是获得所有起点管道
36 | for (int j = 0; j < p; j++) {
37 | int x = scanner.nextInt();
38 | int y = scanner.nextInt();
39 | int v = scanner.nextInt();
40 | link[x] = y;
41 | val[x] = v;
42 | degree[y]++;
43 | }
44 | List list = new ArrayList<>();
45 | for (int j = 1; j <= n; j++) {
46 | if (degree[j] == 0) {
47 | list.add(j);
48 | }
49 | }
50 | List res = new ArrayList<>();
51 | for (Integer start: list) {
52 | int end = link[start];
53 | int minVal = val[start];
54 | while (link[end] != 0) {
55 | minVal = Math.min(minVal, val[end]);
56 | end = link[end];
57 | }
58 | res.add(start + " " + end + " " + minVal);
59 | }
60 | System.out.println(res.size());
61 | for (String s: res) {
62 | System.out.println(s);
63 | }
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0507.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0507 {
6 | /*
7 | 调整数组使差最小
8 | Description
9 | 有两个序列 a,b,大小都为 n,序列元素的值任意整数,无序; 要求:通过交换 a,b 中的元素,使[序列 a 元素的和]与[序列 b 元素的和]之间的差最小。
10 |
11 | Input
12 | 输入第一行为用例个数, 每个测试用例输入为两行,分别为两个数组,每个值用空格隔开。
13 |
14 | Output
15 | 输出变化之后的两个数组内元素和的差绝对值。
16 | */
17 | public static void main(String[] args) {
18 | Scanner scanner = new Scanner(System.in);
19 | int t = Integer.parseInt(scanner.nextLine());
20 | for (int i = 0; i < t; i++) {
21 | String[] s1 = scanner.nextLine().split(" ");
22 | String[] s2 = scanner.nextLine().split(" ");
23 | int[] num1 = new int[s1.length];
24 | int[] num2 = new int[s2.length];
25 | //题目说明了长度一样
26 | for (int j = 0; j < s1.length; j++) {
27 | num1[j] = Integer.parseInt(s1[j]);
28 | num2[j] = Integer.parseInt(s2[j]);
29 | }
30 | System.out.println(getRes(num1, num2));
31 | }
32 | }
33 |
34 | //动态规划,参考博客:https://blog.csdn.net/qq_33935895/article/details/103029767
35 | //将num1和num2数组合并,将问题转化为从2n的数组中,取出n个数,是的这些数之和不超过sum/2且最大
36 | public static int getRes(int[] num1, int[] num2) {
37 | int n = num1.length;
38 | int[] arr = new int[2 * n];
39 | int idx = 0, min = num1[0], sum = 0;
40 | for (int i = 0; i < n; i++) {
41 | arr[idx++] = num1[i];
42 | min = Math.min(min, num1[i]);
43 | }
44 | for (int i = 0; i < n; i++) {
45 | arr[idx++] = num2[i];
46 | min = Math.min(min, num2[i]);
47 | }
48 | //只要求两数组之和差最小,所以只要保持两数组和相对大小不变即可,此步目的是将所有数值调整为非负值
49 | for (int i = 0; i < 2 * n; i++) {
50 | arr[i] -= min;
51 | sum += arr[i];
52 | }
53 | int[][] dp = new int[n + 1][sum / 2 + 1];
54 | for (int i = 0; i <= 2 * n; i++) {
55 | for (int j = Math.min(i, n); j >= 1; j--) {
56 | for (int k = sum / 2; k >= arr[i - 1]; k--) {
57 | dp[j][k] = Math.max(dp[j - 1][k - arr[i - 1]] + arr[i - 1], dp[j][k]);
58 | }
59 | }
60 | }
61 | return sum - 2 * dp[n][sum / 2];
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0508.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | public class Problem0508 {
4 | /*
5 | 先升后降
6 | Description
7 | 从一列不重复的数中筛除尽可能少的数使得从左往右看,这些数是从小到大再从大到小的。
8 |
9 | Input
10 | 输入第一行为用例个数, 每个测试用例输入是一个数组,数值通过空格隔开。
11 |
12 | Output
13 | 输出筛选之后的数组,用空格隔开。如果有多种结果,则一行一种结果, 单个输入的所有结果按从小到大排序,排序的key的优先级随index递增而递减
14 | 例如 3 4 7 6; 1 3 7 5; 1 2 7 6; 1 3 7 6 排序成 1 2 7 6;1 3 7 5;1 3 7 6; 3 4 7 6;
15 | */
16 | }
17 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0509.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Problem0509 {
7 | /*
8 | 固定和的元素对
9 | Description
10 | 输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字,统计这样两个数的对数。
11 |
12 | Input
13 | 输入第一行为用例个数, 每个测试用例输入第一行是数组,每一个数用空格隔开;第二行是数字和。
14 |
15 | Output
16 | 输出这样两个数有几对。
17 | */
18 | public static void main(String[] args) {
19 | Scanner scanner = new Scanner(System.in);
20 | int t = Integer.parseInt(scanner.nextLine());
21 | for (int i = 0; i < t; i++) {
22 | String[] numStr = scanner.nextLine().split(" ");
23 | int[] nums = new int[numStr.length];
24 | for (int j = 0; j < nums.length; j++) {
25 | nums[j] = Integer.parseInt(numStr[j]);
26 | }
27 | int target = Integer.parseInt(scanner.nextLine());
28 | System.out.println(getRes(nums, target));
29 | }
30 | }
31 |
32 | public static int getRes(int[] nums, int target) {
33 | Arrays.sort(nums);
34 | int left = 0, right = nums.length - 1;
35 | int res = 0;
36 | while (left < right) {
37 | int sum = nums[left] + nums[right];
38 | if (sum == target) {
39 | res++;
40 | left++;
41 | right--;
42 | } else if (sum > target) {
43 | right--;
44 | } else {
45 | left++;
46 | }
47 | }
48 | return res;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0510.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.ArrayDeque;
4 | import java.util.Deque;
5 | import java.util.Scanner;
6 |
7 | public class Problem0510 {
8 | /*
9 | 数组和窗口
10 | Description
11 | 给定一个整型数组arr和一个大小为w的窗口,窗口从数组最左边滑动到最右边,每次向右滑动一个位置,求出每一次滑动时窗口内最大元素的和。
12 |
13 | Input
14 | 输入第一行为用例个数, 每个测试用例输入的第一行为数组,每一个元素使用空格隔开;第二行为窗口大小。
15 |
16 | Output
17 | 输出每个测试用例结果。
18 | */
19 |
20 | public static void main(String[] args) {
21 | Scanner scanner = new Scanner(System.in);
22 | int t = Integer.parseInt(scanner.nextLine());
23 | for (int i = 0; i < t; i++) {
24 | String[] numStr = scanner.nextLine().split(" ");
25 | int[] nums = new int[numStr.length];
26 | for (int j = 0; j < numStr.length; j++) {
27 | nums[j] = Integer.parseInt(numStr[j]);
28 | }
29 | int k = Integer.parseInt(scanner.nextLine());
30 | System.out.println(getMaxSum(nums, k));
31 | }
32 | }
33 |
34 | //经典单调队列问题
35 | //维护一个单调递减的双端队列
36 | public static int getMaxSum(int[] nums, int k) {
37 | if (nums == null || nums.length == 0) {
38 | return 0;
39 | }
40 | int sum = 0;
41 | Deque queue = new ArrayDeque<>();
42 | for (int i = 0; i < nums.length; i++) {
43 | //先清除队列中不在窗口范围内的下标
44 | while (!queue.isEmpty() && i - queue.peekFirst() >= k) {
45 | queue.pollFirst();
46 | }
47 | //比较新加入的元素与队列末尾元素的大小,保持队列的单调性
48 | while (!queue.isEmpty() && nums[i] > nums[queue.peekLast()]) {
49 | queue.pollLast();
50 | }
51 | queue.offerLast(i);
52 | if (i >= k - 1) {
53 | sum += nums[queue.peekFirst()];
54 | }
55 | }
56 | return sum;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/FifthExercise/Problem0511.java:
--------------------------------------------------------------------------------
1 | package FifthExercise;
2 |
3 | import java.util.ArrayDeque;
4 | import java.util.Deque;
5 | import java.util.Scanner;
6 |
7 | public class Problem0511 {
8 | /*
9 | 子数组的取值范围
10 | Description
11 | 给定数组arr和整数num,求arr的连续子数组中满足:其最大值减去最小值的结果大于num的个数。请实现一个时间复杂度为O(length(arr))的算法。
12 |
13 | Input
14 | 输入第一行为测试用例个数。每一个用例有若干行,第一行为数组,每一个数用空格隔开,第二行为num。
15 |
16 | Output
17 | 输出一个值。
18 | */
19 | public static void main(String[] args) {
20 | Scanner scanner = new Scanner(System.in);
21 | int t = Integer.parseInt(scanner.nextLine());
22 | for (int i = 0; i < t; i++) {
23 | String[] numStr = scanner.nextLine().split(" ");
24 | int[] arr = new int[numStr.length];
25 | for (int j = 0; j < numStr.length; j++) {
26 | arr[j] = Integer.parseInt(numStr[j]);
27 | }
28 | int num = Integer.parseInt(scanner.nextLine());
29 | System.out.println(getSubArrCnt(arr, num));
30 | }
31 | }
32 |
33 | //显然暴力是不满足O(n)的时间复杂度要求的
34 | //用两个双端队列维护滑动窗口内的最大最小值
35 | public static int getSubArrCnt(int[] arr, int num) {
36 | int n = arr.length;
37 | int left = 0, right = 0;
38 | int res = 0;
39 | Deque maxQueue = new ArrayDeque<>();
40 | Deque minQueue = new ArrayDeque<>();
41 | while (left < n) {
42 | while (right < n) {
43 | while (!maxQueue.isEmpty() && arr[right] >= arr[maxQueue.peekLast()]) {
44 | maxQueue.pollLast();
45 | }
46 | while (!minQueue.isEmpty() && arr[right] <= arr[minQueue.peekLast()]) {
47 | minQueue.pollLast();
48 | }
49 | maxQueue.offerLast(right);
50 | minQueue.offerLast(right);
51 | //满足最大-最小>num后跳出循环,将窗口右端固定,移动窗口左端
52 | if (arr[maxQueue.peekFirst()] - arr[minQueue.peekFirst()] > num) {
53 | break;
54 | }
55 | right++;
56 | }
57 | //如果左端即是最大或最小值,则要将队列更新
58 | if (!maxQueue.isEmpty() && maxQueue.peekFirst() == left) {
59 | maxQueue.pollFirst();
60 | }
61 | if (!minQueue.isEmpty() && minQueue.peekFirst() == left) {
62 | minQueue.pollFirst();
63 | }
64 | //事实上,只要找到满足条件的第一个子数组,那么只要左端不动,右端不论往后移动多少这个数组都是满足条件的
65 | //因为这不会使得最大最小值之间的差距变小,只可能变大
66 | res += n - right;
67 | left++;
68 | }
69 | return res;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0105.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0105 {
6 | /*
7 | 宠物屋涂色
8 | Description
9 | Dilpreet wants to paint his dog- Buzo's home that has n boards with different lengths[A1, A2,..., An].
10 | He hired k painters for this work and each painter takes 1 unit time to paint 1 unit of the board.
11 | The problem is to find the minimum time to get this job done under the constraints that any painter will only paint continuous sections of boards,
12 | say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}.
13 |
14 | Input
15 | The first line consists of a single integer T, the number of test cases.
16 | For each test case, the first line contains an integer k denoting the number of painters and integer n denoting the number of boards.
17 | Next line contains n- space separated integers denoting the size of boards.
18 | Constraints:1<=T<=100 1<=k<=30 1<=n<=50 1<=A[i]<=500
19 |
20 | Output
21 | For each test case, the output is an integer displaying the minimum time for painting that house.
22 | */
23 |
24 | //30 50 的测试用例可能会超时,修改
25 | //二分吧,递归会超时,同第十题思路
26 | public static int partition(int[] boards, int n, int k) {
27 | int max = Integer.MIN_VALUE;
28 | int sum = 0;
29 | for (int i = 0; i < n; i++) {
30 | max = Math.max(max, boards[i]);
31 | sum += boards[i];
32 | }
33 | int left = max, right = sum;
34 | while (left < right) {
35 | int mid = (left + right) / 2;
36 | if (getPainters(boards, mid) > k) {
37 | left = mid + 1;
38 | } else {
39 | right = mid;
40 | }
41 | }
42 | return left;
43 | }
44 |
45 | public static int getPainters(int[] boards, int cnt) {
46 | int temp = 0;
47 | int painters = 1;
48 | for (int i = 0; i < boards.length; i++) {
49 | temp += boards[i];
50 | if (temp > cnt) {
51 | temp = boards[i];
52 | painters++;
53 | }
54 | }
55 | return painters;
56 | }
57 |
58 | public static void main(String[] args) {
59 | Scanner scanner = new Scanner(System.in);
60 | while (scanner.hasNext()) {
61 | int T = scanner.nextInt();
62 | for (int i = 0; i < T; i++) {
63 | int k = scanner.nextInt();
64 | int n = scanner.nextInt();
65 | int[] boards = new int[n];
66 | for (int j = 0; j < n; j++) {
67 | boards[j] = scanner.nextInt();
68 | }
69 | System.out.println(partition(boards, n, k));
70 | }
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0106.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0106 {
6 | /*
7 | 序号乘方
8 | Description
9 | There are Infinite People Standing in a row, indexed from 1.
10 | A person having index 'i' has strength of (i*i).You have Strength 'P'.
11 | You need to tell what is the maximum number of People You can Kill With your Strength P.
12 | You can only Kill a person with strength 'X' if P >= 'X' and after killing him, Your Strength decreases by 'X'.
13 |
14 | Input
15 | First line contains an integer 'T' - the number of testcases,
16 | Each of the next 'T' lines contains an integer 'P'- Your Power.
17 | Constraints:1<=T<=10000 1<=P<=1000000000000000
18 |
19 | Output
20 | For each testcase Output The maximum Number of People You can kill.
21 | */
22 |
23 | public static long getMaxPeople(long p) {
24 | long cnt = 0;
25 | while (p >= 0) {
26 | cnt++;
27 | p -= cnt * cnt;
28 | }
29 | return cnt - 1;
30 | }
31 |
32 | public static void main(String[] args) {
33 | Scanner scanner = new Scanner(System.in);
34 | int T = scanner.nextInt();
35 | for (int i = 0; i < T; i++) {
36 | long P = scanner.nextLong();
37 | System.out.println(getMaxPeople(P));
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0107.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Problem0107 {
7 | /*
8 | Description
9 | Given a Complete Binary tree, print the level order traversal in sorted order.
10 |
11 | Input
12 | The first line of the input contains integer T denoting the number of test cases. For each test case,
13 | the first line takes an integer n denoting the size of array i.e number of nodes followed by n-space separated integers denoting the nodes of the tree in level order fashion.(1<=T<=100;1<=n<=10^5)
14 |
15 | Output
16 | For each test case, the output is the level order sorted tree.
17 | ( Note: For every level, we only print distinct elements.)
18 | */
19 |
20 | //实现完全二叉树的层级遍历
21 | public static void levelOrder(int[] nums) {
22 | //对于完全二叉树,只有每层节点填满后才会填下一层节点
23 | //即除去最后一层外,每层节点个数均为2^h,其中根节点h=0,余下的节点全部填进最后一层
24 | int n = nums.length, nodeCnt = 1, index = 0;
25 | while (n - nodeCnt >= 0) {
26 | n -= nodeCnt;
27 | int[] nodes = new int[nodeCnt];
28 | for (int i = 0; i < nodeCnt; i++) {
29 | nodes[i] = nums[index++];
30 | }
31 | print(nodes);
32 | nodeCnt <<= 1;
33 | }
34 | if (n > 0) {
35 | int[] nodes = new int[n];
36 | for (int i = 0; i < n; i++) {
37 | nodes[i] = nums[index++];
38 | }
39 | print(nodes);
40 | }
41 | }
42 |
43 | //对每层节点进行排序去重输出
44 | public static void print(int[] nodes) {
45 | Arrays.sort(nodes);
46 | int i = 0;
47 | String output = "";
48 | while (i < nodes.length) {
49 | output += nodes[i] + " ";
50 | int tem = nodes[i];
51 | while (i < nodes.length && nodes[i] == tem) i++; //去重
52 | }
53 | output = output.substring(0, output.length() - 1);
54 | System.out.println(output);
55 | }
56 |
57 | public static void main(String[] args) {
58 | Scanner scanner = new Scanner(System.in);
59 | while (scanner.hasNext()) {
60 | int T = scanner.nextInt();
61 | for (int i = 0; i < T; i++) {
62 | int n = scanner.nextInt();
63 | int[] nums = new int[n];
64 | for (int j = 0; j < n; j++) {
65 | nums[j] = scanner.nextInt();
66 | }
67 | levelOrder(nums);
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0108.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0108 {
6 | /*
7 | 数学公式
8 | Description
9 | Implement pow(A, B) % C.In other words, given A, B and C, find (A^B)%C
10 |
11 | Input
12 | The first line of input consists number of the test cases.
13 | The following T lines consist of 3 numbers each separated by a space and in the following order:A B C
14 | 'A' being the base number, 'B' the exponent (power to the base number) and 'C' the modular.
15 | Constraints:1 ≤ T ≤ 70,1 ≤ A ≤ 10^5,1 ≤ B ≤ 10^5,1 ≤ C ≤ 10^5
16 |
17 | Output
18 | In each separate line print the modular exponent of the given numbers in the test case.
19 | */
20 |
21 | // 由于 (a * b) % c = ((a % c) * (b % c)) % c
22 | // 那么 pow(a, b) % c ==> (a * a * ... * a) % c ==> ((a % c)(a % c)...(a % c)) % c
23 | // \______________/ \________________________/
24 | // b 个 a b 个 a % c
25 | // 快速幂
26 | public static int fastPow(int a, int b, int c) {
27 | int res = 1;
28 | while (b > 0) {
29 | if (b % 2 == 1) { //奇数情况下,先乘一个a
30 | res = ((res % c) * (a % c)) % c;
31 | }
32 | a = ((a % c) * (a % c)) % c; //偶数情况,直接平方
33 | b /= 2;
34 | }
35 | return res;
36 | }
37 |
38 | public static void main(String[] args) {
39 | Scanner scanner = new Scanner(System.in);
40 | int T = scanner.nextInt();
41 | for (int i = 0; i < T; i++) {
42 | int a = scanner.nextInt();
43 | int b = scanner.nextInt();
44 | int c = scanner.nextInt();
45 | System.out.println(fastPow(a, b, c));
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0109.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.*;
4 |
5 | public class Problem0109 {
6 | /*
7 | 点的凸包
8 | Description
9 | Convex Hull of a set of points, in 2D plane,
10 | is a convex polygon with minimum area such that each point lies either on the boundary of polygon or inside it.
11 | Now given a set of points the task is to find the convex hull of points.
12 |
13 | Input
14 | The first line of input contains an integer T denoting the no of test cases.
15 | Then T test cases follow. Each test case contains an integer N denoting the no of points.
16 | Then in the next line are N*2 space separated values denoting the points ie x and y.
17 | Constraints:1<=T<=100,1<=N<=100,1<=x,y<=1000
18 |
19 | Output
20 | For each test case in a new line print the points x and y of the convex hull separated by a space in sorted order
21 | (increasing by x) where every pair is separated from the other by a ','. If no convex hull is possible print -1.
22 | */
23 | public static List getConvexHull(Point[] points) {
24 | List res = new ArrayList<>();
25 | List indexes = new ArrayList<>();
26 | for (int i = 0; i < points.length; i++) {
27 | indexes.add(i);
28 | }
29 | List upper = hull(points, indexes, true);
30 | List lower = hull(points, indexes, false);
31 | //合并结果
32 | //若所有点处于同一条线上,则上下包返回的均为所有点索引列表
33 | if (upper.size() == lower.size() && upper.containsAll(lower)) {
34 | return res;
35 | }
36 | List resIndex = new ArrayList<>();
37 | resIndex.add(indexes.get(0));
38 | resIndex.add(indexes.get(indexes.size() - 1));
39 | resIndex.addAll(upper);
40 | resIndex.addAll(lower);
41 | Collections.sort(resIndex);
42 | for (int i = 0; i< resIndex.size(); i++) {
43 | res.add(points[resIndex.get(i)]);
44 | }
45 | return res;
46 | }
47 |
48 | /**
49 | *
50 | * @param points 所有点的数组
51 | * @param indexes 当前需要处理的点的索引列表
52 | * @param dir 上包还是下包
53 | * @return 返回对应的顶点列表
54 | */
55 | public static List hull(Point[] points, List indexes, boolean dir) {
56 | int pDis = 0; //上包记录最大值,下包记录最小值
57 | int pIndex = -1;
58 | int size = indexes.size();
59 | Point p1 = points[indexes.get(0)];
60 | Point p2 = points[indexes.get(size - 1)];
61 | List upOrLowList = new ArrayList<>();//记录上包或下包的点索引
62 | List onList = new ArrayList<>();//记录再p1p2线上的点
63 | for (int i = 1; i < size - 1; i++) {
64 | int dis = getDistance(p1, p2, points[indexes.get(i)]);
65 | if (dis == 0) {
66 | onList.add(indexes.get(i));
67 | }
68 | if (dir) { //上包
69 | if (dis > 0) {
70 | upOrLowList.add(indexes.get(i));
71 | if (dis > pDis) {
72 | pDis = dis;
73 | pIndex = indexes.get(i);
74 | }
75 | }
76 | } else {
77 | if (dis < 0) {
78 | upOrLowList.add(indexes.get(i));
79 | if (dis < pDis) {
80 | pDis = dis;
81 | pIndex = indexes.get(i);
82 | }
83 | }
84 | }
85 | }
86 | if (pIndex == -1) {// 未找到上包或下包点,返回线上的点集
87 | int start = indexes.get(0);
88 | int end = indexes.get(size - 1);
89 | List res = new ArrayList<>();
90 | for (int i = 0; i < onList.size(); i++) {
91 | int index = onList.get(i);
92 | if (index != start && index != end) {
93 | res.add(index);
94 | }
95 | }
96 | return res;
97 | }
98 |
99 | //对上包或下包进行划分,分成p1->pmax, pmax->p2两部分
100 | List divide1 = new ArrayList<>();
101 | List divide2 = new ArrayList<>();
102 | divide1.add(indexes.get(0));
103 | divide2.add(pIndex);
104 | divide1.addAll(upOrLowList);
105 | divide2.addAll(upOrLowList);
106 | divide1.add(pIndex);
107 | divide2.add(indexes.get(size - 1));
108 |
109 | //合并结果
110 | List res1 = hull(points, divide1, dir);
111 | List res2 = hull(points, divide2, dir);
112 | res1.add(pIndex);
113 | res1.addAll(res2);
114 | return res1;
115 | }
116 |
117 | //计算行列式
118 | // |x1 y1 1| >0 p3在p1p2左侧
119 | // |x2 y2 1| = x1y2 + x3y1 + x2y3 - x3y2 - x2y1 - x1y3 =0 p3在p1p2上
120 | // |x3 y3 1| <0 p3在p1p2右侧
121 | //同时该行列式绝对值的1/2等于三角形p1p2p3的面积
122 | public static int getDistance(Point p1, Point p2, Point p3) {
123 | return p1.getX() * p2.getY() + p3.getX() * p1.getY() + p2.getX() * p3.getY() - p3.getX() * p2.getY() - p2.getX() * p1.getY() - p1.getX() * p3.getY();
124 | }
125 |
126 | public static void main(String[] args) {
127 | Scanner scanner = new Scanner(System.in);
128 | int T = scanner.nextInt();
129 | for (int i = 0; i < T; i++) {
130 | int N = scanner.nextInt();
131 | Point[] points = new Point[N];
132 | for (int j = 0; j < N; j++) {
133 | int x = scanner.nextInt();
134 | int y = scanner.nextInt();
135 | points[j] = new Point(String.valueOf(x), String.valueOf(y));
136 | }
137 | if (N == 0) {
138 | System.out.println(-1);
139 | continue;
140 | }
141 | // String[] points_str = scanner.nextLine().split(" ");
142 | // if (N == 0) {
143 | // System.out.println(-1);
144 | // continue;
145 | // }
146 | // Point[] points = new Point[N];
147 | // for (int j = 0; j < N; j++) {
148 | // points[j] = new Point(points_str[2 * j], points_str[2 * j + 1]);
149 | // }
150 | Arrays.sort(points);
151 | List convexHull = getConvexHull(points);
152 | if (convexHull.size() <= 2) {
153 | System.out.println(-1);
154 | } else {
155 | for (int j = 0; j < convexHull.size() - 1; j++) {
156 | System.out.print(convexHull.get(j) + ", ");
157 | }
158 | System.out.println(convexHull.get(convexHull.size() - 1));
159 | }
160 | }
161 |
162 |
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0110.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0110 {
6 | /*
7 | 书本分发
8 | Description
9 | You are given N number of books. Every ith book has Pi number of pages. You have to allocate books to M number of students.
10 | There can be many ways or permutations to do so.
11 | In each permutation one of the M students will be allocated the maximum number of pages. Out of all these permutations,
12 | the task is to find that particular permutation in which the maximum number of pages allocated to a student is minimum of those in all the other permutations,
13 | and print this minimum value. Each book will be allocated to exactly one student.
14 | Each student has to be allocated at least one book.
15 | 每一个学生只能分配连续出现的书本。
16 |
17 | Input
18 | The first line contains 'T' denoting the number of testcases.
19 | Then follows description of T testcases:
20 | Each case begins with a single positive integer N denoting the number of books.
21 | The second line contains N space separated positive integers denoting the pages of each book.
22 | And the third line contains another integer M, denoting the number of students
23 | Constraints:1<= T <=70,1<= N <=50,1<= A [ i ] <=250,1<= M <=50,
24 | Note: Return -1 if a valid assignment is not possible, and allotment should be in contiguous order (see explanation for better understanding)
25 |
26 | Output
27 | For each test case, output a single line containing minimum number of pages each student has to read for corresponding test case.
28 | */
29 |
30 | /**
31 | * 本质上还是1-5这道题,但加了限制条件,m个学生每人至少分到一本书
32 | * 即将n个元素的数组nums划分为m个连续子数组,且这m个子数组都至少包含一个元素,要求求出所有划分方法中,m个子数组和最大值最小的情况
33 | * 可以理解为将数组中的连续元素分到m个桶中,要求桶的容量最小
34 | * 参考博客: https://www.cnblogs.com/ygh1229/p/10637504.html
35 | * @param nums 需要进行划分的数组
36 | * @param n 数组元素个数
37 | * @param m 划分的份数
38 | * @return 返回m个子数组和最大值最小的情况
39 | */
40 | public static int getRes(int[] nums, int n, int m) {
41 | if (n < m) return -1; //无法为每位同学分发一本书时,返回-1
42 | int max = Integer.MIN_VALUE;
43 | int sum = 0;
44 | for (int i = 0; i < n; i++) {
45 | max = Math.max(max, nums[i]);
46 | sum += nums[i];
47 | }
48 | //显然,极端情况下,桶的容量最小为数组中最大元素值,最大为数组所有元素之和
49 | int left = max, right = sum;
50 | //二分查找满足条件的最小桶容量
51 | while (left < right) {
52 | int mid = (left + right) / 2;
53 | if (getBucketsNum(nums, mid) > m) { //桶的容量太小了,扩大容量
54 | left = mid + 1;
55 | } else { //桶的容量大了,缩小
56 | right = mid;
57 | }
58 | }
59 | return left;
60 | }
61 |
62 | /**
63 | *
64 | * @param nums 需要划分的数组
65 | * @param capacity 桶的容量
66 | * @return 返回需要的桶数量
67 | */
68 | public static int getBucketsNum(int[] nums, int capacity) {
69 | int tem = 0;
70 | int buckets = 1;
71 | for (int i = 0; i < nums.length; i++) {
72 | tem += nums[i];
73 | if (tem > capacity) { //超出桶容量,换个新桶
74 | tem = nums[i];
75 | buckets++;
76 | }
77 | }
78 | return buckets;
79 | }
80 |
81 | public static void main(String[] args) {
82 | Scanner scanner = new Scanner(System.in);
83 | int T = scanner.nextInt();
84 | for (int i = 0; i < T; i++) {
85 | int N = scanner.nextInt();
86 | int[] pages = new int[N];
87 | for (int j = 0; j < N; j++) {
88 | pages[j] = scanner.nextInt();
89 | }
90 | int M = scanner.nextInt();
91 | System.out.println(getRes(pages, N, M));
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0112.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Problem0112 {
7 | /*
8 | 客流量
9 | Description
10 | Consider a big party where a log register for guest’s entry and exit times is maintained.
11 | Find the time at which there are maximum guests in the party. Note that entries in register are not in any order.
12 |
13 | Input
14 | The first line of input contains an integer T denoting the number of test cases.
15 | Then T test cases follow. Each test case contains an integer n denoting the size of the entry and exit array.
16 | Then the next two line contains the entry and exit array respectively.(1<=T<=10^5;1<=N<=10^5;1<=entry[i],exit[i]<=10^5)
17 |
18 | Output
19 | Print the maximum no of guests and the time at which there are maximum guests in the party.
20 | */
21 |
22 | public static void getMaxNum(int[] entry, int[] exit) {
23 | //entry数组并不指定进入顺序,因此对数组进行排序,便于按时间顺序进行处理
24 | Arrays.sort(entry);
25 | Arrays.sort(exit);
26 | int n = entry.length;
27 | int i = 0, j = 0;
28 | int maxNum = 0, maxTime = 0;
29 | int curNum = 0; //记录当前时刻的派对人数
30 | while (i < n && j < n) {
31 | if (entry[i] < exit[j]) { //该时刻进入一人,当前人数增加
32 | curNum++;
33 | if (curNum > maxNum) {
34 | maxNum = curNum;
35 | maxTime = entry[i];
36 | }
37 | i++;
38 | } else if (entry[i] > exit[j]) { //该时刻离开一人,当前人数减少
39 | curNum--;
40 | j++;
41 | } else { //该时刻同时有人进出,先处理进入
42 | while (i < n - 1 && entry[i] == entry[i + 1]) { //将所有该时刻进入人员记录
43 | i++;
44 | curNum++;
45 | }
46 | curNum++;
47 | if (curNum > maxNum) {
48 | maxNum = curNum;
49 | maxTime = entry[i];
50 | }
51 | i++;
52 | j++;
53 | curNum--;
54 | }
55 | }
56 | System.out.println(maxNum + " " + maxTime);
57 | }
58 |
59 |
60 | public static void main(String[] args) {
61 | Scanner scanner = new Scanner(System.in);
62 | while (scanner.hasNext()) {
63 | int T = scanner.nextInt();
64 | for (int i = 0; i < T; i++) {
65 | int n = scanner.nextInt();
66 | int[] entry = new int[n];
67 | int[] exit = new int[n];
68 | for (int j = 0; j < n; j++) {
69 | entry[j] = scanner.nextInt();
70 | }
71 | for (int j = 0; j < n; j++) {
72 | exit[j] = scanner.nextInt();
73 | }
74 | getMaxNum(entry, exit);
75 | }
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0113.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.List;
6 | import java.util.Scanner;
7 |
8 | public class Problem0113 {
9 | /*
10 | 和为定值的子数组
11 | Description
12 | Given an array A of size N, find all combination of four elements in the array whose sum is equal to a given value K.
13 | For example, if the given array is {10, 2, 3, 4, 5, 9, 7, 8} and K = 23, one of the quadruple is “3 5 7 8” (3 + 5 + 7 + 8 = 23).
14 |
15 | Input
16 | The first line of input contains an integer T denoting the no of test cases.
17 | Then T test cases follow. Each test case contains two lines. The first line of input contains two integers N and K.
18 | Then in the next line are N space separated values of the array.(1<=T<=100;1<=N<=100;-1000<=K<=1000;-100<=A[]<=100)
19 |
20 | Output
21 | For each test case in a new line print all the quadruples present in the array separated by space which sums up to value of K.
22 | Each quadruple is unique which are separated by a delimeter "$" and are in increasing order.
23 | */
24 |
25 | //四数之和,求出所有和为target的包含四个元素的子数组
26 | public static List> fourSum(int[] nums, int target) {
27 | List> res = new ArrayList<>();
28 | if (nums == null || nums.length < 4) return res; //如果数组长度不足4,则返回空结果
29 | Arrays.sort(nums);
30 | int len = nums.length;
31 | for (int i = 0; i < len - 3; i++) {
32 | //对于重复值,不需要再次计算,最终结果不重复
33 | if (i > 0 && nums[i] == nums[i - 1]) continue;
34 | //数组升序排序后,当连续四个元素和超过target时,后续不再可能出现和为target的子数组,跳出循环
35 | if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
36 | //如果当前元素与最大的三个元素相加结果小于target,也不可能出现包含该元素的最终结果,跳过此次计算
37 | if (nums[i] + nums[len - 3] + nums[len - 2] + nums[len - 1] < target) continue;
38 | //将四数之和问题转化为三数之和
39 | for (int j = i + 1; j < len - 2; j++) {
40 | //与四数之和问题相同的处理方式
41 | if (j > i + 1 && nums[j] == nums[j - 1]) continue;
42 | if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
43 | if (nums[i] + nums[j] + nums[len - 2] + nums[len - 1] < target) continue;
44 | //将三数之和问题转化为两数之和,用二分法解决
45 | int left = j + 1, right = len - 1;
46 | while (left < right) {
47 | int sum = nums[i] + nums[j] + nums[left] + nums[right];
48 | //和为target,则将四数加入结果集中
49 | if (sum == target) {
50 | res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
51 | //跳过重复值,继续寻找下一个解
52 | while (left < right && nums[left] == nums[left + 1]) left++;
53 | left++;
54 | while (left < right && nums[right] == nums[right - 1]) right--;
55 | right--;
56 | } else if (sum > target) { //和大于target,则将right指针左移
57 | right--;
58 | } else { //和小于target,则将left指针右移
59 | left++;
60 | }
61 | }
62 | }
63 | }
64 | return res;
65 | }
66 |
67 |
68 | public static void main(String[] args) {
69 | Scanner scanner = new Scanner(System.in);
70 | while (scanner.hasNext()) {
71 | int T = scanner.nextInt();
72 | for (int i = 0; i < T; i++) {
73 | int N = scanner.nextInt();
74 | int target = scanner.nextInt();
75 | int[] nums = new int[N];
76 | for (int j = 0; j < N; j++) {
77 | nums[j] = scanner.nextInt();
78 | }
79 | List> res = fourSum(nums, target);
80 | for (List subArr : res) {
81 | for (Integer element : subArr) {
82 | System.out.print(element + " ");
83 | }
84 | System.out.print("$");
85 | }
86 | System.out.println();
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0114.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0114 {
6 | /*
7 | 合并数组
8 | Description
9 | Given K sorted arrays arranged in a form of a matrix, your task is to merge them.
10 | You need to complete mergeKArrays function which takes 2 arguments,
11 | an arr[k][k] 2D Matrix containing k sorted arrays and an integer k denoting the no. of sorted arrays.
12 | The function should return a pointer to the merged sorted arrays.
13 | There are multiple test cases. For each test case, this method will be called individually.
14 |
15 | Input
16 | The first line of input will denote the no of Test cases then T test cases will follow .
17 | Each test cases will contain an integer N denoting the no of sorted arrays.
18 | Then in the next line contains all the elements of the array separated by space.(1<=T<=50;1<=N<=10)
19 |
20 | Output
21 | The output will be the sorted merged array.
22 | */
23 |
24 | //分治法解决,将n个数组不断划分,两两合并
25 | public static int[] merge(int[][] arrays, int left, int right) {
26 | if (left == right) return arrays[left];
27 | int mid = (left + right) / 2;
28 | int[] leftArray = merge(arrays, left, mid);
29 | int[] rightArray = merge(arrays, mid + 1, right);
30 | return mergeTwoArray(leftArray, rightArray);
31 | }
32 |
33 | //对两个排序数组进行合并
34 | public static int[] mergeTwoArray(int[] leftArray, int[] rightArray) {
35 | //若其中一个数组为空,则直接返回另一个数组
36 | if (leftArray == null) return rightArray;
37 | if (rightArray == null) return leftArray;
38 | //若两个数组均非空,则进行合并
39 | int leftLen = leftArray.length, rightLen = rightArray.length;
40 | int[] res = new int[leftLen + rightLen];
41 | int i = 0, j = 0, index = 0;
42 | while (i < leftLen && j < rightLen) {
43 | if (leftArray[i] < rightArray[j]) {
44 | res[index++] = leftArray[i++];
45 | } else if (leftArray[i] > rightArray[j]) {
46 | res[index++] = rightArray[j++];
47 | } else {
48 | res[index++] = leftArray[i++];
49 | res[index++] = rightArray[j++];
50 | }
51 | }
52 | //将未遍历的剩余数组进行遍历
53 | while (i < leftLen) {
54 | res[index++] = leftArray[i++];
55 | }
56 | while (j < rightLen) {
57 | res[index++] = rightArray[j++];
58 | }
59 | return res;
60 | }
61 |
62 | public static void main(String[] args) {
63 | Scanner scanner = new Scanner(System.in);
64 | while (scanner.hasNext()) {
65 | int T = scanner.nextInt();
66 | for (int i= 0; i < T; i++) {
67 | int n = scanner.nextInt();
68 | int[][] arrays = new int[n][n];
69 | int index = 0;
70 | while (index < n * n) {
71 | arrays[index / n][index % n] = scanner.nextInt();
72 | index++;
73 | }
74 | int[] res = merge(arrays, 0, n - 1);
75 | for (int j = 0; j < res.length - 1; j++) {
76 | System.out.print(res[j] + " ");
77 | }
78 | System.out.println(res[res.length - 1]);
79 | }
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0115.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 | import java.util.Scanner;
7 |
8 | public class Problem0115 {
9 | /*
10 | 最小交换次数
11 | Description
12 | Given an array of N distinct elementsA[ ], find the minimum number of swaps required to sort the array.
13 | Your are required to complete the function which returns an integer denoting the minimum number of swaps, required to sort the array.
14 |
15 | Input
16 | The first line of input contains an integer T denoting the no of test cases . Then T test cases follow .
17 | Each test case contains an integer N denoting the no of element of the array A[ ].
18 | In the next line are N space separated values of the array A[ ] .(1<=T<=100;1<=N<=100;1<=A[] <=1000)
19 |
20 | Output
21 | For each test case in a new line output will be an integer denoting minimum umber of swaps that are required to sort the array.
22 | */
23 |
24 | //获取使得数组升序的最小交换次数
25 | public static int getMinSwapCnt(int[] nums) {
26 | int[] temp = Arrays.copyOfRange(nums, 0, nums.length);
27 | //排序并记录每个数的正确位置
28 | Arrays.sort(temp);
29 | Map indexMap = new HashMap<>();
30 | for (int i = 0; i < temp.length; i++) {
31 | indexMap.put(temp[i], i);
32 | }
33 | //记录该位置是否已访问
34 | boolean[] isVisited = new boolean[temp.length];
35 | //寻找循环节个数,即循环节内的数依次交换,即可全部回到正确位置
36 | int loops = 0;
37 | for (int i = 0; i < nums.length; i++) {
38 | if (!isVisited[i]) {
39 | int j = i;
40 | while (!isVisited[j]) {
41 | isVisited[j] = true;
42 | j = indexMap.get(nums[j]);
43 | }
44 | loops++;
45 | }
46 | }
47 | //对每一个循环节来说,交换次数为循环节内元素个数减一
48 | //举例,若nums存在三个循环节,nums元素个数为n,各循环节元素个数为x, y, z
49 | //则交换次数应为 (x-1)+(y-1)+(z-1)=x+y+z-3=n-3
50 | //其他情况以此类推,最小交换次数为数组元素个数-循环节个数
51 | return nums.length - loops;
52 | }
53 |
54 | public static void main(String[] args) {
55 | Scanner scanner = new Scanner(System.in);
56 | int T = scanner.nextInt();
57 | for (int i = 0; i < T; i++) {
58 | int N = scanner.nextInt();
59 | int[] nums = new int[N];
60 | for (int j = 0; j < N; j++) {
61 | nums[j] = scanner.nextInt();
62 | }
63 | System.out.println(getMinSwapCnt(nums));
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0116.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0116 {
6 | /*
7 | Description
8 | 实现插入排序。
9 |
10 | Input
11 | 输入第一行为用例个数, 每个测试用例输入的每一行代表一个数组,其中的值用空格隔开,第一个值表示数组的长度。
12 |
13 | Output
14 | 输出排序的数组,用空格隔开,末尾不要空格。
15 | */
16 |
17 | //插入排序
18 | public static void insertSort(int[] nums) {
19 | if (nums == null || nums.length <= 1) return;
20 | int len = nums.length;
21 | for (int i = 1; i < len; i++) { //认定前 i-1 个数已完成排序,遍历找到第 i 个数的位置进行插入
22 | int temp = nums[i];
23 | int j;
24 | for (j = i - 1; j >= 0; j--) {
25 | if (nums[j] > temp) {
26 | nums[j + 1] = nums[j]; //当前位置比第 i 个数大,将其后移
27 | } else {
28 | //找到第 i 个数的位置,进行插入
29 | break;
30 | }
31 | }
32 | nums[j + 1] = temp;
33 | }
34 | }
35 |
36 | public static void main(String[] args) {
37 | Scanner scanner = new Scanner(System.in);
38 | int T = scanner.nextInt();
39 | for (int i = 0; i < T; i++) {
40 | int N = scanner.nextInt();
41 | int[] nums = new int[N];
42 | for (int j = 0; j < N; j++) {
43 | nums[j] = scanner.nextInt();
44 | }
45 | insertSort(nums);
46 | for (int j = 0; j < N - 1; j++) {
47 | System.out.print(nums[j] + " ");
48 | }
49 | System.out.println(nums[N - 1]);
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0117.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0117 {
6 | /*
7 | Description
8 | 实现冒泡排序。
9 |
10 | Input
11 | 输入的每一行表示一个元素为正整数的数组,所有值用空格隔开,第一个值为数值长度,其余为数组元素值。
12 |
13 | Output
14 | 输出的每一行为排序结果,用空格隔开,末尾不要空格。
15 | */
16 |
17 | //冒泡排序
18 | public static void bubbleSort(int[] nums) {
19 | int temp = 0;
20 | for (int i = 0; i < nums.length - 1; i++) {
21 | for (int j = 0; j < nums.length - i - 1; j++) {
22 | if (nums[j] > nums[j + 1]) {//相邻两数比较大小,若前者大,交换位置
23 | temp = nums[j];
24 | nums[j] = nums[j + 1];
25 | nums[j + 1] = temp;
26 | }
27 | }
28 | }
29 | }
30 |
31 | public static void main(String[] args) {
32 | Scanner scanner = new Scanner(System.in);
33 | while (scanner.hasNext()) {
34 | int N = scanner.nextInt();
35 | int[] nums = new int[N];
36 | for (int i = 0; i < N; i++) {
37 | nums[i] = scanner.nextInt();
38 | }
39 | bubbleSort(nums);
40 | for (int i = 0; i < N - 1; i++) {
41 | System.out.print(nums[i] + " ");
42 | }
43 | System.out.println(nums[N - 1]);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0118.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0118 {
6 | /*
7 | Description
8 | 实现计数排序,通过多次遍历数组,统计比每一个元素小的其它元素个数,根据该统计量对数据进行排序。
9 |
10 | Input
11 | 输入的每一行表示一个元素为正整数的数组,所有值用空格隔开,第一个值为数值长度,其余为数组元素值。
12 |
13 | Output
14 | 输出的每一行为排序结果,用空格隔开,末尾不要空格。
15 | */
16 |
17 | //计数排序
18 | public static void countSort(int[] nums) {
19 | int max = nums[0], min = nums[0];
20 | for (int i = 1; i < nums.length; i++) { //求数组最大最小值
21 | max = Math.max(max, nums[i]);
22 | min = Math.min(min, nums[i]);
23 | }
24 | int[] cnt = new int[max - min + 1]; //数组取值范围为 min ~ max
25 | for (int i = 0; i < nums.length; i++) {
26 | cnt[nums[i] - min]++; //求得每个取值的个数
27 | }
28 | int index = 0;
29 | for (int i = 0; i < cnt.length; i++) {
30 | for (int j = 0; j < cnt[i]; j++) {
31 | nums[index++] = min + i;
32 | }
33 | }
34 | }
35 |
36 | public static void main(String[] args) {
37 | Scanner scanner = new Scanner(System.in);
38 | while (scanner.hasNext()) {
39 | int N = scanner.nextInt();
40 | int[] nums = new int[N];
41 | for (int i = 0; i < N; i++) {
42 | nums[i] = scanner.nextInt();
43 | }
44 | countSort(nums);
45 | for (int i = 0; i < N - 1; i++) {
46 | System.out.print(nums[i] + " ");
47 | }
48 | System.out.println(nums[N - 1]);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0119.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 | import java.util.Stack;
5 |
6 | public class Problem0119 {
7 | /*
8 | 非递归快排
9 | Description
10 | 快速排序的核心思想是使用元素的值对数组进行划分。实现其非递归方案。
11 |
12 | Input
13 | 输入的每一行表示一个元素为正整数的数组,所有值用空格隔开,第一个值为数值长度,其余为数组元素值。
14 |
15 | Output
16 | 输出的每一行为排序结果,用空格隔开,末尾不要空格。
17 | */
18 |
19 | //非递归快排,使用栈
20 | public static void quickSort(int[] nums) {
21 | Stack stack = new Stack<>();
22 | stack.push(0);
23 | stack.push(nums.length - 1);
24 | while (!stack.isEmpty()) {
25 | int right = stack.pop(); //数组右端位置后入先出
26 | int left = stack.pop();
27 | if (left >= right) continue;
28 | int index = partition(nums, left, right);
29 | stack.push(left);
30 | stack.push(index - 1);
31 | stack.push(index + 1);
32 | stack.push(right);
33 | }
34 | }
35 |
36 | //模拟一趟快排,并返回基准元素的最终位置,以将数组进行划分
37 | public static int partition(int[] nums, int left, int right) {
38 | //以开始位置元素为基准,将比其小的置于左边,大的置于右边
39 | int base = nums[left];
40 | while (left < right) {
41 | while (left < right && nums[right] >= base) {
42 | right--;
43 | }
44 | nums[left] = nums[right];
45 | while (left < right && nums[left] <= base) {
46 | left++;
47 | }
48 | nums[right] = nums[left];
49 | }
50 | //此时,left == right,这个坑位就是之前的基准元素
51 | nums[left] = base;
52 | return left;
53 | }
54 |
55 | public static void main(String[] args) {
56 | Scanner scanner = new Scanner(System.in);
57 | while (scanner.hasNext()) {
58 | int N = scanner.nextInt();
59 | int[] nums = new int[N];
60 | for (int i = 0; i < N; i++) {
61 | nums[i] = scanner.nextInt();
62 | }
63 | quickSort(nums);
64 | for (int i = 0; i < N - 1; i++) {
65 | System.out.print(nums[i] + " ");
66 | }
67 | System.out.println(nums[N - 1]);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0120.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0120 {
6 | /*
7 | 非递归合并排序
8 | Description
9 | 合并(归并)排序的核心思想是:每次从中间位置将数组分组再分别排序。请实现其非递归方案。
10 |
11 | Input
12 | 输入的每一行表示一个元素为正整数的数组,所有值用空格隔开,第一个值为数值长度,其余为数组元素值。
13 |
14 | Output
15 | 输出的每一行为排序结果,用空格隔开,末尾不要空格。
16 | */
17 |
18 | //将两个子序列进行合并
19 | public static void merge(int[] nums, int left, int mid, int right) {
20 | int[] temp = new int[right - left + 1];
21 | int i = left, j = mid + 1, k = 0;
22 | while (i <= mid && j <= right) {
23 | if (nums[i] > nums[j]) {
24 | temp[k++] = nums[j++];
25 | } else {
26 | temp[k++] = nums[i++];
27 | }
28 | }
29 | while (i <= mid) {
30 | temp[k++] = nums[i++];
31 | }
32 | while (j <= right) {
33 | temp[k++] = nums[j++];
34 | }
35 | for (int l = left; l < right + 1; l++) {
36 | nums[l] = temp[l - left];
37 | }
38 | }
39 |
40 | //非递归归并排序
41 | public static void mergeSort(int[] nums) {
42 | int k = 1;
43 | int len = nums.length;
44 | while (k < len) {
45 | int i = 0;
46 | while (i <= len - 2 * k) {
47 | merge(nums, i, i + k - 1, i + 2 * k - 1); //两两合并
48 | i += 2 * k;
49 | }
50 | if (i <= len - k) {
51 | merge(nums, i, i + k - 1, len - 1); //余下不足2k的长度也需要进行合并处理
52 | }
53 | k <<= 1; //每次处理的子序列长度成倍扩展
54 | }
55 | }
56 |
57 | public static void main(String[] args) {
58 | Scanner scanner = new Scanner(System.in);
59 | while (scanner.hasNext()) {
60 | int N = scanner.nextInt();
61 | int[] nums = new int[N];
62 | for (int i = 0; i < N; i++) {
63 | nums[i] = scanner.nextInt();
64 | }
65 | mergeSort(nums);
66 | for (int i = 0; i < N - 1; i++) {
67 | System.out.print(nums[i] + " ");
68 | }
69 | System.out.println(nums[N - 1]);
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0121.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.Scanner;
5 |
6 | public class Problem0121 {
7 |
8 | /*
9 | Description
10 | 有一个由N个实数构成的数组,如果一对元素A[i]和A[j]是倒序的,即iA[j]则称它们是一个倒置,设计一个计算该数组中所有倒置数量的算法。
11 | 要求算法复杂度为O(nlogn)
12 |
13 | Input
14 | 输入有多行,第一行整数T表示为测试用例个数,后面是T个测试用例,每一个用例包括两行,第一行的一个整数是元素个数,第二行为用空格隔开的数组值。
15 |
16 | Output
17 | 输出每一个用例的倒置个数,一行表示一个用例。
18 | */
19 |
20 | public static int sort(int[] nums) {
21 | if (nums.length <= 1) return 0;
22 | int cnt = 0;
23 |
24 | //划分左右数组
25 | int[] left = Arrays.copyOfRange(nums, 0, nums.length / 2);
26 | int[] right = Arrays.copyOfRange(nums, nums.length / 2, nums.length);
27 | int leftCnt = sort(left);
28 | int rightCnt = sort(right);
29 |
30 | int index = 0, i = 0, j = 0;
31 | int leftLen = left.length, rightLen = right.length;
32 | while (i < leftLen && j < rightLen) {
33 | if (left[i] > right[j]) {
34 | nums[index++] = right[j++];
35 | cnt += leftLen - i;
36 | } else {
37 | nums[index++] = left[i++];
38 | }
39 | }
40 | while (i < leftLen) {
41 | nums[index++] = left[i++];
42 | }
43 | while (j < rightLen) {
44 | nums[index++] = right[j++];
45 | }
46 | return leftCnt + rightCnt + cnt;
47 | }
48 |
49 |
50 |
51 | public static void main(String[] args) {
52 | Scanner scanner = new Scanner(System.in);
53 | int T = scanner.nextInt();
54 | for (int i = 0; i < T; i++) {
55 | int N = scanner.nextInt();
56 | int[] nums = new int[N];
57 | for (int j = 0; j < N; j++) {
58 | nums[j] = scanner.nextInt();
59 | }
60 | int cnt = sort(nums);
61 | System.out.println(cnt);
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0122.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0122 {
6 | /*
7 | 实现Shell排序
8 |
9 | Description
10 | 实现Shell排序,对给定的无序数组,按照给定的间隔变化(间隔大小即同组数字index的差),打印排序结果,注意不一定是最终排序结果!
11 |
12 | Input
13 | 输入第一行表示测试用例个数,后面为测试用例,每一个用例有两行,第一行为给定数组,第二行为指定间隔,每一个间隔用空格隔开。
14 |
15 | Output
16 | 输出的每一行为一个用例对应的指定排序结果。
17 | */
18 |
19 | /**
20 | *
21 | * @param nums 要排序的数组
22 | * @param gaps 给定的间隔
23 | */
24 | public static void shellSort(int[] nums, int[] gaps) {
25 | for (int i = 0; i < gaps.length; i++) {
26 | int temp, index;
27 | for (int j = gaps[i]; j < nums.length; j++) {
28 | temp = nums[j];
29 | for (index = j - gaps[i]; index >= 0; index -= gaps[i]) {
30 | if (nums[index] > temp) {
31 | nums[index + gaps[i]] = nums[index];
32 | } else {
33 | break;
34 | }
35 | }
36 | nums[index + gaps[i]] = temp;
37 | }
38 | }
39 | }
40 |
41 | public static int[] toIntArray(String[] strs) {
42 | int[] arr = new int[strs.length];
43 | for (int i = 0; i < strs.length; i++) {
44 | arr[i] = Integer.parseInt(strs[i]);
45 | }
46 | return arr;
47 | }
48 |
49 | public static void main(String[] args) {
50 | Scanner scanner = new Scanner(System.in);
51 | int T = Integer.parseInt(scanner.nextLine());
52 | for (int i = 0; i < T; i++) {
53 | String[] num_str = scanner.nextLine().split(" ");
54 | String[] gap_str = scanner.nextLine().split(" ");
55 | int[] nums = toIntArray(num_str);
56 | int[] gaps = toIntArray(gap_str);
57 | shellSort(nums, gaps);
58 | for (int j = 0; j < nums.length - 1; j++) {
59 | System.out.print(nums[j] + " ");
60 | }
61 | System.out.println(nums[nums.length - 1]);
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0123.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Scanner;
5 |
6 | public class Problem0123 {
7 | /*
8 | 棋盘覆盖问题
9 | Description
10 | 棋盘覆盖问题:给定一个大小为2^n*2^n个小方格的棋盘,其中有一个位置已经被填充,现在要用一个L型(2*2个小方格组成的大方格中去掉其中一个小方格)形状去覆盖剩下的小方格。
11 | 求出覆盖方案,即哪些坐标下的小方格使用同一个L型格子覆盖。
12 | 注意:坐标从0开始。左上方的第一个格子坐标为(0,0),第一行第二个坐标为(0,1),第二行第一个为(1,0),以此类推。
13 |
14 | Input
15 | 输入第一行为测试用例个数,后面每一个用例有两行,第一行为n值和特殊的格子的坐标(用空格隔开),第二行为需要查找其属于同一个L型格子的格子坐标。
16 |
17 | Output
18 | 输出每一行为一个用例的解,先按照行值从小到大、再按照列值从小到大的顺序输出每一个用例的两个坐标;用逗号隔开。
19 | */
20 |
21 | static int cnt = 0; //记录L块的编号
22 | /**
23 | * 分治的方式进行棋盘覆盖,每次将棋盘划分为四个大小相等的正方形块
24 | * @param board 棋盘
25 | * @param leftUpX 当前块左上角在整个棋盘的x坐标
26 | * @param leftUpY 当前块左上角在整个棋盘的y坐标
27 | * @param specialX 特殊格子的x坐标
28 | * @param specialY 特殊各自的y坐标
29 | * @param size 分治后当前处理的棋盘块的大小
30 | */
31 | public static void setLBlock(int[][] board, int leftUpX, int leftUpY, int specialX, int specialY, int size) {
32 | if (size == 1) return;
33 | int s = size / 2; //将棋盘划分为左上,右上,左下,右下四个部分
34 | int curNum = ++cnt;
35 |
36 | //左上
37 | if (specialX < leftUpX + s && specialY < leftUpY + s) {
38 | //如果特殊格子在左上,则对左上继续划分,其他部分同理
39 | setLBlock(board, leftUpX, leftUpY, specialX, specialY, s);
40 | } else {
41 | //如果特殊格子不在左上,则将左上的右下角格子设置为特殊格子,即设定为一个L块的一部分,再进行划分
42 | board[leftUpX + s - 1][leftUpY + s - 1] = curNum;
43 | setLBlock(board, leftUpX, leftUpY, leftUpX + s - 1, leftUpY + s - 1, s);
44 | }
45 |
46 | //右上
47 | if (specialX < leftUpX + s && specialY >= leftUpY + s) {
48 | setLBlock(board, leftUpX, leftUpY + s, specialX, specialY, s);
49 | } else {
50 | //如果特殊格子不在右上,则将右上的左下角格子设置为特殊格子
51 | board[leftUpX + s - 1][leftUpY + s] = curNum;
52 | setLBlock(board, leftUpX, leftUpY + s, leftUpX + s - 1, leftUpY + s, s);
53 | }
54 |
55 | //左下
56 | if (specialX >= leftUpX + s && specialY < leftUpY + s) {
57 | setLBlock(board, leftUpX + s, leftUpY, specialX, specialY, s);
58 | } else {
59 | //如果特殊格子不在左下,则将左下的右上角格子设置为特殊格子
60 | board[leftUpX + s][leftUpY + s - 1] = curNum;
61 | setLBlock(board, leftUpX + s, leftUpY, leftUpX + s, leftUpY + s - 1, s);
62 | }
63 |
64 | //右下
65 | if (specialX >= leftUpX + s && specialY >= leftUpY + s) {
66 | setLBlock(board, leftUpX + s, leftUpY + s, specialX, specialY, s);
67 | } else {
68 | //如果特殊格子不在右下,则将右下的左上角格子设置为特殊格子
69 | board[leftUpX + s][leftUpY + s] = curNum;
70 | setLBlock(board, leftUpX + s, leftUpY + s, leftUpX + s, leftUpY + s, s);
71 | }
72 | }
73 |
74 | public static void main(String[] args) {
75 | Scanner scanner = new Scanner(System.in);
76 | int T = scanner.nextInt();
77 | for (int i = 0; i < T; i++) {
78 | cnt = 0;
79 | int n = scanner.nextInt();
80 | int specialX = scanner.nextInt();
81 | int specialY = scanner.nextInt();
82 | int targetX = scanner.nextInt();
83 | int targetY = scanner.nextInt();
84 | int size = (int) Math.pow(2, n);
85 | int[][] board = new int[size][size];
86 | setLBlock(board, 0, 0, specialX, specialY, size);
87 | int targetCnt = board[targetX][targetY];
88 | //根据目标格子中的L块编号寻找与其处于同一个L块的其他格子
89 | //又L块为2*2缺一块的形状,所以只需遍历目标格子周围一圈的格子即可
90 | ArrayList res = new ArrayList<>();
91 | for (int j = Math.max(0, targetX - 1); j <= Math.min(size - 1, targetX + 1); j++) {
92 | for (int k = Math.max(0, targetY - 1); k <= Math.min(size - 1, targetY + 1); k++) {
93 | if (j == targetX && k == targetY) continue;//跳过目标格子
94 | if (board[j][k] == targetCnt) {
95 | res.add(new int[]{j, k});
96 | }
97 | }
98 | }
99 | System.out.println(res.get(0)[0] + " " + res.get(0)[1] + "," + res.get(1)[0] + " " + res.get(1)[1]);
100 | }
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/src/FirstExercise/Problem0125.java:
--------------------------------------------------------------------------------
1 | package FirstExercise;
2 |
3 | import java.util.*;
4 |
5 | public class Problem0125 {
6 | /*
7 | 按照数值个数排序
8 | Description
9 | 对给定数组中的元素按照元素出现的次数排序,出现次数多的排在前面,如果出现次数相同,则按照数值大小排序。
10 | 例如,给定数组为{2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12},则排序后结果为{3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}。
11 |
12 | Input
13 | 输入的第一行为用例个数;后面每一个用例使用两行表示,第一行为数组长度,第二行为数组内容,数组元素间使用空格隔开。
14 |
15 | Output
16 | 每一个用例的排序结果在一行中输出,元素之间使用空格隔开。
17 | */
18 |
19 | public static void sort(int[] nums) {
20 | Map map = new HashMap<>();
21 | for (int i = 0; i < nums.length; i++) {
22 | map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
23 | }
24 | int[][] cnt = new int[map.keySet().size()][2]; //记录数组中所有值极其相应的出现次数
25 | int index = 0;
26 | for (Integer i: map.keySet()) {
27 | cnt[index][0] = i;
28 | cnt[index][1] = map.get(i);
29 | index++;
30 | }
31 | Arrays.sort(cnt, new Comparator() {
32 | @Override
33 | public int compare(int[] o1, int[] o2) {
34 | if (o1[1] == o2[1]) return o1[0] - o2[0]; //排序,如果出现次数相同,则按数值大小排序
35 | return o2[1] - o1[1]; //出现次数不同,则出现次数多的在前
36 | }
37 | });
38 | index = 0;
39 | for (int i = 0; i < cnt.length; i++) {
40 | for (int j = 0; j < cnt[i][1]; j++) {
41 | nums[index++] = cnt[i][0];
42 | }
43 | }
44 | }
45 |
46 | public static void main(String[] args) {
47 | Scanner scanner = new Scanner(System.in);
48 | int T = scanner.nextInt();
49 | for (int i = 0; i < T; i++) {
50 | int n = scanner.nextInt();
51 | int[] nums = new int[n];
52 | for (int j = 0; j < n; j++) {
53 | nums[j] = scanner.nextInt();
54 | }
55 | sort(nums);
56 | for (int j = 0; j < n - 1; j++) {
57 | System.out.print(nums[j] + " ");
58 | }
59 | System.out.println(nums[n - 1]);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0401.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0401 {
6 | /*
7 | 最小化初始点
8 | Description
9 | Given a grid with each cell consisting of positive, negative or no points i.e, zero points.
10 | We can move across a cell only if we have positive points ( > 0 ).
11 | Whenever we pass through a cell, points in that cell are added to our overall points.
12 | We need to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
13 | 1.From a cell (i, j) we can move to (i+1, j) or (i, j+1).
14 | 2.We cannot move from (i, j) if your overall points at (i, j) is <= 0.
15 | 3.We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
16 |
17 | Input
18 | The first line contains an integer 'T' denoting the total number of test cases.
19 | In each test cases, the first line contains two integer 'R' and 'C' denoting the number of rows and column of array.
20 | The second line contains the value of the array i.e the grid, in a single line separated by spaces in row major order.
21 | Constraints:1 ≤ T ≤ 30; 1 ≤ R,C ≤ 10; -30 ≤ A[R][C] ≤ 30
22 | Input: points[m][n] = { {-2, -3, 3},{-5, -10, 1},{10, 30, -5}};
23 |
24 | Output
25 | Print the minimum initial points to reach the bottom right most cell in a separate line.
26 | 7
27 | Explanation:
28 | 7 is the minimum value to reach destination with positive throughout the path. Below is the path.
29 | (0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
30 | We start from (0, 0) with 7, we reach(0, 1) with 5, (0, 2) with 2, (1, 2) with 5, (2, 2)with
31 | and finally we have 1 point (we needed greater than 0 points at the end).
32 | */
33 | public static void main(String[] args) {
34 | Scanner scanner = new Scanner(System.in);
35 | int t = scanner.nextInt();
36 | for (int i = 0; i < t; i++) {
37 | int m = scanner.nextInt();
38 | int n = scanner.nextInt();
39 | int[][] grid = new int[m][n];
40 | for (int j = 0; j < m; j++) {
41 | for (int k = 0; k < n; k++) {
42 | grid[j][k] = scanner.nextInt();
43 | }
44 | }
45 | System.out.println(getMinPoint(grid));
46 | }
47 | }
48 |
49 | //dp[i][j]表示到(i, j)位置的最小值应为多少,由题意值该值至少为1
50 | //min = min(dp[i + 1][j], dp[i][j + 1])
51 | //dp[i][j] = max(min - grid[i][j], 1)
52 | public static int getMinPoint(int[][] grid) {
53 | int m = grid.length, n = grid[0].length;
54 | int[][] dp = new int[m][n];
55 | dp[m - 1][n - 1] = grid[m - 1][n - 1] > 0 ? 1 : (Math.abs(grid[m - 1][n - 1]) + 1);
56 | for (int i = n - 2; i >= 0; i--) {
57 | dp[m - 1][i] = Math.max(dp[m - 1][i + 1] - grid[m - 1][i], 1);
58 | }
59 | for (int i = m - 2; i >= 0; i--) {
60 | dp[i][n - 1] = Math.max(dp[i + 1][n - 1] - grid[i][n - 1], 1);
61 | }
62 | for (int i = m - 2; i >= 0; i--) {
63 | for (int j = n - 2; j >= 0; j--) {
64 | int point = Math.min(dp[i + 1][j], dp[i][j + 1]);
65 | dp[i][j] = Math.max(point - grid[i][j], 1);
66 | }
67 | }
68 | return dp[0][0];
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0402.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0402 {
6 | /*
7 | 矩阵计算
8 | Description
9 | Let's define a Series Whose recurrence formula is as follows :
10 | C(n)= 3C(i-1) + 4C(i-2) + 5C(i-3) + 6C(i-4)
11 | C(0)= 2
12 | C(1)= 0
13 | C(2)= 1
14 | C(3)= 7
15 | Now based on this Series a Matrix Mi,j of size nn is to be formed.
16 | The top left cell is(1,1) and the bottom right corner is (n,n).
17 | Each cell (i,j) of the Matrix contains either 1 or 0. If C( (i*j)^3 ) is odd, Mi,j is 1,
18 | otherwise, it's 0.Count the total number of ones in the Matrix.
19 |
20 | Input
21 | First Line Of the input will contain an integer 'T'- the number of test cases .
22 | Each of the next 'T' lines consists of an integer 'n'.-denoting the size of the matrix.
23 | Constraints :1 ≤ T ≤ 1000; 1 ≤ n ≤ 1000
24 |
25 | Output
26 | For each test case, output a single Integer -the taste value fo the dish of size-n*n.
27 | */
28 |
29 | public static void main(String[] args) {
30 | Scanner scanner = new Scanner(System.in);
31 | int t = scanner.nextInt();
32 | for (int i = 0; i < t; i++) {
33 | int n = scanner.nextInt();
34 | System.out.println(getOddCnt(n));
35 | }
36 | }
37 |
38 | //找规律题
39 | //C(n) = 3C(n-1)+4C(n-2)+5C(n-3)+6C(n-4)
40 | //显然C(n)的奇偶性取决于C(n-1)和C(n-3)的奇偶性,只要C(n-1)和C(n-3)一奇数一偶数,则C(n)为奇数,否则为偶数
41 | //经过计算可以发现,C(n)的奇偶性每7为一个循环
42 | public static int getOddCnt(int n) {
43 | int cnt = 0;
44 | int[] arr = {0, 0, 1, 1, 1, 0, 1};
45 | for (int i = 1; i <= n; i++) {
46 | for (int j = 1; j <= n; j++) {
47 | int index = (int) (Math.pow((i % 7) * (j % 7), 3) % 7);
48 | if (arr[index] == 1) {
49 | cnt++;
50 | }
51 | }
52 | }
53 | return cnt;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0403.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0403 {
6 | /*
7 | 无重复字符子集问题
8 | Description
9 | Mike is a lawyer with the gift of photographic memory.
10 | He is so good with it that he can tell you all the numbers on a sheet of paper by having a look at it without any mistake.
11 | Mike is also brilliant with subsets so he thought of giving a challenge based on his skill and knowledge to Rachael.
12 | Mike knows how many subset are possible in an array of N integers. The subsets may or may not have the different sum.
13 | The challenge is to find the maximum sum produced by any subset under the condition:
14 | The elements present in the subset should not have any digit in common.
15 | Note: Subset {12, 36, 45} does not have any digit in common and Subset {12, 22, 35} have digits in common.
16 | Rachael find it difficult to win the challenge and is asking your help. Can you help her out in winning this challenge?
17 |
18 | Input
19 | First Line of the input consist of an integer T denoting the number of test cases.
20 | Then T test cases follow. Each test case consist of a number N denoting the length of the array.
21 | Second line of each test case consist of N space separated integers denoting the array elements.
22 | Constraints:1 <= T <= 100;1 <= N <= 100;1 <= array elements <= 100000
23 |
24 | Output
25 | Corresponding to each test case, print output in the new line.
26 | */
27 |
28 | public static void main(String[] args) {
29 | Scanner scanner = new Scanner(System.in);
30 | int t = scanner.nextInt();
31 | for (int i = 0; i < t; i++) {
32 | int n = scanner.nextInt();
33 | int[] arr = new int[n];
34 | for (int j = 0; j < n; j++) {
35 | arr[j] = scanner.nextInt();
36 | }
37 | boolean[] isVisited = new boolean[10];
38 | System.out.println(getMaxSumOfSubSequence(arr, isVisited, 0, 0));
39 | }
40 | }
41 |
42 | public static int getMaxSumOfSubSequence(int[] arr, boolean[] isVisited, int index, int res) {
43 | if (index == arr.length) {
44 | return res;
45 | }
46 | int skipRes = getMaxSumOfSubSequence(arr, isVisited, index + 1, res);
47 | int num = arr[index];
48 | boolean flag = false;
49 | char[] cArr = Integer.toString(num).toCharArray();
50 | for (char c: cArr) {
51 | if (isVisited[c - '0']) {
52 | flag = true;
53 | break;
54 | }
55 | }
56 | //有数字重复了,当前数不加
57 | if (flag) {
58 | return skipRes;
59 | } else {
60 | //没有数字重复,可加可不加
61 | for (char c: cArr) {
62 | isVisited[c - '0'] = true;
63 | }
64 | int noSkipRes = getMaxSumOfSubSequence(arr, isVisited, index + 1, res + num);
65 | for (char c: cArr) {
66 | isVisited[c - '0'] = false;
67 | }
68 | return Math.max(skipRes, noSkipRes);
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0404.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0404 {
6 | /*
7 | 是否能通过考试
8 | Description
9 | 小张想要通过明天的考试。他知道考题的分值分布,也知道考试中要拿到每一个题目需要耗费的时间。
10 | 假设考试时长为h,共n个题目,需要拿到p分才能通过考试。现在已知每个考题的得分与耗时,
11 | 请你判断小张能否通过合理安排时间,而通过考试,并给出通过考试的最短时间。
12 |
13 | Input
14 | 输入第一行为测试用例个数.每一个用例有若干行,第一行为任务数量n、考试时常h、通过分数p,下面的n行是每一个题目的耗时和得分。所有数值用空格分开。
15 |
16 | Output
17 | 对每一个用例输出一行,如果能够通过考试,则输出“YES”和消耗最短时间,用空格隔开。 否则,输出“NO”。
18 | */
19 | public static void main(String[] args) {
20 | Scanner scanner = new Scanner(System.in);
21 | int t = scanner.nextInt();
22 | for (int i = 0; i < t; i++) {
23 | int n = scanner.nextInt();
24 | int h = scanner.nextInt();
25 | int p = scanner.nextInt();
26 | int[][] tasks = new int[n][2];
27 | for (int j = 0; j < n; j++) {
28 | tasks[j][0] = scanner.nextInt();
29 | tasks[j][1] = scanner.nextInt();
30 | }
31 | int minTime = getMinTime(tasks, n, h, p);
32 | if (minTime == -1) {
33 | System.out.println("NO");
34 | } else {
35 | System.out.println("YES " + minTime);
36 | }
37 | }
38 | }
39 |
40 | //背包问题(背包容量就是考试时长,物品价值就是题目的分数)
41 | //dp[i][j]表示在不超过时间j的情况下,做到第i题最多能拿多少分
42 | //dp[i][j] = max(dp[i-1][j], dp[i-1][j-tasks[i][0]] + tasks[i][1])
43 | public static int getMinTime(int[][] tasks, int n, int h, int p) {
44 | int[][] dp = new int[n + 1][h + 1];
45 | int minTime = h + 1;
46 | for (int i = 1; i <= n; i++) {
47 | for (int j = 1; j <= h; j++) {
48 | if (tasks[i - 1][0] <= j) { //余下的时间够做这题,做or不做
49 | dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - tasks[i - 1][0]] + tasks[i - 1][1]);
50 | } else {
51 | dp[i][j] = dp[i - 1][j];
52 | }
53 | if (dp[i][j] >= p) {
54 | minTime = Math.min(minTime, j);
55 | }
56 | }
57 | }
58 | //如果全都做完还无法满足p,说明无法通过考试,返回-1
59 | return dp[n][h] < p ? -1 : minTime;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0405.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0405 {
6 | /*
7 | 如何花最少的钱购买蔬菜
8 | Description
9 | Rahul wanted to purchase vegetables mainly brinjal, carrot and tomato. There are N different vegetable sellers in a line.
10 | Each vegetable seller sells all three vegetable items, but at different prices.
11 | Rahul, obsessed by his nature to spend optimally, decided not to purchase same vegetable from adjacent shops.
12 | Also, Rahul will purchase exactly one type of vegetable item (only 1 kg) from one shop.
13 | Rahul wishes to spend minimum money buying vegetables using this strategy. Help Rahul determine the minimum money he will spend.
14 |
15 | Input
16 | First line indicates number of test cases T.
17 | Each test case in its first line contains N denoting the number of vegetable sellers in Vegetable Market.
18 | Then each of next N lines contains three space separated integers denoting cost of brinjal, carrot and tomato per kg with that particular vegetable seller.
19 |
20 | Output
21 | For each test case, output the minimum cost of shopping taking the mentioned conditions into account in a separate line.
22 | Constraints:1 <= T <= 10,1 <= N <= 100000 Cost of each vegetable(brinjal/carrot/tomato) per kg does not exceed 10^4
23 | */
24 |
25 | public static void main(String[] args) {
26 | Scanner scanner = new Scanner(System.in);
27 | int t = scanner.nextInt();
28 | for (int i = 0; i < t; i++) {
29 | int n = scanner.nextInt();
30 | int[][] costs = new int[n][3];
31 | for (int j = 0; j < n; j++) {
32 | for (int k = 0; k < 3; k++) {
33 | costs[j][k] = scanner.nextInt();
34 | }
35 | }
36 | System.out.println(getMinCost(costs));
37 | }
38 | }
39 |
40 | //dp[i][j]表示到第i家店买到j产品的最小成本
41 | //dp[i][j] = min(dp[i-1][0...2])+costs[i][j]
42 | // |--这里不取j值
43 | public static int getMinCost(int[][] costs) {
44 | int n = costs.length;
45 | int[][] dp = new int[n][3];
46 | for (int i = 0; i < 3; i++) {
47 | dp[0][i] = costs[0][i];
48 | }
49 | for (int i = 1; i < n; i++) {
50 | for (int j = 0; j < 3; j++) {
51 | dp[i][j] = Math.min(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + costs[i][j];
52 | }
53 | }
54 | return Math.min(dp[n - 1][0], Math.min(dp[n - 1][1], dp[n - 1][2]));
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0406.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.List;
6 | import java.util.Scanner;
7 |
8 | public class Problem0406 {
9 | /*
10 | 按照要求保留数组元素使得和最大
11 | Description
12 | Given an array of N numbers, we need to maximize the sum of selected numbers.
13 | At each step, you need to select a number Ai, delete one occurrence of Ai-1 (if exists) and Ai each from the array.
14 | Repeat these steps until the array gets empty. The problem is to maximize the sum of selected numbers.
15 |
16 | Input
17 | The first line of the input contains T denoting the number of the test cases.
18 | For each test case, the first line contains an integer n denoting the size of the array.
19 | Next line contains n space separated integers denoting the elements of the array.
20 | Constraints:1<=T<=100,1<=n<=50,1<=A[i]<=20
21 | Note: Numbers need to be selected from maximum to minimum.
22 |
23 | Output
24 | For each test case, the output is an integer displaying the maximum sum of selected numbers.
25 | */
26 |
27 | public static void main(String[] args) {
28 | Scanner scanner = new Scanner(System.in);
29 | int t = scanner.nextInt();
30 | for (int i = 0; i < t; i++) {
31 | int n = scanner.nextInt();
32 | List list = new ArrayList<>();
33 | for (int j = 0; j < n; j++) {
34 | list.add(scanner.nextInt());
35 | }
36 | System.out.println(getMaxSumOfSelected(list));
37 | }
38 | }
39 |
40 | //迷惑,说了从最大删到最小,那还有什么可操作的点,直接遍历不就完了
41 | public static int getMaxSumOfSelected(List list) {
42 | Collections.sort(list);
43 | int index = list.size() - 1;
44 | int sum = 0;
45 | while (list.size() > 0) {
46 | int num = list.get(index);
47 | list.remove(index);
48 | sum += num;
49 | int lowIndex = list.indexOf(num - 1);
50 | if (lowIndex != -1) {
51 | list.remove(lowIndex);
52 | index--;
53 | }
54 | index--;
55 | }
56 | return sum;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0407.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0407 {
6 | /*
7 | 数组查询
8 | Description
9 | Given an array, the task is to complete the function which finds the maximum sum subarray,
10 | where you may remove at most one element to get the maximum sum.
11 |
12 | Input
13 | 第一行为测试用例个数T;后面每两行表示一个用例,第一行为用例中数组长度N,第二行为数组具体内容。
14 |
15 | Output
16 | 每一行表示对应用例的结果。
17 | */
18 |
19 | public static void main(String[] args) {
20 | Scanner scanner = new Scanner(System.in);
21 | int t = scanner.nextInt();
22 | for (int i = 0; i < t; i++) {
23 | int n = scanner.nextInt();
24 | int[] arr = new int[n];
25 | for (int j = 0; j < n; j++) {
26 | arr[j] = scanner.nextInt();
27 | }
28 | System.out.println(getMaxSum(arr));
29 | }
30 | }
31 |
32 | //刚开始理解错题意了
33 | //题目的意思是,找最大和的连续子数组,计算最大和的时候允许从数组中至多删除一个元素
34 | public static int getMaxSum(int[] arr) {
35 | int n = arr.length;
36 | int max = arr[0];
37 | for (int i = 0; i < n; i++) {
38 | max = Math.max(max, arr[i]);
39 | }
40 | //如果全部非正数,则返回最大值
41 | if (max <= 0) {
42 | return max;
43 | }
44 | //记录当前位置左侧的最大子数组和
45 | int[] left = new int[n];
46 | //记录当前位置右侧的最大子数组和
47 | int[] right = new int[n];
48 | for (int i = 0; i < n - 1; i++) {
49 | left[i + 1] = Math.max(left[i] + arr[i], 0);
50 | }
51 | for (int i = n - 1; i > 0; i--) {
52 | right[i - 1] = Math.max(right[i] + arr[i], 0);
53 | }
54 | int res = Integer.MIN_VALUE;
55 | for (int i = 0; i < n; i++) {
56 | res = Math.max(res, Math.max(left[i] + right[i], left[i] + arr[i] + right[i]));
57 | }
58 | return res;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/FourthExercise/Problem0408.java:
--------------------------------------------------------------------------------
1 | package FourthExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0408 {
6 | /*
7 | 订单问题
8 | Description
9 | Rahul and Ankit are the only two waiters in Royal Restaurant. Today, the restaurant received N orders.
10 | The amount of tips may differ when handled by different waiters,
11 | if Rahul takes the ith order, he would be tipped Ai rupees and if Ankit takes this order, t
12 | he tip would be Bi rupees.In order to maximize the total tip value they decided to distribute the order among themselves.
13 | One order will be handled by one person only.
14 | Also, due to time constraints Rahul cannot take more than X orders and Ankit cannot take more than Y orders.
15 | It is guaranteed that X + Y is greater than or equal to N, which means that all the orders can be handled by either Rahul or Ankit.
16 | Find out the maximum possible amount of total tip money after processing all the orders.
17 |
18 | Input
19 | • The first line contains one integer, number of test cases.
20 | • The second line contains three integers N, X, Y.
21 | • The third line contains N integers. The ith integer represents Ai.
22 | • The fourth line contains N integers. The ith integer represents Bi.
23 |
24 | Output
25 | Print a single integer representing the maximum tip money they would receive.
26 | */
27 | public static void main(String[] args) {
28 | Scanner scanner = new Scanner(System.in);
29 | int t = scanner.nextInt();
30 | for (int i = 0; i < t; i++) {
31 | int n = scanner.nextInt();
32 | int x = scanner.nextInt();
33 | int y = scanner.nextInt();
34 | int[] aCost = new int[n];
35 | for (int j = 0; j < n; j++) {
36 | aCost[j] = scanner.nextInt();
37 | }
38 | int[] bCost = new int[n];
39 | for (int j = 0; j < n; j++) {
40 | bCost[j] = scanner.nextInt();
41 | }
42 | System.out.println(getMaxTipMoney(n, x, y, aCost, bCost));
43 | }
44 | }
45 |
46 |
47 | //dp[i][j][k]指的是i个任务,A做了j个,B做了k个的最大值情况
48 | public static int getMaxTipMoney(int n, int x, int y, int[] aCost, int[] bCost) {
49 | int[][][] dp = new int[n + 1][x + 1][y + 1];
50 | for (int i = 1; i <= n; i++) {
51 | for (int j = 1; j <= x; j++) {
52 | dp[i][j][0] = dp[i - 1][j - 1][0] + aCost[i - 1];
53 | }
54 | }
55 | for (int i = 1; i <= n; i++) {
56 | for (int j = 1; j <= y; j++) {
57 | dp[i][0][j] = dp[i - 1][0][j - 1] + bCost[j - 1];
58 | }
59 | }
60 | for (int i = 1; i <= n; i++) {
61 | for (int j = 1; j <= x; j++) {
62 | for (int k = 1; k <= y; k++) {
63 | dp[i][j][k] = Math.max(dp[i - 1][j - 1][k] + aCost[i - 1], dp[i - 1][j][k - 1] + bCost[i - 1]);
64 | }
65 | }
66 | }
67 | return dp[n][x][y];
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0201.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.*;
4 |
5 | public class Problem0201 {
6 | /*
7 | 拓扑排序解的个数
8 | Description
9 | 给定有向无环图中所有边,计算图的拓扑排序解的个数。
10 |
11 | Input
12 | 输入第一行为用例个数,后面每一行表示一个图中的所有边,边的起点和终点用空格隔开,边之间使用逗号隔开。
13 |
14 | Output
15 | 输出拓扑排序解的个数。
16 | */
17 |
18 | /**
19 | *
20 | * @param graph 邻接矩阵
21 | * @param indegree 入度数组
22 | * @param zeros 入度为0的节点列表
23 | * @param isVisited 标志节点是否已访问
24 | * @param list 记录当前拓扑
25 | * @param res 所有拓扑的列表
26 | */
27 | public static void topo(int[][] graph, int[] indegree, List zeros, boolean[] isVisited, ArrayList list, List> res) {
28 | if (list.size() >= graph.length) { //所有点都已访问
29 | res.add(new ArrayList<>(list));
30 | return;
31 | }
32 | for (int i = 0; i < zeros.size(); i++) {
33 | int index = zeros.get(i);
34 | if (!isVisited[index]) {
35 | list.add(index); //将顶点插入拓扑排序中
36 | isVisited[index] = true;
37 | List deleteEdge = new ArrayList<>(); //记录删去的边的终点
38 | int cnt = 0; //记录新的入度为0的顶点个数
39 | for (int j = 0; j < graph.length; j++) { //将index顶点出发的边删除
40 | if (graph[index][j] == 1) {
41 | deleteEdge.add(j);
42 | indegree[j]--;
43 | graph[index][j] = 0;
44 | if (indegree[j] == 0) {
45 | zeros.add(j);
46 | cnt++;
47 | }
48 | }
49 | }
50 | topo(graph, indegree, zeros, isVisited, list, res);
51 | //将删除的边恢复,将点的访问状态重置
52 | isVisited[index] = false;
53 | list.remove(list.size() - 1);
54 | for (int j = 0; j < cnt; j++) {
55 | zeros.remove(zeros.size() - 1);
56 | }
57 | for (Integer endIndex : deleteEdge) {
58 | graph[index][endIndex] = 1;
59 | indegree[endIndex]++;
60 | }
61 | }
62 | }
63 | }
64 |
65 | public static void main(String[] args) {
66 | Scanner scanner = new Scanner(System.in);
67 | int T = Integer.parseInt(scanner.nextLine());
68 | for (int i = 0; i < T; i++) {
69 | String[] edges = scanner.nextLine().split(",");
70 | Set vertex = new HashSet<>();
71 | for (String edge: edges) {
72 | String[] v = edge.split(" ");
73 | vertex.add(v[0]);
74 | vertex.add(v[1]);
75 | }
76 | int n = vertex.size();
77 | int[][] graph = new int[n][n]; //邻接矩阵
78 | int[] indegree = new int[n]; //记录所有顶点的入度
79 | for (String edge: edges) {
80 | String[] v = edge.split(" ");
81 | int x = v[0].charAt(0) - 'a';
82 | int y = v[1].charAt(0) - 'a';
83 | graph[x][y] = 1;
84 | indegree[y]++;
85 | }
86 | List zeros = new ArrayList<>(); //记录入度为0的点
87 | for (int j = 0; j < n; j++) {
88 | if (indegree[j] == 0) {
89 | zeros.add(j);
90 | }
91 | }
92 | boolean[] isVisited = new boolean[n]; //标志节点是否访问,即是否已加入拓扑队列中
93 | List> res = new ArrayList<>();//存放所有拓扑排序结果
94 | topo(graph, indegree, zeros, isVisited, new ArrayList(), res);
95 | System.out.println(res.size());
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0202.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.*;
4 |
5 | public class Problem0202 {
6 | /*
7 | 广度优先遍历图
8 | Description
9 | 按照给定的起始顶点广度优先遍历图,每一次通过字母顺序选择顶点查找下一层邻接点,打印遍历顺序。
10 |
11 | Input
12 | 输入第一行为测试用例个数,后面每一个用例用多行表示,用例第一行是节点个数n和开始顶点,用空格隔开,后面n+1行为图的邻接矩阵,其中第一行为节点名称。
13 | 值之间使用空格隔开。
14 |
15 | Output
16 | 输出遍历顺序,用空格隔开
17 | */
18 |
19 | /**
20 | *
21 | * @param startIndex 起始点
22 | * @param graph 邻接矩阵
23 | * @param isVisited 节点访问标记
24 | * @param bfsList 遍历结果
25 | */
26 | public static void bfs(int startIndex, int[][] graph, boolean[] isVisited, List bfsList) {
27 | isVisited[startIndex] = true;
28 | bfsList.add(startIndex);
29 | Queue queue = new PriorityQueue<>();
30 | queue.offer(startIndex);
31 | while (!queue.isEmpty()) {
32 | int index = queue.poll();
33 | for (int i = 0; i < graph[index].length; i++) {
34 | if (!isVisited[i] && graph[index][i] == 1) {
35 | isVisited[i] = true;
36 | queue.offer(i);
37 | bfsList.add(i);
38 | }
39 | }
40 | }
41 | }
42 |
43 | public static void main(String[] args) {
44 | Scanner scanner = new Scanner(System.in);
45 | int T = Integer.parseInt(scanner.nextLine());
46 | for (int i = 0; i < T; i++) {
47 | String s = scanner.nextLine();
48 | int n = Integer.parseInt(s.split(" ")[0]);
49 | String startVertex = s.split(" ")[1];
50 | String[] vertex_str = scanner.nextLine().split(" ");
51 | List vertexes = new ArrayList<>();
52 | for (int j = 0; j < n; j++) {
53 | vertexes.add(vertex_str[j]);
54 | }
55 | Collections.sort(vertexes);
56 | int[][] graph = new int[n][n]; //邻接矩阵
57 | for (int j = 0; j < n; j++) {
58 | String[] strs = scanner.nextLine().split(" ");
59 | int index = vertexes.indexOf(strs[0]);
60 | for (int k = 0; k < n; k++) {
61 | graph[index][k] = Integer.parseInt(strs[k + 1]);
62 | }
63 | }
64 | List bfsList = new ArrayList<>(); //记录广度优先遍历结果
65 | boolean[] isVisited = new boolean[n]; //记录节点访问情况
66 | int startIndex = vertexes.indexOf(startVertex);
67 | bfs(startIndex, graph, isVisited, bfsList);
68 | for (int j = 0; j < n - 1; j++) {
69 | System.out.print(vertexes.get(bfsList.get(j)) + " ");
70 | }
71 | System.out.println(vertexes.get(bfsList.get(n - 1)));
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0203.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 |
7 | public class Problem0203 {
8 | /*
9 | 深度优先遍历
10 | Description
11 | 按照给定的起始顶点深度优先遍历给定的无向图,尝试所有可能的遍历方式,打印遍历过程中出现的最大深度。
12 |
13 | Input
14 | 输入第一行是用例个数,后面每个用例使用多行表示,用例的第一行是图中节点的个数n和起始点,用空格隔开,后面n+1行为图的邻接矩阵,其中第一行为节点名称。
15 | 值之间使用空格隔开。
16 |
17 | Output
18 | 输出深度优先遍历中遇到的最大深度。
19 | */
20 | public static int dfs(int startIndex, int[][] graph, boolean[] isVisited, List dfsList) {
21 | if (dfsList.size() >= graph.length || isVisited[startIndex]) { //遍历完毕的情况 或 遇到的点已遍历 则返回
22 | return dfsList.size();
23 | }
24 | isVisited[startIndex] = true;
25 | dfsList.add(startIndex);
26 | int maxSize = dfsList.size();
27 | for (int i = 0; i < graph[startIndex].length; i++) {
28 | if (graph[startIndex][i] == 1) {
29 | maxSize = Math.max(maxSize, dfs(i, graph, isVisited, dfsList));
30 | }
31 | }
32 | isVisited[startIndex] = false;
33 | dfsList.remove(dfsList.size() - 1);
34 | return maxSize;
35 | }
36 |
37 | public static void main(String[] args) {
38 | Scanner scanner = new Scanner(System.in);
39 | int T = Integer.parseInt(scanner.nextLine());
40 | for (int i = 0; i < T; i++) {
41 | String s = scanner.nextLine();
42 | int n = Integer.parseInt(s.split(" ")[0]);
43 | String startVertex = s.split(" ")[1];
44 | String[] vertex_str = scanner.nextLine().split(" ");
45 | List vertexes = new ArrayList<>();
46 | for (int j = 0; j < n; j++) {
47 | vertexes.add(vertex_str[j]);
48 | }
49 | int[][] graph = new int[n][n]; //邻接矩阵
50 | for (int j = 0; j < n; j++) {
51 | String[] strs = scanner.nextLine().split(" ");
52 | int index = vertexes.indexOf(strs[0]);
53 | for (int k = 0; k < n; k++) {
54 | graph[index][k] = Integer.parseInt(strs[k + 1]);
55 | }
56 | }
57 | List dfsList = new ArrayList<>(); //记录深度优先遍历结果
58 | boolean[] isVisited = new boolean[n]; //记录节点访问情况
59 | int startIndex = vertexes.indexOf(startVertex);
60 | System.out.println(dfs(startIndex, graph, isVisited, dfsList));
61 | }
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0204.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.*;
4 |
5 | public class Problem0204 {
6 | /*
7 | 分配问题
8 | Description
9 | 对给定的n个任务与n个人之间的成本矩阵完成成本最低的任务分配策略。
10 |
11 | Input
12 | 输入:第一行为用例个数,之后为每一个用例;用例的第一行为任务个数,即n;
13 | 用例的第二行为使用逗号隔开的人员完成任务的成本;每一个成本描述包括人员序号、任务序号和成本,使用空格隔开。
14 | 人员序号和任务序号都是从1到n的整数,序号出现的次序没有固定规则。
15 |
16 | Output
17 | 输出:每一个用例输出一行,从序号为1的人员开始,给出其分配的任务序号,使用空格隔开;使用逗号将多个解隔开。
18 | 结果按照人员分配的任务序号大小排,第一个人员的任务序号大的放在前面,如果相同则看第二个人员的任务,以此类推。
19 | */
20 |
21 | /**
22 | *
23 | * @param list 当前排列列表
24 | * @param all 所有排列的集合
25 | * @param isVisited 记录各任务是否被访问
26 | * @param n 任务数量
27 | */
28 | public static void perm(List list, List> all, boolean[] isVisited, int n) {
29 | if (list.size() >= n) {
30 | all.add(new ArrayList<>(list));
31 | return;
32 | }
33 | for (int i = 0; i < n; i++) {
34 | if (!isVisited[i]) {
35 | list.add(i + 1);
36 | isVisited[i] = true;
37 | perm(list, all, isVisited, n);
38 | list.remove(list.size() - 1);
39 | isVisited[i] = false;
40 | }
41 | }
42 | }
43 |
44 | public static void printList(List list) {
45 | for (int i = 0; i < list.size() - 1; i++) {
46 | System.out.print(list.get(i) + " ");
47 | }
48 | System.out.print(list.get(list.size() - 1));
49 | }
50 |
51 | public static void main(String[] args) {
52 | Scanner scanner = new Scanner(System.in);
53 | int T = Integer.parseInt(scanner.nextLine());
54 | for (int i = 0; i < T; i++) {
55 | int n = Integer.parseInt(scanner.nextLine());
56 | int[][] matrix = new int[n][n];
57 | String[] costs = scanner.nextLine().split(",");
58 | for (String cost_str: costs) {
59 | String[] str = cost_str.split(" ");
60 | int person = Integer.parseInt(str[0]);
61 | int task = Integer.parseInt(str[1]);
62 | int cost = Integer.parseInt(str[2]);
63 | matrix[person - 1][task - 1] = cost;
64 | }
65 | List> all = new ArrayList<>();
66 | boolean[] isVisited = new boolean[n];
67 | perm(new ArrayList<>(), all, isVisited, n);
68 | int min = Integer.MAX_VALUE;
69 | List> minList = new ArrayList<>();
70 | for (List list : all) {
71 | int total = 0;
72 | for (int k = 0; k < n; k++) {
73 | total += matrix[k][list.get(k) - 1];
74 | }
75 | if (total < min) {
76 | min = total;
77 | minList = new ArrayList<>();
78 | minList.add(list);
79 | } else if (total == min) {
80 | minList.add(list);
81 | }
82 | }
83 | minList.sort(new Comparator>() {
84 | @Override
85 | public int compare(List o1, List o2) {
86 | for (int j = 0; j < o1.size(); j++) {
87 | if (o1.get(j) > o2.get(j)) {
88 | return -1;
89 | } else if (o1.get(j) < o2.get(j)) {
90 | return 1;
91 | }
92 | }
93 | return 0;
94 | }
95 | });
96 | for (int j = 0; j < minList.size() - 1; j++) {
97 | printList(minList.get(j));
98 | System.out.print(",");
99 | }
100 | printList(minList.get(minList.size() - 1));
101 | System.out.println();
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0205.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0205 {
6 | /*
7 | Searching_4
8 | Description
9 | Given n Magnets which are placed linearly, with each magnet to be considered as of point object.
10 | Each magnet suffers force from its left sided magnets such that they repel it to the right and vice versa.
11 | All forces are repulsive. The force being equal to the distance (1/d , d being the distance).
12 | Now given the positions of the magnets, the task to find all the points along the linear line where net force is ZERO.
13 | Note: Distance have to be calculated with precision of 0.0000000000001.
14 | Constraints:1<=T<=100,1<=N<=100,1<=M[]<=1000
15 |
16 | Input
17 | The first line of input contains an integer T denoting the no of test cases.
18 | Then T test cases follow. The second line of each test case contains an integer N.
19 | Then in the next line are N space separated values of the array M[], denoting the positions of the magnet.
20 |
21 | Output
22 | For each test case in a new line print the space separated points having net force zero till precised two decimal places.
23 | */
24 |
25 | public static double getForce(double x, int[] m) {
26 | double force = 0.0;
27 | for (int i = 0; i < m.length; i++) {
28 | force += 1 / (x - m[i]);
29 | }
30 | return force;
31 | }
32 |
33 | //二分查找每两个磁体间的所求受力为0的点
34 | public static double getForceZero(int[] m, int index) {
35 | //double error = 0.0000000000001;//允许误差
36 | double error = 0.000000000001;//题中给出的允许误差太精确了,会超时,适当扩大误差范围
37 | double left = m[index], right = m[index + 1];
38 | while (left < right) {
39 | double mid = (left + right) / 2.0;
40 | double force = getForce(mid, m);
41 | if (Math.abs(force) < error) {
42 | return mid;
43 | } else if (force > 0) {
44 | left = mid;
45 | } else {
46 | right = mid;
47 | }
48 | }
49 | return left;
50 | }
51 |
52 | public static void main(String[] args) {
53 | Scanner scanner = new Scanner(System.in);
54 | int T = scanner.nextInt();
55 | for (int i = 0; i < T; i++) {
56 | int N = scanner.nextInt();
57 | int[] M = new int[N];
58 | for (int j = 0; j < N; j++) {
59 | M[j] = scanner.nextInt();
60 | }
61 | for (int j = 0; j < N - 2; j++) {
62 | double pos = getForceZero(M, j);
63 | System.out.print(String.format("%.2f", pos) + " ");
64 | }
65 | System.out.print(String.format("%.2f", getForceZero(M, N - 2)));
66 | System.out.println();
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0206.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.*;
4 |
5 | public class Problem0206 {
6 | /*
7 | 按照另一个数组排序
8 | Description
9 | Given two array A1[] and A2[], sort A1 in such a way that the relative order among the elements will be same as those in A2.
10 | For the elements not present in A2. Append them at last in sorted order.
11 | It is also given that the number of elements in A2[] are smaller than or equal to number of elements in A1[] and A2[] has all distinct elements.
12 | Input:A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8} A2[] = {2, 1, 8, 3}Output: A1[] = {2, 2, 1, 1, 8, 8, 3, 5, 6, 7, 9}
13 | Since 2 is present first in A2[], all occurrences of 2s should appear first in A[],
14 | then all occurrences 1s as 1 comes after 2 in A[]. Next all occurrences of 8 and then all occurrences of 3.
15 | Finally we print all those elements of A1[] that are not present in A2[]
16 | Constraints:1 ≤ T ≤ 50 1 ≤ M ≤ 50 1 ≤ N ≤ 10 & N ≤ M 1 ≤ A1[i], A2[i] ≤ 1000
17 |
18 | Input
19 | The first line of input contains an integer T denoting the number of test cases.
20 | The first line of each test case is M and N. M is the number of elements in A1 and N is the number of elements in A2.
21 | The second line of each test case contains M elements. The third line of each test case contains N elements.
22 |
23 | Output
24 | Print the sorted array according order defined by another array.
25 | */
26 |
27 | public static int[] sortByAnother(int[] a1, int[] a2) {
28 | Map map = new HashMap<>();
29 | for (int i : a2) {
30 | map.put(i, 0);
31 | }
32 | List notInAnother = new ArrayList<>();
33 | for (int i : a1) {
34 | if (map.containsKey(i)) {
35 | map.put(i, map.get(i) + 1);
36 | } else {
37 | notInAnother.add(i);
38 | }
39 | }
40 | int[] res = new int[a1.length];
41 | int index = 0;
42 | for (int i = 0; i < a2.length; i++) {
43 | int cnt = map.get(a2[i]);
44 | for (int j = 0; j < cnt; j++) {
45 | res[index++] = a2[i];
46 | }
47 | }
48 | Collections.sort(notInAnother);
49 | for (int i = 0; i < notInAnother.size(); i++) {
50 | res[index++] = notInAnother.get(i);
51 | }
52 | return res;
53 | }
54 |
55 | public static void main(String[] args) {
56 | Scanner scanner = new Scanner(System.in);
57 | int T = scanner.nextInt();
58 | for (int i = 0; i < T; i++) {
59 | int m = scanner.nextInt();
60 | int n = scanner.nextInt();
61 | int[] a1 = new int[m];
62 | int[] a2 = new int[n];
63 | for (int j = 0; j < m; j++) {
64 | a1[j] = scanner.nextInt();
65 | }
66 | for (int j = 0; j < n; j++) {
67 | a2[j] = scanner.nextInt();
68 | }
69 | int[] res = sortByAnother(a1, a2);
70 | for (int j = 0; j < res.length - 1; j++) {
71 | System.out.print(res[j] + " ");
72 | }
73 | System.out.println(res[res.length - 1]);
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0207.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0207 {
6 | /*
7 | Searching_3
8 | Description
9 | They declared Sonam as bewafa. Although she is not, believe me!
10 | She asked a number of queries to people regrading their position in a test.
11 | Now its your duty to remove her bewafa tag by answering simple queries.
12 | All the students who give test can score from 1 to 10^18. Lower the marks, better the rank.
13 | Now instead of directly telling the marks of student they have been assigned groups where marks are distributed in continuous intervals,
14 | you have been given l(i) lowest mark of interval i and r(i) highest marks in interval i.
15 | So marks distribution in that interval is given as l(i), l(i)+1, l(i)+2 . . . r(i)
16 | Now Sonam ask queries in which she gives rank of the student (x) and you have to tell marks obtained by that student
17 | Note: rank1 is better than rank2 and rank2 is better than rank3 and so on and the first interval starts from 1.
18 | Constraints:1<=T<=50,1<=N<=10^5,1<=Q<=10^5,1<= l(i) < r(i) <=10^18,1<=x<=10^18
19 |
20 | Input
21 | The first line of input contains an integer T, denoting the no of test cases.
22 | Then T test cases follow. Each test case contains two space separated values N and Q denoting the no of groups and number of queries asked respectively.
23 | The next line contains N group of two integers separated by space which shows lowest marks in group i ie l(i) and highest marks in group i ie r(i) such that if i < j then r(i) < l(j).
24 | The next lines contain Q space separated integers x, denoting rank of student.
25 |
26 | Output
27 | For each query output marks obtain by student whose rank is x(1<=x<=10^18).
28 | */
29 |
30 | public static long getMark(long[] low, long[] len_arr, long x) {
31 | int left = 0, right = low.length - 1;
32 | //二分方式寻找x所处的区间
33 | while (left < right) {
34 | int mid = (left + right) / 2;
35 | if (len_arr[mid] < x) {
36 | left = mid + 1;
37 | } else {
38 | right = mid;
39 | }
40 | }
41 | //x所处区间前的所有区间长度和
42 | long preLen = left == 0 ? 0 : len_arr[left - 1];
43 | return low[left] + x - preLen - 1;
44 | }
45 |
46 | public static void main(String[] args) {
47 | Scanner scanner = new Scanner(System.in);
48 | int T = scanner.nextInt();
49 | for (int i = 0; i < T; i++) {
50 | int N = scanner.nextInt();
51 | int Q = scanner.nextInt();
52 | long[] low = new long[N]; //记录各区间的左端点
53 | long[] high = new long[N]; //记录各区间的右端点
54 | long[] len_arr = new long[N]; //记录各区间的长度
55 | long[] query = new long[Q];
56 | long len = 0;
57 | for (int j = 0; j < N; j++) {
58 | low[j] = scanner.nextLong();
59 | high[j] = scanner.nextLong();
60 | len += high[j] - low[j] + 1;
61 | len_arr[j] = len;
62 | }
63 | for (int j = 0; j < Q; j++) {
64 | query[j] = scanner.nextLong();
65 | }
66 | for (int j = 0; j < Q - 1; j++) {
67 | System.out.print(getMark(low, len_arr, query[j]) + " ");
68 | }
69 | System.out.println(getMark(low, len_arr, query[Q - 1]));
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0208.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 |
7 | public class Problem0208 {
8 | /*
9 | 有9个因数的数
10 | Description
11 | Find the count of numbers less than N having exactly 9 divisors
12 | 1<=T<=1000,1<=N<=10^12
13 |
14 | Input
15 | First Line of Input contains the number of testcases.
16 | Only Line of each testcase contains the number of members N in the rival gang.
17 |
18 | Output
19 | Print the desired output.
20 | */
21 |
22 | //9个因数的数应当满足以下条件之一
23 | // x = a^2 * b^2 (a != b) || x = a ^ 8
24 | //上式中a, b均为质数
25 | //由于当a,b超过sqrt(n)后,上式结果必然超过n,因此舍去这部分的素数
26 | public static int getNineDivisorsNum(long n, List primes) {
27 | int cnt = 0;
28 | //x = a^2 * b^2
29 | for (int i = 0; i < primes.size() - 1; i++) {
30 | for (int j = i + 1; j < primes.size(); j++) {
31 | if (Math.pow(primes.get(i), 2) * Math.pow(primes.get(j), 2) > n) {
32 | break;
33 | }
34 | cnt++;
35 | }
36 | }
37 | for (int i = 0; i < primes.size(); i++) {
38 | if (Math.pow(primes.get(i), 8) > n) {
39 | break;
40 | }
41 | cnt++;
42 | }
43 | return cnt;
44 | }
45 |
46 | public static List getPrime(long n) {
47 | List primes = new ArrayList<>();
48 | int size = (int) Math.sqrt(n) + 1;
49 | int[] isPrime = new int[size];
50 | for (int i = 2; i < size; i++) {
51 | if (isPrime[i] == 0) {
52 | primes.add(i);
53 | //将所有素数的倍数标记为合数
54 | for (int j = i * i; j < size; j += i) {
55 | isPrime[j] = 1;
56 | }
57 | }
58 | }
59 | return primes;
60 | }
61 |
62 | public static void main(String[] args) {
63 | Scanner scanner = new Scanner(System.in);
64 | int T = scanner.nextInt();
65 | for (int i = 0; i < T; i++) {
66 | long N = scanner.nextLong();
67 | List primeList = getPrime(N);
68 | System.out.println(getNineDivisorsNum(N, primeList));
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/SecondExercise/Problem0209.java:
--------------------------------------------------------------------------------
1 | package SecondExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0209 {
6 | /*
7 | 漆狗屋
8 | Description
9 | Dilpreet wants to paint his dog- Buzo's home that has n boards with different lengths[A1, A2,..., An].
10 | He hired k painters for this work and each painter takes 1 unit time to paint 1 unit of the board.
11 | The problem is to find the minimum time to get this job done under the constraints that any painter will only paint continuous sections of boards,
12 | say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}.
13 | Constraints:1<=T<=100,1<=k<=30,1<=n<=50,1<=A[i]<=500
14 |
15 | Input
16 | The first line consists of a single integer T, the number of test cases.
17 | For each test case, the first line contains an integer k denoting the number of painters and integer n denoting the number of boards.
18 | Next line contains n- space separated integers denoting the size of boards.
19 |
20 | Output
21 | For each test case, the output is an integer displaying the minimum time for painting that house.
22 | */
23 |
24 | public static int partition(int[] boards, int n, int k) {
25 | int max = Integer.MIN_VALUE;
26 | int sum = 0;
27 | for (int i = 0; i < n; i++) {
28 | max = Math.max(max, boards[i]);
29 | sum += boards[i];
30 | }
31 | int left = max, right = sum;
32 | while (left < right) {
33 | int mid = (left + right) / 2;
34 | if (getPainters(boards, mid) > k) {
35 | left = mid + 1;
36 | } else {
37 | right = mid;
38 | }
39 | }
40 | return left;
41 | }
42 |
43 | public static int getPainters(int[] boards, int cnt) {
44 | int temp = 0;
45 | int painters = 1;
46 | for (int i = 0; i < boards.length; i++) {
47 | temp += boards[i];
48 | if (temp > cnt) {
49 | temp = boards[i];
50 | painters++;
51 | }
52 | }
53 | return painters;
54 | }
55 |
56 | public static void main(String[] args) {
57 | Scanner scanner = new Scanner(System.in);
58 | while (scanner.hasNext()) {
59 | int T = scanner.nextInt();
60 | for (int i = 0; i < T; i++) {
61 | int k = scanner.nextInt();
62 | int n = scanner.nextInt();
63 | int[] boards = new int[n];
64 | for (int j = 0; j < n; j++) {
65 | boards[j] = scanner.nextInt();
66 | }
67 | System.out.println(partition(boards, n, k));
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/Test/Problem0101.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0101 {
6 | /*
7 | Description
8 | our task is to Calculate a + b. Too easy?! Of course! I specially designed the problem for acm beginners.
9 | You must have found that some problems have the same titles with this one,
10 | yes, all these problems were designed for the same aim
11 |
12 | Input
13 | The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
14 |
15 | Output
16 | For each pair of input integers a and b you should output the sum of a and b in one line,
17 | and with one line of output for each line in input.
18 | */
19 | public static void main(String[] args) {
20 | Scanner scanner = new Scanner(System.in);
21 | while (scanner.hasNext()) {
22 | String[] nums = scanner.nextLine().split(" ");
23 | int sum = 0;
24 | for (String num: nums) {
25 | sum += Integer.parseInt(num);
26 | }
27 | System.out.println(sum);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Test/Problem0102.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0102 {
6 | /*
7 | Description
8 | The first line integer means the number of input integer a and b. Your task is to Calculate a + b.
9 | Input
10 | Your task is to Calculate a + b. The first line integer means the numbers of pairs of input integers.
11 | Output
12 | For each pair of input integers a and b you should output the sum of a and b in one line,
13 | and with one line of output for each line in input.
14 | */
15 | public static void main(String[] args) {
16 | Scanner scanner = new Scanner(System.in);
17 | int n = Integer.parseInt(scanner.nextLine());
18 | for (int i = 0; i < n; i++) {
19 | String[] nums = scanner.nextLine().split(" ");
20 | int sum = 0;
21 | for (String num: nums) {
22 | sum += Integer.parseInt(num);
23 | }
24 | System.out.println(sum);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Test/Problem0103.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0103 {
6 | /*
7 | Description
8 | Your task is to Calculate a + b.
9 | Input
10 | Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line.
11 | A test case containing 0 0 terminates the input and this test case is not to be processed.
12 | Output
13 | For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
14 | */
15 | public static void main(String[] args) {
16 | Scanner scanner = new Scanner(System.in);
17 | String input = "";
18 | while (!(input = scanner.nextLine()).contains("0 0")) {
19 | int sum = 0;
20 | String[] nums = input.split(" ");
21 | for (String num: nums) {
22 | sum += Integer.parseInt(num);
23 | }
24 | System.out.println(sum);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Test/Problem0104.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0104 {
6 | /*
7 | Description
8 | Your task is to Calculate the sum of some integers.
9 | Input
10 | Input contains multiple test cases.
11 | Each test case contains a integer N, and then N integers follow in the same line.
12 | A test case starting with 0 terminates the input and this test case is not to be processed.
13 | Output
14 | For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
15 | */
16 | public static void main(String[] args) {
17 | Scanner scanner = new Scanner(System.in);
18 | int N = 0;
19 | while ((N = scanner.nextInt()) != 0) {
20 | int sum = 0;
21 | for (int i = 0; i < N; i++) {
22 | sum += scanner.nextInt();
23 | }
24 | System.out.println(sum);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Test/Problem0105.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0105 {
6 | /*
7 | Description
8 | Your task is to calculate the sum of some integers.
9 | Input
10 | Input contains an integer N in the first line, and then N lines follow.
11 | Each line starts with a integer M, and then M integers follow in the same line.
12 | Output
13 | For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
14 | */
15 | public static void main(String[] args) {
16 | Scanner scanner = new Scanner(System.in);
17 | int N = scanner.nextInt();
18 | for (int i = 0; i < N; i++) {
19 | int M = scanner.nextInt();
20 | int sum = 0;
21 | for (int j = 0; j < M; j++) {
22 | sum += scanner.nextInt();
23 | }
24 | System.out.println(sum);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Test/Problem0106.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0106 {
6 | /*
7 | Description
8 | Your task is to calculate the sum of some integers.
9 | Input
10 | Input contains multiple test cases, and one case one line.
11 | Each case starts with an integer N, and then N integers follow in the same line.
12 | Output
13 | For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.
14 | */
15 | public static void main(String[] args) {
16 | Scanner scanner = new Scanner(System.in);
17 | while (scanner.hasNext()) {
18 | int N = scanner.nextInt();
19 | int sum = 0;
20 | for (int i = 0; i < N; i++) {
21 | sum += scanner.nextInt();
22 | }
23 | System.out.println(sum);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Test/Problem0107.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0107 {
6 | /*
7 | Description
8 | Your task is to Calculate a + b.
9 | Input
10 | The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
11 | Output
12 | For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
13 | */
14 | public static void main(String[] args) {
15 | Scanner scanner = new Scanner(System.in);
16 | //boolean flag = true;
17 | while (scanner.hasNext()) {
18 | String[] nums = scanner.nextLine().split(" ");
19 | int sum = 0;
20 | for (String num: nums) {
21 | sum += Integer.parseInt(num);
22 | }
23 | System.out.println(sum);
24 | System.out.println();
25 | }
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Test/Problem0108.java:
--------------------------------------------------------------------------------
1 | package Test;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0108 {
6 | /*
7 | Description
8 | Your task is to calculate the sum of some integers
9 | Input
10 | Input contains an integer N in the first line, and then N lines follow.
11 | Each line starts with a integer M, and then M integers follow in the same line
12 | Output
13 | For each group of input integers you should output their sum in one line,
14 | and you must note that there is a blank line between outputs.
15 | */
16 | public static void main(String[] args) {
17 | Scanner scanner = new Scanner(System.in);
18 | int N = scanner.nextInt();
19 | for (int i = 0; i < N; i++) {
20 | int M = scanner.nextInt();
21 | int sum = 0;
22 | for (int j = 0; j < M; j++) {
23 | sum += scanner.nextInt();
24 | }
25 | System.out.println(sum);
26 | System.out.println();
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0301.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.List;
6 | import java.util.Scanner;
7 |
8 | public class Problem0301 {
9 | /*
10 | 最长公共子序列
11 | Description
12 | 给定两个字符串,返回两个字符串的最长公共子序列(不是最长公共子字符串),可能是多个。
13 |
14 | Input
15 | 输入第一行为用例个数, 每个测试用例输入为两行,一行一个字符串
16 |
17 | Output
18 | 如果没有公共子序列,不输出,如果有多个则分为多行,按字典序排序。
19 | */
20 |
21 | /**
22 | * 寻找两字符串的最长公共子序列,并打印
23 | * @param s1 字符串一
24 | * @param s2 字符串二
25 | */
26 | public static void findLCS(String s1, String s2) {
27 | int len1 = s1.length(), len2 = s2.length();
28 | int[][] dp = new int[len1 + 1][len2 + 1];
29 | for (int i = 1; i <= len1; i++) {
30 | for (int j = 1; j <= len2; j++) {
31 | if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
32 | dp[i][j] = dp[i - 1][j - 1] + 1;
33 | } else {
34 | dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
35 | }
36 | }
37 | }
38 | int maxLen = dp[len1][len2];
39 | List lcs = new ArrayList<>();
40 | for (int i = 1; i <= len1; i++) {
41 | for (int j = 1; j <= len2; j++) {
42 | if (dp[i][j] == maxLen) {
43 | getLCS(lcs, s1, s2, i, j, dp, "");
44 | }
45 | }
46 | }
47 | Collections.sort(lcs);
48 | for (String s: lcs) {
49 | System.out.println(s);
50 | }
51 | }
52 |
53 | /**
54 | *
55 | * @param list 存储最长公共子序列的集合
56 | * @param s1 字符串1
57 | * @param s2 字符串2
58 | * @param i 字符串1的索引位置
59 | * @param j 字符串2的索引位置
60 | * @param dp dp[i][j]表示s1前i位和s2前j位的最长公共子序列长度
61 | * @param s 记录最长公共子序列
62 | */
63 | public static void getLCS(List list, String s1, String s2, int i, int j, int[][] dp, String s) {
64 | if (i == 0 || j == 0) {
65 | if (!list.contains(s)) {
66 | list.add(s);
67 | }
68 | return;
69 | }
70 | if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
71 | s = s1.charAt(i - 1) + s;
72 | getLCS(list, s1, s2, i - 1, j - 1, dp, s);
73 | } else if (dp[i - 1][j] > dp[i][j - 1]) {
74 | getLCS(list, s1, s2, i - 1, j, dp, s);
75 | } else if (dp[i - 1][j] < dp[i][j - 1]) {
76 | getLCS(list, s1, s2, i, j - 1, dp, s);
77 | } else {
78 | getLCS(list, s1, s2, i - 1, j, dp, s);
79 | getLCS(list, s1, s2, i, j - 1, dp, s);
80 | }
81 | }
82 |
83 | public static void main(String[] args) {
84 | Scanner scanner = new Scanner(System.in);
85 | int t = Integer.parseInt(scanner.nextLine());
86 | for (int i = 0; i < t; i++) {
87 | String s1 = scanner.nextLine();
88 | String s2 = scanner.nextLine();
89 | findLCS(s1, s2);
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0302.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.PriorityQueue;
4 | import java.util.Scanner;
5 |
6 | public class Problem0302 {
7 | /*
8 | 区间第k最小
9 | Description
10 | 找到给定数组的给定区间内的第K小的数值。
11 |
12 | Input
13 | 输入第一行为用例个数, 每个测试用例输入的第一行为数组,每一个数用空格隔开;第二行是区间(第几个数到第几个数,两头均包含),两个值用空格隔开;第三行为K值。
14 |
15 | Output
16 | 结果。
17 | */
18 |
19 | public static int getKthSmallest(int[] nums, int start, int end, int k) {
20 | PriorityQueue pq = new PriorityQueue<>((o1, o2) -> (o2 - o1));
21 | for (int i = start; i <= end; i++) {
22 | pq.offer(nums[i]);
23 | }
24 | for (int i = 0; i < end - start + 1 - k; i++) {
25 | pq.poll();
26 | }
27 | return pq.peek();
28 | }
29 | public static void main(String[] args) {
30 | Scanner scanner = new Scanner(System.in);
31 | int t = Integer.parseInt(scanner.nextLine());
32 | for (int i = 0; i < t; i++) {
33 | String[] num_str = scanner.nextLine().split(" ");
34 | int[] nums = new int[num_str.length];
35 | for (int j = 0; j < num_str.length; j++) {
36 | nums[j] = Integer.parseInt(num_str[j]);
37 | }
38 | String[] index_str = scanner.nextLine().split(" ");
39 | int start = Integer.parseInt(index_str[0]) - 1;
40 | int end = Integer.parseInt(index_str[1]) - 1;
41 | int k = Integer.parseInt(scanner.nextLine());
42 | System.out.println(getKthSmallest(nums, start, end, k));
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0303.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0303 {
6 | /*
7 | 子矩阵问题
8 | Description
9 | 给定一个矩形区域,每一个位置上都是1或0,求该矩阵中每一个位置上都是1的最大子矩形区域中的1的个数。
10 |
11 | Input
12 | 输入第一行为测试用例个数。每一个用例有若干行,第一行为矩阵行数n和列数m,下面的n行每一行是用空格隔开的0或1。
13 |
14 | Output
15 | 输出一个数值。
16 | */
17 |
18 | public static int getMaxSubArea(int[][] matrix) {
19 | int n = matrix.length, m = matrix[0].length;
20 | //横向记录连续1的个数
21 | int[][] cnt = new int[n][m];
22 | for (int i = 0; i < n; i++) {
23 | for (int j = 0; j < m; j++) {
24 | if (j == 0) {
25 | cnt[i][j] = matrix[i][j];
26 | continue;
27 | }
28 | if (matrix[i][j] == 1) {
29 | cnt[i][j] = cnt[i][j - 1] + 1;
30 | }
31 | }
32 | }
33 | int res = 0;
34 | for (int i = 0; i < m; i++) {
35 | for (int j = 0; j < n; j++) {
36 | if (cnt[j][i] == 0) {
37 | continue;
38 | }
39 | int count = 0;
40 | int down = 0;
41 | while (j + down < n && cnt[j][i] <= cnt[j + down][i]) {
42 | count++;
43 | down++;
44 | }
45 | int up = 0;
46 | while (j - up >= 0 && cnt[j][i] <= cnt[j - up][i]) {
47 | count++;
48 | up++;
49 | }
50 | res = Math.max(res, cnt[j][i] * (count - 1));
51 | }
52 | }
53 | return res;
54 | }
55 |
56 | public static void main(String[] args) {
57 | Scanner scanner = new Scanner(System.in);
58 | int t = scanner.nextInt();
59 | for (int i = 0; i < t; i++) {
60 | int n = scanner.nextInt();
61 | int m = scanner.nextInt();
62 | int[][] matrix = new int[n][m];
63 | for (int j = 0; j < n; j++) {
64 | for (int k = 0; k < m; k++) {
65 | matrix[j][k] = scanner.nextInt();
66 | }
67 | }
68 | System.out.println(getMaxSubArea(matrix));
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0304.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 |
7 | public class Problem0304 {
8 | /*
9 | 能否成环
10 | Description
11 | Given an array of strings A[ ], determine if the strings can be chained together to form a circle.
12 | A string X can be chained together with another string Y if the last character of X is same as first character of Y.
13 | If every string of the array can be chained, it will form a circle.
14 | For example, for the array arr[] = {"for", "geek", "rig", "kaf"}
15 | the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf".
16 |
17 | Input
18 | The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.
19 | The first line of each test case contains a positive integer N, denoting the size of the array.
20 | The second line of each test case contains a N space seprated strings, denoting the elements of the array A[ ].
21 | 1 <= T; 0 < N; 0 < A[i]
22 |
23 | Output
24 | If chain can be formed, then print 1, else print 0.
25 | */
26 |
27 | public static boolean findCircle(List list, String[] strings, boolean[] isVisited, int index) {
28 | list.add(strings[index]);
29 | isVisited[index] = true;
30 |
31 | if (list.size() == strings.length) {
32 | String first = list.get(0);
33 | String last = list.get(list.size() - 1);
34 | //成环要求最后一个字符串的末尾字符和第一个字符串的首字符相同
35 | return first.charAt(0) == last.charAt(last.length() - 1);
36 | }
37 |
38 | char c = strings[index].charAt(strings[index].length() - 1);
39 | for (int i = 0; i < strings.length; i++) {
40 | String s = strings[i];
41 | if (!isVisited[i] && s.charAt(0) == c) {
42 | if (findCircle(list, strings, isVisited, i)) {
43 | return true;
44 | }
45 | }
46 | }
47 | list.remove(list.size() - 1);
48 | isVisited[index] = false;
49 | return false;
50 | }
51 |
52 | public static void main(String[] args) {
53 | Scanner scanner = new Scanner(System.in);
54 | int t = Integer.parseInt(scanner.nextLine());
55 | for (int i = 0; i < t; i++) {
56 | int n = Integer.parseInt(scanner.nextLine());
57 | String[] strings = scanner.nextLine().split(" ");
58 | List list = new ArrayList<>();
59 | boolean[] isVisited = new boolean[n];
60 | if (findCircle(list, strings, isVisited, 0)) {
61 | System.out.println(1);
62 | } else {
63 | System.out.println(0);
64 | }
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0305.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.List;
6 | import java.util.Scanner;
7 |
8 | public class Problem0305 {
9 | /*
10 | 数字重组整除问题
11 | Description
12 | Babul’s favourite number is 17. He likes the numbers which are divisible by 17.
13 | This time what he does is that he takes a number N and tries to find the largest number which is divisible by 17, by rearranging the digits.
14 | As the number increases he gets puzzled with his own task.
15 | So you as a programmer have to help him to accomplish his task.
16 | Note: If the number is not divisible by rearranging the digits, then print “Not Possible”. N may have leading zeros.
17 |
18 | Input
19 | The first line of input contains an integer T denoting the no of test cases. Each of the next T lines contains the number N.
20 |
21 | Output
22 | For each test case in a new line print the desired output.
23 | */
24 |
25 | public static void permute(List list, char[] nums, StringBuilder sb, boolean[] isVisited) {
26 | if (sb.length() == nums.length) {
27 | list.add(Integer.parseInt(sb.toString()));
28 | return;
29 | }
30 | for (int i = 0; i < nums.length; i++) {
31 | if (!isVisited[i]) {
32 | sb.append(nums[i]);
33 | isVisited[i] = true;
34 | permute(list, nums, sb, isVisited);
35 | isVisited[i] = false;
36 | sb.deleteCharAt(sb.length() - 1);
37 | while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
38 | i++;
39 | }
40 | }
41 | }
42 | }
43 |
44 | public static void main(String[] args) {
45 | Scanner scanner = new Scanner(System.in);
46 | int t = Integer.parseInt(scanner.nextLine());
47 | for (int i = 0; i < t; i++) {
48 | char[] nums = scanner.nextLine().toCharArray();
49 | Arrays.sort(nums);
50 | List list = new ArrayList<>();
51 | boolean[] isVisited = new boolean[nums.length];
52 | //全排列
53 | permute(list, nums, new StringBuilder(), isVisited);
54 | int max = 0;
55 | for (Integer num: list) {
56 | if (num % 17 == 0 && num > max) {
57 | max = num;
58 | }
59 | }
60 |
61 | if (max == 0) {
62 | System.out.println("Not Possible");
63 | } else {
64 | System.out.println(max);
65 | }
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0306.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0306 {
6 | /*
7 | 牛的繁殖问题
8 | Description
9 | Cows in the FooLand city are interesting animals. One of their specialties is related to producing offsprings.
10 | A cow in FooLand produces its first calve (female calf) at the age of two years and proceeds to produce other calves (one female calf a year).
11 | Now the farmer Harold wants to know how many animals would he have at the end of N years,
12 | if we assume that none of the calves die, given that initially, he has only one female calf?
13 | explanation:At the end of 1 year, he will have only 1 cow, at the end of 2 years he will have 2 animals
14 | (one parent cow C1 and other baby calf B1 which is the offspring of cow C1).
15 | At the end of 3 years, he will have 3 animals (one parent cow C1 and 2 female calves B1 and B2, C1 is the parent of B1 and B2).
16 | At the end of 4 years, he will have 5 animals (one parent cow C1, 3 offsprings of C1 i.e. B1, B2, B3 and one offspring of B1).
17 |
18 | Input
19 | The first line contains a single integer T denoting the number of test cases.
20 | Each line of the test case contains a single integer N as described in the problem.
21 |
22 | Output
23 | For each test case print in new line the number of animals expected at the end of N years modulo 10^9 + 7.
24 | */
25 |
26 | //这题不就是斐波那契数列
27 | //矩阵快速幂的方式(动态规划的方式会超时)
28 | //参考链接:https://leetcode-cn.com/problems/climbing-stairs/solution/pa-lou-ti-by-leetcode-solution/
29 | public static long getNumOfAnimals(long n) {
30 | long[][] q = {{1, 1}, {1, 0}};
31 | long[][] res = {{1, 0}, {0, 1}};
32 | while (n > 0) {
33 | if ((n & 1) == 1) {
34 | res = multiply(res, q);
35 | }
36 | n >>= 1;
37 | q = multiply(q, q);
38 | }
39 | return res[0][0] % 1000000007;
40 | }
41 |
42 | public static long[][] multiply(long[][] a, long[][] b) {
43 | long[][] c = new long[2][2];
44 | for (int i = 0; i < 2; i++) {
45 | for (int j = 0; j < 2; j++) {
46 | c[i][j] = (a[i][0] * b[0][j] + a[i][1] * b[1][j]) % 1000000007;
47 | }
48 | }
49 | return c;
50 | }
51 |
52 | public static void main(String[] args) {
53 | Scanner scanner = new Scanner(System.in);
54 | int t = Integer.parseInt(scanner.nextLine());
55 | for (int i = 0; i < t; i++) {
56 | long n = Long.parseLong(scanner.nextLine());
57 | System.out.println(getNumOfAnimals(n));
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0307.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0307 {
6 | /*
7 | 和最大的连续降序字符
8 | Description
9 | Archana is very fond of strings. She likes to solve many questions related to strings.
10 | She comes across a problem which she is unable to solve. Help her to solve.
11 | The problem is as follows: Given is a string of length L.
12 | Her task is to find the longest string from the given string with characters arranged in descending order of their ASCII code and in arithmetic progression.
13 | She wants the common difference should be as low as possible(at least 1) and the characters of the string to be of higher ASCII value.
14 |
15 | Input
16 | The first line of input contains an integer T denoting the number of test cases. Each test contains a string s of lengthL.
17 | 1<= T <= 100; 3<= L <=1000; A<=s[i]<=Z
18 | The string contains minimum three different characters.
19 |
20 | Output
21 | For each test case print the longest string.Case 1:Two strings of maximum length are possible- “CBA” and “RPQ”.
22 | But he wants the string to be of higher ASCII value therefore, the output is “RPQ”.Case 2:The String of maximum length is “JGDA”.
23 | */
24 |
25 | public static String getMaxSumSequence(String s) {
26 | char[] arr = s.toCharArray();
27 | boolean[] flag = new boolean[26];
28 | for (char c: arr) {
29 | flag[c - 'A'] = true;
30 | }
31 | int maxLen = 0;
32 | String maxSumStr = "";
33 | for (int i = 0; i < flag.length; i++) {
34 | //若该字符在字符串中,则+1, +2, +3, ..., +26寻找下一个字符,循环构建字符序列
35 | if (flag[i]) {
36 | int step = 1;
37 | while (step + i < flag.length) {
38 | int len = 0;
39 | String temp = "";
40 | for (int j = i; j < flag.length; j += step) {
41 | if (flag[j]) {
42 | len++;
43 | temp = (char) (j + 'A') + temp;
44 | } else {
45 | break;
46 | }
47 | }
48 | //选取更长者,或当两字符串相等时,选取首位ASCII码更大者
49 | if (len > maxLen || (len == maxLen && temp.charAt(0) > maxSumStr.charAt(0))) {
50 | maxLen = len;
51 | maxSumStr = temp;
52 | }
53 | step++;
54 | }
55 | }
56 | }
57 | return maxSumStr;
58 | }
59 |
60 | public static void main(String[] args) {
61 | Scanner scanner = new Scanner(System.in);
62 | int t = scanner.nextInt();
63 | for (int i = 0; i < t; i++) {
64 | String s = scanner.next();
65 | System.out.println(getMaxSumSequence(s));
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0308.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.Arrays;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 | import java.util.Scanner;
7 |
8 | public class Problem0308 {
9 | /*
10 | 距离问题
11 | Description
12 | In a given cartesian plane, there are N points. We need to find the Number of Pairs of points(A,B) such that
13 | Point A and Point B do not coincide.
14 | Manhattan Distance and the Euclidean Distance between the points should be equal.
15 | Note : Pair of 2 points(A,B) is considered same as Pair of 2 points(B,A).
16 | Manhattan Distance = |x2-x1|+|y2-y1|
17 | Euclidean Distance = ((x2-x1)^2 + (y2-y1)^2)^0.5 where points are (x1,y1) and (x2,y2).
18 | Constraints:1<=T <= 50, 1<=N <= 2*10 ^ 5, 0<=(|Xi|, |Yi|) <= 10^9
19 |
20 | Input
21 | First Line Consist of T - number of test cases. For each Test case:First Line consist of N , Number of points.
22 | Next line contains N pairs contains two integers Xi and Yi,i.e, X coordinate and the Y coordinate of a Point
23 |
24 | Output
25 | Print the number of pairs as asked above.
26 | */
27 |
28 | //欧式距离 == 曼哈顿距离 即 A, B两点的横坐标相等或纵坐标相等
29 | //又要求A, B两点不在同一位置,所以还需减去横纵坐标均相等的点
30 | public static int getNumOfPoints(int[][] points) {
31 | Map xMap = new HashMap<>();
32 | Map yMap = new HashMap<>();
33 | Map pointMap = new HashMap<>();
34 | for (int[] point : points) {
35 | xMap.put(point[0], xMap.getOrDefault(point[0], 0) + 1);
36 | yMap.put(point[1], yMap.getOrDefault(point[1], 0) + 1);
37 | pointMap.put(Arrays.toString(point), pointMap.getOrDefault(Arrays.toString(point), 0) + 1);
38 | }
39 |
40 | int xCnt = 0, yCnt = 0, pCnt = 0;
41 | for (Map.Entry entry: xMap.entrySet()) {
42 | int cnt = entry.getValue();
43 | xCnt += cnt * (cnt - 1) / 2;
44 | }
45 | for (Map.Entry entry: yMap.entrySet()) {
46 | int cnt = entry.getValue();
47 | yCnt += cnt * (cnt - 1) / 2;
48 | }
49 | for (Map.Entry entry: pointMap.entrySet()) {
50 | int cnt = entry.getValue();
51 | pCnt += cnt * (cnt - 1) / 2;
52 | }
53 | //重合点对在横轴纵轴均算了一遍
54 | return xCnt + yCnt - pCnt * 2;
55 | }
56 |
57 | public static void main(String[] args) {
58 | Scanner scanner = new Scanner(System.in);
59 | int t = Integer.parseInt(scanner.nextLine());
60 | for (int i = 0; i < t; i++) {
61 | int n = Integer.parseInt(scanner.nextLine());
62 | int[][] points = new int[n][2];
63 | for (int j = 0; j < n; j++) {
64 | String[] point_str = scanner.nextLine().split(" ");
65 | points[j][0] = Integer.parseInt(point_str[0]);
66 | points[j][1] = Integer.parseInt(point_str[1]);
67 | }
68 | System.out.println(getNumOfPoints(points));
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0309.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Scanner;
6 |
7 | public class Problem0309 {
8 | /*
9 | 字符串匹配问题
10 | Description
11 | Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[])
12 | that prints all occurrences of pat[] in txt[]. You may assume that n > m.
13 |
14 | Input
15 | 输入第一行是用例个数,后面一行表示一个用例;用例包括两部分,第一部分为给定文本,第二部分为搜索串,两部分使用","隔开。
16 |
17 | Output
18 | 每一个用例输出一行,每行按照找到的位置先后顺序排列,使用空格隔开。
19 | */
20 |
21 | public static List findPatInTxt(String txt, String pat) {
22 | char[] t = txt.toCharArray();
23 | char[] p = pat.toCharArray();
24 | int[] next = getNextArr(p);
25 | List indexes = new ArrayList<>();
26 | int i = 0, j = 0;
27 | while (i < txt.length()) {
28 | if (j == -1 || t[i] == p[j]) {
29 | i++;
30 | j++;
31 | } else {
32 | j = next[j];
33 | }
34 | if (j == pat.length()) {
35 | indexes.add(i - j);
36 | j = next[j];
37 | }
38 | }
39 | return indexes;
40 | }
41 |
42 | public static int[] getNextArr(char[] a) {
43 | int len = a.length;
44 | int[] next = new int[len + 1];
45 | next[0] = -1;
46 | int k = -1, i = 0;
47 | while (i < len) {
48 | if (k == -1 || a[k] == a[i]) {
49 | i++;
50 | k++;
51 | if (i != len && a[i] == a[k]) {
52 | next[i] = next[k];
53 | } else {
54 | next[i] = k;
55 | }
56 | } else {
57 | k = next[k];
58 | }
59 | }
60 | return next;
61 | }
62 |
63 | public static void main(String[] args) {
64 | Scanner scanner = new Scanner(System.in);
65 | int t = Integer.parseInt(scanner.nextLine());
66 | for (int i = 0; i < t; i++) {
67 | String[] s = scanner.nextLine().split(",");
68 | String txt = s[0];
69 | String pat = s[1];
70 | List indexes = findPatInTxt(txt, pat);
71 | if (indexes.size() == 0) {
72 | System.out.println();
73 | continue;
74 | }
75 | for (int j = 0; j < indexes.size() - 1; j++) {
76 | System.out.print(indexes.get(j) + " ");
77 | }
78 | System.out.println(indexes.get(indexes.size() - 1));
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0310.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0310 {
6 | /*
7 | 整除查询
8 | Description
9 | Given an array of positive integers and many queries for divisibility.
10 | In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K.
11 | Constraints:1<=T<=1001<=N,M<=1051<=A[i],Q[i]<=105
12 |
13 | Input
14 | The first line of input contains an integer T denoting the number of test cases.
15 | Then T test cases follow. Each test case consists of three lines.
16 | First line of each test case contains two integers N & M, second line contains N space separated array elements and third line contains M space separated queries.
17 |
18 | Output
19 | For each test case,In new line print the required count for each query Q[i].
20 | */
21 |
22 | public static int[] getRes(int[] a, int[] q) {
23 | int[] res = new int[q.length];
24 | for (int i = 0; i < q.length; i++) {
25 | for (int j = 0; j < a.length; j++) {
26 | if (a[j] % q[i] == 0) {
27 | res[i]++;
28 | }
29 | }
30 | }
31 | return res;
32 | }
33 |
34 | public static void main(String[] args) {
35 | Scanner scanner = new Scanner(System.in);
36 | int t = scanner.nextInt();
37 | for (int i = 0; i < t; i++) {
38 | int n = scanner.nextInt();
39 | int m = scanner.nextInt();
40 | int[] a = new int[n];
41 | int[] q = new int[m];
42 | for (int j = 0; j < n; j++) {
43 | a[j] = scanner.nextInt();
44 | }
45 | for (int j = 0; j < m; j++) {
46 | q[j] = scanner.nextInt();
47 | }
48 | int[] res = getRes(a, q);
49 | for (int j = 0; j < res.length - 1; j++) {
50 | System.out.print(res[j] + " ");
51 | }
52 | System.out.println(res[res.length - 1]);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/ThirdExercise/Problem0311.java:
--------------------------------------------------------------------------------
1 | package ThirdExercise;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Problem0311 {
6 | /*
7 | 对称子字符串
8 | Description
9 | Given a string ‘str’ of digits, find length of the longest substring of ‘str’,
10 | such that the length of the substring is 2k digits and sum of left k digits is equal to the sum of right k digits.
11 |
12 | Input
13 | 输入第一行是测试用例的个数,后面每一行表示一个数字组成的字符串,例如:"123123"
14 |
15 | Output
16 | 输出找到的满足要求的最长子串的长度。例如,给定的例子长度应该是 6。每行对应一个用例的结果。
17 | */
18 |
19 | public static int getMaxSubLength(String s) {
20 | int[] nums = new int[s.length()];
21 | for (int i = 0; i < s.length(); i++) {
22 | nums[i] = s.charAt(i) - '0';
23 | }
24 | int maxLen = 0;
25 | for (int i = 0; i < s.length(); i++) {
26 | int leftSum = 0, rightSum = 0;
27 | int left = i, right = i + 1;
28 | //两边扩散
29 | while (left >= 0 && right < s.length()) {
30 | leftSum += nums[left];
31 | rightSum += nums[right];
32 | if (leftSum == rightSum) {
33 | maxLen = Math.max(maxLen, right - left + 1);
34 | }
35 | left--;
36 | right++;
37 | }
38 | }
39 | return maxLen;
40 | }
41 |
42 | public static void main(String[] args) {
43 | Scanner scanner = new Scanner(System.in);
44 | int t = Integer.parseInt(scanner.nextLine());
45 | for (int i = 0; i < t; i++) {
46 | String s = scanner.nextLine();
47 | System.out.println(getMaxSubLength(s));
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------