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 ...
随机推荐
- Windows使用Node.js自动生成Vue.js模版环境部署步骤-----记录
node.js官网下载并安装node 进入node文档目录下,运行cmd 输入 node -v 查看node版本 出现表示安装完成 输入 npm -v 显示npm版本信息 安装cnpm 输入 npm ...
- 九、IIC驱动原理分析
学习目标:学习IIC驱动原理: 一.IIC总线协议 IIC串行总线包括一条数据线(SDA)和一条时钟线(SCL),支持“一主多从”和“多主机”模式:每个从机设备都有唯一的地址来识别. 图 1 IIC ...
- python3爬虫之开篇
写在前面的话: 折腾爬虫也有一段时间了,从一开始的懵懵懂懂,到现在的有一定基础,对于这一路的跌跌撞撞,个人觉得应该留下一些文字性的东西,毕竟好记性不如烂笔头,而且毕竟这是吃饭的家伙,必须用心对待才可以 ...
- ts包、表、子表、section的关系
我们经常接触到创建 DEMUX,注册 Filter 过滤数据, 通过回调过滤出 section 数据,然后我们对 section 数据做具体的解析或者其他操作. 我们这里说的 section 就是段的 ...
- ssh安装和使用
1.基础知识 ssh用于远程登陆,linux默认安装了client,如果需要被登陆则需要安装 server 2.安装 apt-get install openssh-server 检查是否安装成功 a ...
- 1176: [Balkan2007]Mokia
1176: [Balkan2007]Mokia 链接 分析 三维偏序问题,CDQ分治论文题. 代码 #include<bits/stdc++.h> using namespace std; ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
- hive报错:Caused by: ERROR XBM0H: Directory /var/lib/hive/metastore/metastore_db cannot be created.
在cdh集群中,删除之前的hive服务,然后将hive添加到其他节点,然后再通过hive客户端连接hive报错: Caused by: ERROR XJ041: Failed to create da ...
- 在Linux中安装和配置OpenVPN Server的最简便方法!
本文介绍了如何在基于RPM和DEB的系统中安装和配置OpenVPN服务器.我们在本文中将使用一个名为openvpn-install的脚本,它使整个OpenVPN服务器的安装和配置过程实现了自动化.该脚 ...
- 链接程序的时候遇到问题:fatal error LNK1104: cannot open file 'rctrl-d.lib'
1.lib库文件没有添加到工程中(工程里面根本就没有这个文件) 2.