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. 实时消息传输协议 RTMP(Real Time Messaging Protocol)

    实时消息传输协议(RTMP)最初是由 Macromedia 为互联网上 Flash player 和服务器之间传输音频.视频以及数据流而开发的一个私有协议.Adobe 收购 Macromedia 购以 ...

  2. IOS 学习笔记(1) 视图UIViewController

    1.UIViewController *newController=[[UIViewController alloc] initWithNibName:@"XXX" bundle: ...

  3. qt 国际化(翻译时会触发changeEvent)

    1. 修改工程文件 .pro ,加入翻译源文件 hello_world.ts: TRANSLATIONS += \        Resource/translations/hello_world.t ...

  4. 转:CI配置SMARTY

    1.到相应站点下载Smarty的源码包:2.将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty:3.在目录 application/libra ...

  5. DllMain加载其他DLL造成的死锁问题及其解决办法

    使用VS 2008新建一个MFC ActiveX工程,因为在工程里要用到GDI+.我习惯把初始化GDI+库的代码放在应用程序类的InitInstance函数,对应的销毁代码放在ExitInstance ...

  6. openScales源码学习系列之 Feature属性

    coordinates:当前区域,geometry或Polygon的点集合. countries.国家所在位置下标 SQKM.平方千米 COLOR_MAP.该国家的颜色类别 SQMI.平方英里 CON ...

  7. HDU 1001 Sum Problem

    /* 注意可以是负整数,而且在过程中会超过int,所以要用longlong */ #include <cstdio> int main() { long long n; while (sc ...

  8. Oracle 日期计算

    日期相减,求天数 方法一: )),createdate from goods t

  9. [转载]Android 知识图谱

    from: http://blog.csdn.net/xyz_lmn/article/details/41411355

  10. 首页 导航栏隐藏 下一级页面显示,pop回来遇到的问题

    - (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];    [self.navigationContr ...