码迷,mamicode.com
首页 > 其他好文 > 详细

贾君鹏玩游戏

时间:2016-08-21 09:44:20      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

定义一个数组f[i][k],表示以i结尾,分成k份,能得到的最大和,由于这要定义不好转移,我们可以再定义一个数组g[i][k],表示将前i个分成k份能得到的最大和,

那么f[i][k]=max(f[i-1][k-1]+a[i],g[i-1][k]+a[i]);g[i][k]=max(f[i][k],g[i-1][k]);

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <conio.h>
 6 #include <cstdio>
 7 #include <cmath>
 8 using namespace std;
 9 
10 
11 int cnt=0,sum[100003],f[100003][4],g[100003][4];
12 int main(){
13     scanf("%d",&cnt);
14     for(int x=1;x<=cnt;x++){scanf("%d",sum+x);}
15     memset(f,-127/2,sizeof(f));
16     memset(g,-127/2,sizeof(g));
17     f[0][0]=0;g[0][0]=0;
18     for(int x=1;x<=cnt;x++)f[x][0]=0,g[x][0]=0;
19     for(int x=1;x<=3;x++){
20         for(int j=1;j<=cnt;j++){
21             if(j<x)continue;
22             g[j][x]=max(f[j-1][x]+sum[j],g[j-1][x-1]+sum[j]);
23             f[j][x]=g[j][x];
24             g[j][x]=max(g[j][x],g[j-1][x]);
25         }
26     }
27     printf("%d",g[cnt][3]);
28     return 0;
29 }

 

贾君鹏玩游戏

标签:

原文地址:http://www.cnblogs.com/Ateisti/p/5792094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!