top1编程
← 返回题目
题解

小鱼比可爱

1 条题解

  • 0
    @ 2026-10-20 19:12:28

    1. 解题思路

    **核心方法:**对每只鱼枚举它左边的鱼,统计可爱程度严格小于自己的数量。

    2. 坑点和注意事项

    • 相同可爱程度不能计入,也不能统计右边的鱼;数组100格足够,int足够。
    /*
    思路:对每只鱼枚举它左边的鱼,统计可爱程度严格小于自己的数量。
    关键规则与坑点:相同可爱程度不能计入,也不能统计右边的鱼;数组100格足够,int足够。
    时间复杂度:O(n²)。空间复杂度:O(n)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        // 读取题目给定数据。
        cin >> n;
        int a[101];
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (int i = 0; i < n; i++)
        {
            int count = 0;
            for (int j = 0; j < i; j++)
            {
                // 对每只鱼枚举它左边的鱼,统计可爱程度严格小于自己的数量。
                count += a[j] < a[i];
            }
            cout << (i ? " " : "") << count;
        }
        cout << "\n";
    }
    

    3. 复杂度

    • **时间复杂度:**O(n²)。
    • **空间复杂度:**O(n)。
    • 1