1 solutions

  • 0
    @ 2025-6-4 12:36:44

    这个问题可以通过贪心来解决,具体步骤如下: 排序:首先将每轮的扫雷币花费和轮次编号按照花费从小到大排序,如果花费相同则按轮次编号从大到小排序,按排序后的顺序遍历每一轮,维护两个变量:当前处理到的最大轮次p和当前拥有的扫雷币数量coin,对于每一轮,如果其轮次编号大于当前处理到的最大轮次p,则更新p为当前轮次编号,并计算可以购买的探测器数量。 通过贪心,优先选择花费低的轮次购买探测器,从而最大化探测器的购买数量。

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define rep(i, a, b) for(int i=a; i<=b; ++i)
    #define rep_(i, a, b) for(int i=a; i>=b; --i)
    #define PII pair<int, int>
    #define tul array<int, 3>
    const int N = 2e5+10;
    int n, res;
    struct node{
        int c, i;
    
        friend bool operator< (node A, node B){
            if(A.c == B.c) return A.i > B.i;
            return A.c < B.c;
        }
    
    }e[N];
    
    void solve()
    {
        cin >> n;
        rep(i, 1, n){
            int c;
            cin >> c;
            e[i] = {c, i};
        }
    
        sort(e+1, e+1+n);
    
        int p = 0;
        int coin = 0;
        rep(i, 1, n){
            int c = e[i].c, j = e[i].i;
            if(j > p){
                p = j;
                int t = (j-coin)/c;
                res += t;
                coin += t*c;
            }
        }
    
        cout << res << endl;
    }
    
    signed main()
    {
        int _ = 1;
        while(_--){
            solve();
        }
    
        return 0;
    }
    
    • 1

    Information

    ID
    1494
    Time
    2000ms
    Memory
    512MiB
    Difficulty
    9
    Tags
    (None)
    # Submissions
    180
    Accepted
    8
    Uploaded By