SCU3037 Painting the Balls

本文介绍了一种通过动态规划解决特定染色问题的方法。给定一系列球和染料成本,目标是最小化染色总成本,同时确保每M个连续球中至少有两个黑色球。文章详细介绍了状态定义、转移方程及前缀优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Petya puts the \(N\) white balls in a line and now he wants to paint some of them in black, so that at least two black balls could be found among any \(M\) successive balls.
Petya knows that he needs \(C_i\) milliliters of dye exactly to paint the \(i\)-th ball.
Your task is to find out for Petya the minimum amount of dye he will need to paint the balls.

Input

The first line contains two integer numbers \(N and M (2<=N<=10000, 2<=M<=100, M<=N)\).
The second line contains \(N\) integer numbers \(C_1, C_2, ..., C_N (1 \le C_i \le10000)\).

Output

Output only one integer number - the minimum amount of dye Petya will need (in milliliters).

Sample Input

6 3
1 5 6 2 1 3

Sample Output

9

\(f[i][j]\)表示最后一个黑球在\(i\)倒数第二个黑球在\(i-j\)的最小值。
转移方程\[f[i][j] = \min(f[j][1 \sim m-j])+C_i\]
前缀优化,另\(g[i][j] = \min(f[i][1 \sim j])\)。复杂度\(O(NM)\)

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

const int maxn = 10010,maxm = 110,inf = 1<<30;
int N,M,C[maxn],f[maxn][maxm],g[maxn][maxm],ans = inf;

inline int gi()
{
    char ch; int ret = 0,f = 1;
    do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
    if (ch == '-') f = -1,ch = getchar();
    do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
    return ret*f;
}

int main()
{
    freopen("3037.in","r",stdin);
    freopen("3037.out","w",stdout);
    N = gi(); M = gi();
    for (int i = 1;i <= N;++i) C[i] = gi();
    for (int i = 0;i <= N;++i) for (int j = 0;j <= M;++j) g[i][j] = f[i][j] = inf;
    for (int i = 1;i <= M;++i) for (int j = 1;j < i;++j) f[i][j] = C[i]+C[i-j],g[i][j] = min(g[i][j-1],f[i][j]);
    for (int i = M+1;i <= N;++i) for (int j = 1;j < M;++j) f[i][j] = g[i-j][M-j]+C[i],g[i][j] = min(g[i][j-1],f[i][j]);
    for (int i = 1;i <= M;++i) for (int j = 1;j < i;++j) ans = min(ans,f[N-M+i][j]);
    cout << ans << endl;
    fclose(stdin); fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/mmlz/p/6403852.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值