日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SGU 183. Painting the balls( dp )

發布時間:2024/8/26 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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, 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 KBinput: 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.
InputThe 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).
OutputOutput only one integer number - the minimum amount of dye Petya will need (in milliliters).
Sample test(s)
Input6 3?
1 5 6 2 1 3
Output9
NoteExample note: 1, 2, 4, 5 balls must be painted.[submit][forum]

?

轉載于:https://www.cnblogs.com/JSZX11556/p/4797697.html

總結

以上是生活随笔為你收集整理的SGU 183. Painting the balls( dp )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。