比赛 20241126 评测结果 AWWWAAAAAA
题目名称 小b爱取模 最终得分 70
用户昵称 darkMoon 运行时间 4.063 s
代码语言 C++ 内存使用 36.92 MiB
提交时间 2024-11-26 10:11:35
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
auto IN = freopen("changgao_modulo.in", "r", stdin);
auto OUT = freopen("changgao_modulo.out", "w", stdout);
auto mread = [](){int x;scanf("%d", &x);return x;};
const int N = 1e7 + 5, M = 5;
int m, a[N], ans = INT_MAX, n, f[2][M + 5];
char s[N];
signed main(){
    cin >> m;
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for(int i = 1; i <= n; i ++){
        a[i] = m - (s[i] - '0');
        if(s[i] == '0'){
            a[i] = 0;
        }
    }
    memset(f, 0x3f, sizeof(f));
    f[0][0] = 0;
    for(int i = 1; i <= n; i ++){
        for(int j = 0; j <= M; j ++){
            f[i & 1][j] = 0x3f3f3f3f;
            for(int k = 0; k <= M; k ++){
                int cost = f[i - 1 & 1][k];
                if(a[i] + m * j > a[i - 1] + m * k){
                    cost += (a[i] + m * j) - (a[i - 1] + m * k);
                }
                f[i & 1][j] = min(f[i & 1][j], cost);
            }
        }
    }
    for(int j = 0; j <= M; j ++){
        ans = min(ans, f[n & 1][j]);
    }
    printf("%d", ans);
    return 0;
}