#BS0000329. Xếp bánh mochi thành cặp (Simultaneous Kagamimochi)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xếp bánh mochi thành cặp (Simultaneous Kagamimochi)
Simultaneous Kagamimochi
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
There are mochi rice cakes in nondecreasing order of size; cake has size .
A kagamimochi is a stack of two cakes, with a cake of size placed on one of size exactly when . Each cake may be used in at most one stack, whether as its top or bottom.
Determine the maximum number of stacks that can be formed simultaneously.
Input
The first line contains integer , the number of cakes.
The second line contains integers in nondecreasing order, their sizes.
Output
Print one nonnegative integer: the maximum number of stacks.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; ; at most two distinct sizes.
- Subtask 3 (50 points): ; ; .
Examples
Input 1
6
2 3 4 4 7 10
Output 1
3
Explanation
Create stacks with top and bottom sizes (2,4), (3,7), (4,10), using six distinct cakes. Four stacks would require eight cakes, so three is optimal.

Input 2
3
5 6 7
Output 2
0
Explanation
Twice the smallest size 5 is 10, greater than the largest size 7. No valid stack exists.
Input 3
3
387 388 389
Output 3
0
Explanation
Twice the smallest size is 774, exceeding the largest size 389. No pair is valid.
Input 4
24
307 321 330 339 349 392 422 430 477 481 488 537 541 571 575 602 614 660 669 678 712 723 785 792
Output 4
6
Explanation
Use pairs (307,614), (321,660), (330,669), (339,678), (349,712), (392,785). The largest bottom size is 792, so every top must have size at most 396. Only six cakes meet that bound, proving optimality.