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-5.6.26源代码 - include_once、require_once、include、require、eval 的opcode处理器

    # ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER 实现在文件 php-\Zend\zend_vm_execute.h static int ZEND_FASTCALL ...

  2. 程序设计的SOLID原则

    要想设计一个良好的程序,建议采用SOLID原则,若考虑了SOLID,可以使程序在模块内具有高内聚.而模块间具有低耦合的特点. SOLID原则包括5方面的内容: S---单责任原则(SRP) 一个模块只 ...

  3. python3 练习题100例 (二十二)输入两个字符串,输出两个字符串集合的并集

    题目内容: 输入两个字符串,输出两个字符串集合的并集. 为保证输出结果一致,请将集合内元素排序之后再输出, 如对于集合aset,可输出sorted(aset). 输入格式: 共两行,每一行为一个字符串 ...

  4. python爬虫 爬取steam热销游戏

    好久没更新了啊...最近超忙 这学期学了学python 感觉很有趣 就写着玩~~~ 爬取的页面是:https://store.steampowered.com/search/?filter=globa ...

  5. 1826: [JSOI2010]缓存交换

    1826: [JSOI2010]缓存交换 https://www.lydsy.com/JudgeOnline/problem.php?id=1826 分析: 简单的贪心,然后调啊调...最近怎么了,码 ...

  6. POJ 1568 Find the Winning Move

    Find the Winning Move 链接 题意: 4*4的棋盘,给出一个初始局面,问先手有没有必胜策略? 有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), ...

  7. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  8. 还原T4模板执行前的警告对话框

    T4模板在保存的时候都会弹出个对话框,确认是否立即执行,大部分情况下我是不想立即执行的,所以一般都点Cancel,只有想执行的时候才点OK. 今天操作的时候不小心勾选了“Do not show thi ...

  9. 云计算之路-阿里云上:奇怪的CPU 100%问题

    这篇博文记录一下6月1日在阿里云上遇到的奇怪的CPU 100%问题,希望多年以后能真相大白. 那天负载均衡(SLB)中只放了1台云服务器(平时都放2台),由于是节假日,虽然只放了一台,但这台服务器的负 ...

  10. Scala学习笔记(四):从文件里读取文本行

    第一个版本: import scala.io.Source if(args.length>0){ for(line<-Source.fromFile(args(0)).getLines) ...