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;
}

SCU3037 Painting the Balls的更多相关文章

  1. SGU 183. Painting the balls( dp )

    dp..dp(i, j)表示画两个点为i-j, i的最优答案. dp(i, j) = min{ dp(i-j, k) } + cost[i] (1≤k≤M-j) 令f(i, j) = min{dp(i ...

  2. sgu 183. Painting the balls 动态规划 难度:3

    183. Painting the balls time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard ...

  3. SGU 183 Painting the balls (优化的动态规划)

    题意:给n个白球,选其中一些涂为黑色,且给了涂第i个球的花费为ci,要求每m个连续的球中至少有两个黑球,问最小花费是多少? 容易想到一个方程dp[i][j]=min{dp[k][i]}+c[j] dp ...

  4. SGU 183.Painting the balls

    时间限制:0.25s 空间限制:4M 题意:  在n(n<=10000)个球中,给若干个球涂色,每个球涂色的代价为Ci,使得任意连续m(m<=100)个球中有至少两个球被涂了色. Solu ...

  5. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  6. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  7. Codeforces Gym 100015B Ball Painting 找规律

    Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...

  8. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  9. 13 Balls Problem

    今天讨论的是称球问题. No.3 13 balls problem You are given 13 balls. The odd ball may be either heavier or ligh ...

随机推荐

  1. php成绩排序

    $arr = ['12','12','23']; $arr = $arr; $arr1=$arr; rsort($arr1); $c=[]; foreach ( $arr as $k=>$v){ ...

  2. Yaf学习(一)----Linux安装Yaf

    1.简介 Yaf,全称 Yet Another Framework,是一个高性能的PHP开发框架,采用PHP扩展实现(c语言).Blablablabla....... 2.环境 2.1 虚拟机 虚拟机 ...

  3. YII2.0学习一 Advanced 模板安装

    下载github上的完事安装包(本机环境使用Composer安装非常慢) https://github.com/yiisoft/yii2-app-advanced 解压到文件目录 wwwroot/sh ...

  4. Cython中def,cdef,cpdef的区别

    这是我的第一篇翻译,希望大家多多给出意见和建议. 如有转载,请注明出处. 原文来自:https://stackoverflow.com/questions/28362009/definition-of ...

  5. 一、Linux知识体系结构图

    参考: https://blog.csdn.net/Swing_Liu/article/details/79202479

  6. HDU暑假多校第八场G-Card Game

    一.题意 给出N个卡牌,卡牌的正反两面具有两个数字,取值范围为[1,2*n],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反 ...

  7. x86的控制寄存器CR0,CR1,CR2,CR3

    状态和控制寄存器组除了EFLAGS.EIP ,还有四个32位的控制寄存器,它们是CR0,CR1,CR2和CR3. 这几个寄存器中保存全局性和任务无关的机器状态. CR0中包含了6个预定义标志,0位是保 ...

  8. itop-4412开发板使用第一篇-信号量的学习使用

    1. 本次基于itop-4412研究下Linux信号量的使用方法. 2. 创建信号量的函数,信号量的头文件在那个路径?编译应用程序的话,头文件有3个路径,内核源码头文件,交叉编译器头文件,ubuntu ...

  9. JMeter学习笔记(九) 参数化4--User Variables

    4.User Variables 用户参数 1)线程组右键添加 -> 前置处理器 -> 用户参数 2)配置用户参数 3)添加HTTP请求,引用用户参数,格式: ${} 4)配置线程数 5) ...

  10. 8.0 TochAction各种用法

    1.滑动---TouchAction  支持相对坐标.绝对坐标.Element 注意看顶部的导入TouchAction这个库.. #实例化 action = TouchAction(driver) # ...