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, j)}, 那么转移时间下降为O(1).然后滚动数组..这道题卡空间..时间复杂度O(NM)

--------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
const int maxn = 10001;
const int maxm = 101;
const int INF = 0x3F3F3F3F;
 
int dp[maxm], f[maxn][maxm], cost[maxn], N, M;
 
int main() {
memset(f, INF, sizeof f);
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; i++) scanf("%d", cost + i);
int ans = INF;
for(int i = 1; i <= N; i++) {
if(i < M) f[i][0] = dp[0] = cost[i];
   for(int j = 1; j <= M; j++)  {
    if(j > 1) f[i][j] = f[i][j - 1];
    if(j > i) continue;
    dp[j] = f[i - j][M - j];
    if(i <= M) dp[j] = min(dp[j], f[i - j][0]);
    f[i][j] = min(f[i][j], dp[j] += cost[i]);
    if(N - i + j + 1 <= M) ans = min(ans, dp[j]);
   }
}
printf("%d\n", ans);
return 0;
}

--------------------------------------------------------------------------------

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.

SGU 183. Painting the balls( dp )的更多相关文章

  1. 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 ...

  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. SGU 149 Computer Network 树DP/求每个节点最远端长度

    一个比较经典的题型,两次DFS求树上每个点的最远端距离. 参考这里:http://hi.baidu.com/oi_pkqs90/item/914e951c41e7d0ccbf904252 dp[i][ ...

  5. ZOJ 3725 Painting Storages(DP+排列组合)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5048 Sample Input 4 3 Sample Output ...

  6. SGU 310. Hippopotamus( 状压dp )

    题目大意:给N块板, 有A,B2种类型的板, 要求任意M块连续的板中至少有K块B板.1≤n≤60,1≤m≤15,0≤k≤m≤n. dp(x, s)表示第x块板, x前M块板的状态为s, 然后合法状态转 ...

  7. SGU 149. Computer Network( 树形dp )

    题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, d ...

  8. SGU 415. Necessary Coins ( 背包dp )

    题意大概是:给出N个硬币, 面值为a_i, 问要凑成X元哪些硬币是不可或缺的.1 ≤ N ≤ 200, 1 ≤ x ≤ 10^4 直接枚举, 然后就是01背包了. 为了不让复杂度多乘个N, 我们就从左 ...

  9. 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 ...

随机推荐

  1. jquery左右自适应伸缩插件

    简单css布局 body { margin: 0 auto; padding: 0 auto; } .showpannal { position:absolute; left: 200px; top: ...

  2. set up blog on github

    http://www.ruanyifeng.com/blog/2012/08/blogging_with_jekyll.html

  3. codeforces 607B. Zuma 区间dp

    题目链接 给一个长度为n的序列, 每一次可以消去其中的一个回文串, 问最少几次才可以消完. 代码很清楚 #include <iostream> #include <vector> ...

  4. idea中使用scala运行spark出现Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class

    idea中使用scala运行spark出现: Exception in thread "main" java.lang.NoClassDefFoundError: scala/co ...

  5. 堆排序(java实现)

    public class Test04 { static int a[] = {9,8,7,6,5,4,3,2,1,11,12,10,19,18,17,16}; public static void ...

  6. spss

    编辑 SPSS(Statistical Product and Service Solutions),“统计产品与服务解决方案”软件.最初软件全称为“社会科学统计软件包” (SolutionsStat ...

  7. 1TB到底能存放多少东西?

    网盘大战逐渐升级,360和百度网盘先后推出的1TB网盘存储,而腾讯甚至为其微云网盘打出10TB的招牌来哄抢用户. 这里我们聊聊1TB的网盘究竟能放多少东西? 以下是我在网上找到的一些资料. 一)30年 ...

  8. 用Jetty和redis实现接入服务器adapter

    传统的服务器端为若干个客户端提供服务,一般需要开启多个服务器端进程.为了进一步提升服务器端的处理能力,可以如下图所示将服务解耦为两部分(adapter与workers),它们之间通过消息队列传输数据, ...

  9. linux 中多线程使用

    /* * 对于线程之间的操作: * 一个进程中会有多个线程,各个线程之间的或向切换会付出很小的代价相比于进程之间的切换 * 为什么要引入多线程: * 1.与进程相比,他是一种非常节俭的多任务的操作方式 ...

  10. No.2小白的HTML+CSS心得篇

    今天要强调的重点是分析把握好HTML标签的两个方面: 1.标签的用途(用途指的是用来干什么,有什么作用)在专业术语叫作 语义化. 举个列子:<br/> 换行的作用 见了它就明白它的语义就是 ...