首页 > 其他 > 详细

NOIP 2018 摆渡车

时间:2019-08-30 22:51:27      阅读:68      评论:0      收藏:0      [点我收藏+]

技术分享图片

技术分享图片
送上AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 505

int n, m, ans = inf;
int t[N], s[N], f[N][N];

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", t + i);
    sort(t + 1, t + n + 1);
    for (int i = 1; i <= n; i++)
        s[i] = s[i - 1] + t[i];
    t[0] = -inf;
    memset(f, 0x3f, sizeof f);
    f[0][0] = 0;
    for (int i = 0; i <= n; i++) {
        int mj = min(m - 1, t[i + 1] - t[i]);
        for (int j = 0; j <= mj; j++)
            if (f[i][j] != inf)
                for (int k = 1; i + k <= n; k++) {
                    int now = max(t[i] + j + m - t[i + k], 0);
                    f[i + k][now] = min(f[i + k][now],
                        f[i][j] + (t[i + k] + now) * k - (s[i + k] - s[i]));
                }
    }
    for (int i = 0; i < m; i++)
        ans = min(ans, f[n][i]);
    printf("%d\n", ans);

    return 0;
}

NOIP 2018 摆渡车

原文:https://www.cnblogs.com/zi-nai-boboyang/p/11437165.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!