183. Painting the balls

time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output
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 Ci 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 C1, C2, ..., CN (1<=Ci<=10000).
Output
Output only one integer number - the minimum amount of dye Petya will need (in milliliters).
Sample test(s)
Input
 
 
6 3 
1 5 6 2 1 3
 
 
Output
 
 
9
 
 
Note
Example note: 1, 2, 4, 5 balls must be painted.

思路:dp[i][j]//在i染色,在i-j染色的最小花费 设a b更新到,a b c,由远(距a m-1距c 1)到近以b为中心更新dp即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10001;
const int maxm=101;
const int inf=1e9+5;
int dp[maxn][maxm];//back maxm
int c[maxn];
int n,m;
int main(){
while(scanf("%d%d",&n,&m)==2){
//memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)scanf("%d",c+i);
for(int i=0;i<m;i++){
for(int j=0;j<i;j++){
dp[i][i-j]=c[i]+c[j];//g[i-j]=min(dp[i][j],g[i-j]);
}
}
for(int j=1;j<n;j++){
int minn=inf;
for(int i=min(n,j+m)-1;i>j&&i>=m;i--){
minn=min(dp[j][m+j-i],minn);
dp[i][i-j]=minn+c[i];
}
}
int ans=inf;
for(int i=n-m+1;i<n;i++){
for(int j=min(m-1,i-n+m);j>0;j--)
ans=min(ans,dp[i][j]);
}
printf("%d\n",ans);
}
return 0;
}

  

 

sgu 183. Painting the balls 动态规划 难度:3的更多相关文章

  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 (优化的动态规划)

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

  3. SGU 183.Painting the balls

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

  4. poj 3783 Balls 动态规划 100层楼投鸡蛋问题

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...

  5. Codeforces 264C Choosing Balls 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF264C.html 题目传送门 - CF264C 题意 给定一个有 $n$ 个元素的序列,序列的每一个元素是个 ...

  6. SCU3037 Painting the Balls

    Description Petya puts the \(N\) white balls in a line and now he wants to paint some of them in bla ...

  7. UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  8. UVa LA 3695 - Distant Galaxy 前缀和,状态拆分,动态规划 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  9. ZOJ 3161 Damn Couples 动态规划 难度:2

    Damn Couples Time Limit: 1 Second      Memory Limit: 32768 KB As mentioned in the problem "Coup ...

随机推荐

  1. Duilib初级控件扩展一例: 具有鼠标滚动消息的OptionUI

    转载:http://www.cnblogs.com/memset/p/Duilib_MouseWheelOptionUI_Deprecated.html

  2. cogs 330. [NOI2003] 文本编辑器

    ★★★   输入文件:editor2003.in   输出文件:editor2003.out   简单对比 时间限制:2 s   内存限制:128 MB [问题描述] 很久很久以前,DOS3.x的程序 ...

  3. POJ 2155 Matrix (二维树状数组)题解

    思路: 没想到二维树状数组和一维的比只差了一行,update单点更新,query求和 这里的函数用法和平时不一样,query直接算出来就是某点的值,怎么做到的呢? 我们在更新的时候不止更新一个点,而是 ...

  4. 【ssh免登录】设置集群环境ssh免登录步骤

    1.每台机器都需要执行,生成自己的密钥 # ssh-keygen -t rsa 过程中遇到选项,全部enter #cd ~/.ssh # cat id_rsa.pub > authorized_ ...

  5. v-bind绑定属性样式

    一.class的四种绑定方式 1.布尔值的绑定方式 <div id="demo"> <span v-bind:class="{'class-a':isA ...

  6. NS3 一个小问题

    可能会在执行./waf 命令的时候遇到这个问题,比如我想编译 /home/wasdns/Documents/NS3/ns-3.17/scratch 目录下的一个文件:newnsthree.cpp 编译 ...

  7. POJ 2117 Electricity(割点求连通分量)

    http://poj.org/problem?id=2117 题意:求删除图中任意一个顶点后的最大连通分量数. 思路: 求出每个割点对应的连通分量数,注意这道题目中图可能是不连通的. 这道题目我wa了 ...

  8. 简单介绍tomcat中maxThreads,acceptCount,connectionTimeout

    <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOW ...

  9. 难部署的taiga,式微的circus——趋势从进程管理到容器管理,简单才是美

    一直需要一个项目管理系统,一直没时间弄. taiga是github上搜project management star最多的项目,又是基于django用python写的后端,所以就用它: 但是,集中精力 ...

  10. Codeforces 847H - Load Testing

    847H - Load Testing 思路:dp. 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...