4 条题解

  • 0
    @ 2025-11-4 19:41:05
    #include <stdio.h>
    #include <stdlib.h>
    #define min(a, b) ((a) < (b) ? (a) : (b))
    long long a[200009], b[200009];
    int main() {
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
         {
            scanf("%lld", &a[i]);
        }
        b[n] = 1e18;
        //b[i]表示第i到第n中最低的价格,i=n的时候,第n的价格和自己做比较
        //肯定还是自己,为了防止a[n]>b[n]导致无法取到正常的a[n],我们把
        //b[n]给个很大很大的值
        //现在我们知道b[i+1]的意思是从第i+1到n中最小的值
        //所以b[i]只需要让b[i+1]和a[i]做比较就好
        for(int i = n-1; i >= 0; i--)
         {
            b[i] = min(b[i+1], a[i]);
        }
        long long coins = 0;
        long long totalcost = 0;
        int ans = 0;
        for(int i = 0; i < n; i++)
         {
            coins++;//买不买先把每天的钱领了
            if(totalcost > i + 1)//i从0开始,i+1是总钱数
             {
                break;
            }
            if(a[i] == b[i])//很合适的价格,再往后没有比他更便宜的了
             {
                long long buy = coins / a[i];
                if(buy > 0) 
                {
                    ans += buy;
                    long long cost = buy * a[i];
                    coins -= cost;
                    totalcost += cost;
                }
            }
        }
        
        printf("%d\n", ans);
        return 0;
    }
    

    信息

    ID
    1179
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    217
    已通过
    40
    上传者