CF1667A

一个位置,要不一直加,要不一直减。

枚举最终 $b_i = 0$ 的 $i$,

那么小于 $i$ 的位置都为负数,大于 $i$ 的位置都为正数,

既然“加”还是“减”的方向都定了,那么就容易了。

思路有了,最好还是自己独立写一写。

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

const int N = 5010;

int a[N];

int main() {
    int n; scanf ("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf ("%d", &a[i]);
    }
    long long ans = 1e18;
    for (int i = 1; i <= n; ++i) {
        long long use = 0; // use 表示需要“加”“减”多少次
        long long now = 0;
        for (int j = i - 1; j >= 1; --j) {
            long long t = (now + 1) / a[j]; // 选取一个最小的t,得到的结果大于前一个数。
            if (t * a[j] < now + 1) t ++;
            use += t; now = 1ll * t * a[j];
        }
        now = 0;
        for (int j = i + 1; j <= n; ++j) {
            long long t = (now + 1) / a[j];
            if (t * a[j] < now + 1) t ++;
            use += t; now = 1ll * t * a[j];
        }
        ans = min (ans, use);
    }
    printf ("%lld", ans);
    return 0;
} 
posted @ 2022-04-20 16:03  wangzhongyuan  阅读(2)  评论(0编辑  收藏  举报  来源