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. Java 网络编程(Elliotte Rusty Harold)

    最近看了本Java网络编程方面的书,系统化一下Java网络编程,主要内容如下: 1.网络基础篇 这里包括基础的网络概念/ 输入输出流BIO/ Internet地址/URI/URL/HTTP/URLCo ...

  2. 关于类似于自动填充搜索框的DEMO

    接了个单子,客户要求左边输入时,右边自动到数据库查出对应内容,如果是单个INPUT还好,这个是动态增加INPUT,不过都是一样,关键是思路 这里遇到最郁闷的问题,就是我用的JQ1.9 以前用的JQ1. ...

  3. 05-C语言运算符

    目录: 一.进制转换 二.常量 三.sizeof 四.运算符 五.赋值运算符 六.自增减运算符 七.关系运算符 八.逻辑运算符 九.取址寻址运算符 回到顶部 一.进制转换 1 进制转换是人们利用符号来 ...

  4. 射频识别技术漫谈(13)——Mifare S50与Mifare S70

    Mifare S50和Mifare S70又常被称为Mifare Standard.Mifare Classic.MF1,是遵守ISO14443A标准的卡片中应用最为广泛.影响力最大的的一员.而Mif ...

  5. Hibernate JPA中@Transient、@JsonIgnoreProperties、@JsonIgnore、@JsonFormat、@JsonSerialize等注解解释

    @jsonignore的作用作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响. http://www.cnblogs.com/toSeeMyDream/p/443 ...

  6. BZOJ 1008 越狱 (组合数学)

    题解:正难则反,从总数中减去全部相邻不相同的数目就是答案,n*(n-1)^(m-1):第一个房间有n中染色方案,剩下m-1个房间均只有n-1种染色方案,用总数减就是答案. #include <c ...

  7. android 关于LCD背光调节渐变过程引起背光闪烁问题

    如果背光渐变过程会引起背光闪烁,可以采取以下任意一种方法修改:   方法1.减少调节级别时间 http://blog.csdn.net/sergeycao   默认的设计在关闭背光时会有灭屏动画,就是 ...

  8. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  9. out/target/common/obj/PACKAGING/public_api.txt android.view.KeyEvent.KEYCODE_has changed value from

    编译出错: out/target/common/obj/PACKAGING/public_api.txt:22549: error 17: Field android.view.KeyEvent.KE ...

  10. Boost.Asio基础(五) 异步编程初探

    异步编程 本节深入讨论异步编程将遇到的若干问题.建议多次阅读,以便吃透这一节的内容,这一节是对整个boost.asio来说是非常重要的. 为什么须要异步 如前所述,通常同步编程要比异步编程更简单.同步 ...