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. tomcat管理员manager app无法进入解决方法

    浏览器输入http://localhost:8080/进入tomcat页面后,点击manager app输入用户名(admin)密码(admin)后页面跳转出现如下错误:备注:tomcat7.0.39 ...

  2. @Transactional失效的问题

    spring事物配置一般没有问题, 优先检查mysql的引擎是否是innodb, 是的话检查包的扫描是否有问题. 我就是因为包的扫描导致@Transactional失效. 具体情况如下, 在sprin ...

  3. mybatis配置方法

    首先导入mybatis-3.1.1.jar包以及Mysql-connector-java-5.1.6-bin.jar包 新建一个数据库 create database mybatis; use myb ...

  4. mybatis+postgresql平台

    mybatis+postgresql平台        最近有个项目的数据库使用postgresql,使用原生态的mybatis操作数据,原生态的没什么不好,只不过国内有个tk.mybatis的工具帮 ...

  5. js函数调用模式总结

    在javascript中一共有四种调用模式:方法调用模式.函数调用模式.构造器调用模式和apply调用模式.这些模式在如何初始化关键参数this上存在差异 方法调用模式 当一个函数被保存为对象的一个属 ...

  6. 导入Excel加行公式和验证

    package com.sprucetec.tms.controller.fee.export; import com.sprucetec.tms.controller.base.BaseFeeExp ...

  7. 在原有3306端口mysqld服务的情况再搭建第二个3308端口的mysql实例

    1 download the tar.gz [root@472322 tmp]# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6 ...

  8. hdu1281结题报告

    哎哎...自己刚刚一看到这个题目居然.....什么都想不到...看了一下别人的解题报告说最大匹配...于是就自己开始构思啦... 对于这个棋盘,有K个可以放棋子的位置....那么 首先我们开始可以求出 ...

  9. SQL 注入与防御实例

    注入 1. 创建存储 USE TSQL2012; GO IF OBJECT_ID('Sales.ListCustomersByAddress') IS NOT NULL DROP PROCEDURE ...

  10. NSOperationQueue和GCD的区别

    使用NSOperationQueue用来管理子类化的NSOperation对象,控制其线程并发数目.GCD和NSOperation都可以实现对线程的管理,区别是 NSOperation和NSOpera ...