SCU3037 Painting the Balls
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的更多相关文章
- 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 ...
- 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 ...
- SGU 183 Painting the balls (优化的动态规划)
题意:给n个白球,选其中一些涂为黑色,且给了涂第i个球的花费为ci,要求每m个连续的球中至少有两个黑球,问最小花费是多少? 容易想到一个方程dp[i][j]=min{dp[k][i]}+c[j] dp ...
- SGU 183.Painting the balls
时间限制:0.25s 空间限制:4M 题意: 在n(n<=10000)个球中,给若干个球涂色,每个球涂色的代价为Ci,使得任意连续m(m<=100)个球中有至少两个球被涂了色. Solu ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- Codeforces Gym 100015B Ball Painting 找规律
Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- 13 Balls Problem
今天讨论的是称球问题. No.3 13 balls problem You are given 13 balls. The odd ball may be either heavier or ligh ...
随机推荐
- Linux(CentOS)安装Node.JS
源码安装 比使用yum安装灵活 1.创建目录 cd /opt mkdir program cd program 2.下载安装包 wget https://nodejs.org/dist/v8.12.0 ...
- CSS3复选框动画
本示例实现了两种单选按钮动画效果,一种是移动,一种是滑块,以下是html布局以及css样式 html:这里使用了label标签的for属性,以此来绑定radio <div class=" ...
- 1394-Minimum Inversion Number
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hibernate 各历史版本下载 spring各历史版本下载
hibernate 各历史版本下载http://sourceforge.net/projects/hibernate/files/ spring各历史版本下载http://www.springsour ...
- 1,理解java中的IO
IO中的几种形式 基于字节:InputStream.OutputStream 基于字符:Writer.Reader 基于磁盘:File 基于网络Socket 最终都是字节操作,字符到字节要编码转换 ...
- Scala function programming
1. Arbitrary multi parameters funcs sum(1,2,3,4,5) = sum(1 to 5: _*)the equal '=' can be ignored if ...
- Python-类-函数参数-takes 0 positional arguments but 1 was given
在学习Python基础的时候,在创建某一个shownametest()函数,解析器会报错 TypeError: shownametest() takes 0 positional arguments ...
- python接口自动化: CAS系统验证,自动完成登录并获取token,遇到302请求重定向设置(requests模块 allow_redirects=False)即可
import requestsimport re import requests import re class Crm_token(object): try: username=int(input( ...
- fidder工具学习抓取Firefox包
fidder抓取Firefox的https请求 抓包之前需要设置fidder,我下面的截图是fidder4,打开fidder—>Tools—>Options如图: 选择https,勾选所有 ...
- Spring实战第六章学习笔记————渲染Web视图
Spring实战第六章学习笔记----渲染Web视图 理解视图解析 在之前所编写的控制器方法都没有直接产生浏览器所需的HTML.这些方法只是将一些数据传入到模型中然后再将模型传递给一个用来渲染的视图. ...