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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Ilya Muromets(DP or 思维)

發(fā)布時(shí)間:2023/11/29 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ilya Muromets(DP or 思维) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Ilya Muromets

?Gym - 100513F

Силачом слыву недаром — семерых одним ударом! From the Russian cartoon on the German fairy tale.

Ilya Muromets is a legendary bogatyr. Right now he is struggling against Zmej Gorynych, a dragon with?n?heads numbered from 1 to?n?from left to right.

Making one sweep of sword Ilya Muromets can cut at most?k?contiguous heads of Zmej Gorynych. Thereafter heads collapse getting rid of empty space between heads. So in a moment before the second sweep all the heads form a contiguous sequence again.

As we all know, dragons can breathe fire. And so does Zmej Gorynych. Each his head has a firepower. The firepower of the?i-th head is?fi.

Ilya Muromets has time for at most two sword sweeps. The bogatyr wants to reduce dragon's firepower as much as possible. What is the maximum total firepower of heads which Ilya can cut with at most two sword sweeps?

Input

The first line contains a pair of integer numbers?n?and?k?(1?≤?n,?k?≤?2·105) — the number of Gorynych's heads and the maximum number of heads Ilya can cut with a single sword sweep. The second line contains the sequence of integer numbers?f1,?f2,?...,?fn?(1?≤?fi?≤?2000), where?fi?is the firepower of the?i-th head.

Output

Print the required maximum total head firepower that Ilya can cut.

Examples

Input 8 2
1 3 3 1 2 3 11 1 Output 20 Input 4 100
10 20 30 40 Output100 題意:
  給你兩個(gè)數(shù)n和k,然后n個(gè)數(shù),進(jìn)行兩次操作,一次最多可以消除連續(xù)的K個(gè)數(shù),然后剩余的數(shù)又變成連續(xù)的,問兩次操作后,消除的數(shù)的總和最大為多少。
題解:
  dp[i][1]表示的是1-i的區(qū)間中,第一次操作能得到的最大值,因?yàn)閕從k-n,每次加一,所以每次對(duì)最大值可能造成改變的只有新加進(jìn)來的數(shù),dp[i][1]=max(dp[i-1][1],sum[i]-sum[i-k]);
  dp[i][1]表示的是1-i的區(qū)間中,兩次操作后能得到的最大值,dp[i][2]=max(dp[i-1][2],sum[i]-sum[i-k]+dp[i-k][1]);對(duì)于兩次操作,狀態(tài)轉(zhuǎn)移方程就是1- i-1區(qū)間兩次操作后最大值或者是新加入的i影響的區(qū)間sum[i]-sum[i-k]+1- i-k區(qū)間操作一次的最大值。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=2e5+10; 7 int sum[maxn],a[maxn],dp[maxn][3]; 8 int main() 9 { 10 int n,k; 11 scanf("%d%d",&n,&k); 12 for(int i=1;i<=n;i++) 13 { 14 scanf("%d",&a[i]); 15 sum[i]=sum[i-1]+a[i]; 16 } 17 if(2*k>=n) 18 { 19 printf("%d\n",sum[n]); 20 return 0; 21 } 22 memset(dp,0,sizeof(dp)); 23 for(int i=k;i<=n;i++) 24 { 25 dp[i][1]=max(dp[i-1][1],sum[i]-sum[i-k]); 26 dp[i][2]=max(dp[i-1][2],sum[i]-sum[i-k]+dp[i-k][1]); 27 // printf("%d %d %d\n",i,dp[i][1],dp[i][2]); 28 } 29 printf("%d\n",dp[n][2]); 30 return 0; 31 }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/1013star/p/10070083.html

總結(jié)

以上是生活随笔為你收集整理的Ilya Muromets(DP or 思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。