ABC085B - Kagami Mochi

GitHub跳转原题关系图返回列表

统计不同直径的个数即最大层数。

OJ: atcoder

题目 ID: abc085_b

难度:入门

标签:贪心排序c++haskell

日期: 2026-07-10 16:21

题意

下层饼必须比上层饼直径大,每个饼只能用一次。求最多能叠多少层。

思路

每个直径只能出现一次,所以统计不同直径的个数就是答案。

代码

cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-07-10 16:21
 * update_at: 2026-07-10 16:21
 */
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;
    set<int> s;
    for (int i = 0; i < n; i++) { int x; cin >> x; s.insert(x); }
    cout << s.size() << '\n';

    return 0;
}
haskell
{-
 Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 rainboy的学习导航网站: https://idx.roj.ac.cn
 create_at: 2026-07-10 16:21
 update_at: 2026-07-10 16:21
-}
import Data.List (sort, group)

main = do
    input <- getContents
    let (_:xs) = map read (lines input) :: [Int]
    print $ length (group (sort xs))

复杂度

时间复杂度 O(NlogN)O(N \log N),空间复杂度 O(N)O(N)

总结

去重计数即可。