题面:

传送门

B. Teamwork

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
 
For his favorite holiday, Farmer John wants to send presents to his friends. Since he isn’t very good at wrapping presents, he wants to enlist the help of his cows. As you might expect, cows are not much better at wrapping presents themselves, a lesson Farmer John is about to learn the hard way. Farmer John’s N cows (1 ≤ N ≤ 10^4) are all standing in a row, conveniently numbered 1...N in order. Cow i has skill level si at wrapping presents. These skill levels might vary quite a bit, so FJ decides to combine his cows into teams. A team can consist of any consecutive set of up to K cows (1 ≤ K ≤ 10^3), and no cow can be part of more than one team. Since cows learn from each-other, the skill level of each cow on a team can be replaced by the skill level of the most-skilled cow on that team. Please help FJ determine the highest possible sum of skill levels he can achieve by optimally forming teams.
 
Input
The first line of input contains N and K. The next N lines contain the skill levels of the N cows in the order in which they are standing. Each skill level is a positive integer at most 105.
 
Output
Please print the highest possible sum of skill levels FJ can achieve by grouping appropriate consecutive sets of cows into teams.
 
Example
Input
7 3
1
15
7
9
2
5
10
Output
84
 
Note
In this example, the optimal solution is to group the first three cows and the last three cows, leaving the middle cow on a team by itself (remember that it is fine to have teams of size less than K). This effectively boosts the skill levels of the 7 cows to 15, 15, 15, 9, 10, 10, 10, which sums to 84.
 

题目描述:

有n头奶牛,我们可以让连续的奶牛组成一队,组队后队里所有奶牛的等级就会变成队里等级最高的那个,求n头奶牛经过组队后,所有奶牛的等级之和最大的是多少。一队奶牛的数量最多不超过C头牛。
 

题目分析:

这道题可以用动态规划解决:我们先重新定义这个问题:求前n头牛的等级之和最大值是多少?再看看这个问题的子问题:前i头牛的等级之和最大值是多少?(1 <= i <= n)会想到如何联系子问题和我们要求的题目问的问题,其实就是:我已经知道前i头牛的等级之和最大值是多少,然后通过这个计算出前n头牛的等级之和最大值是多少。然后,我们可以定义一个dp[i]数组来记录前i头牛的等级之和最大值,来通过利用dp[i]的值算出dp[n]。
 
我们具体看看样例怎样算:首先,我们已经知道dp[i](1 <= i <= 6)的值,求dp[7]。我们对最后一头牛,也就是第7头牛进行分类讨论:
 
1.第7头奶牛自己组成一队,那么,dp[7] = dp[6] + 10
 
 
2.第7头奶牛和前面的一头奶牛组成一队,那么,dp[7] = dp[5] + 10 * 2
 
 
3.第7头奶牛和前面的两头奶牛组成一队,那么,dp[7] = dp[4] + 10 * 3

然后选出最大的那种情况就可以了。
那么,dp[4], dp[5], dp[6]怎么求?我们可以用求dp[7]的方法同样求出这三个的值。
状态转移方程:dp[i] = max{ dp[i-j] + j * max level[k] ( i-j+1 <= k <= i ) }   
 
 
AC代码:
 
 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <cmath>
5 #include <algorithm>
6 using namespace std;
7 const int maxn = 1e4+5;
8 const int maxk = 1e3+5;
9 int n, k;
10 int a[maxn];
11 int dp[maxn];
12
13 void test(){
14 cout << endl;
15 for(int i = 1; i <= n; i++){
16 printf("dp[%d] = %d\n", i, dp[i]);
17 }
18 }
19
20 int main(){
21 scanf("%d%d", &n, &k);
22 for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
23
24
25 for(int i = 1; i <= n; i++){
26 int pre_max = 0;
27 for(int j = 1; j <= k; j++){
28 if(i >= j){
29 if(a[i-j+1] > pre_max) pre_max = a[i-j+1];
30 dp[i] = max(dp[i], dp[i-j]+pre_max*j);
31 }
32 }
33 }
34
35 //test();
36 printf("%d\n", dp[n]);
37 return 0;
38 }
 
 

最新文章

  1. IOS之计算器实现
  2. msvc库没有安装包,编译选项选择 代码生成 MT【多线程】,C#调用
  3. Matlab绘图系列之高级绘图
  4. 关于android:screenOrientation=&quot;portrait&quot;等
  5. Qt 中程序自动重启
  6. DLR、ASTER GDEM、SRTM3、GMTED2010等5种全球高程数据对比
  7. iOS 数据库第三方FMDB的简单使用
  8. CSS之 border 属性
  9. 浏览器正确理解和使用GBK及UTF-8(UTF-8 + BOM)网页编码
  10. css实现多行文本溢出显示省略号(…)全攻略
  11. Go基础之--排序和查找操作
  12. opencv学习之路(39)、PCA
  13. Linux MySQL数据库文件同步及数据库备份
  14. Atcoder Regular-074 Writeup
  15. codeblocks调试
  16. win10 下 protobuf 与 qt
  17. LevelDB和ForestDB简单性能测试(含代码)
  18. deque容器的运用一点一点积累
  19. odex反编译dex异常 Cannot locate boot class path file /system/framework/core.odex
  20. Unity 3d导入3dMax模型 产生若干问题

热门文章

  1. java 提供了哪些IO方式
  2. hautoj 1268 小天使改名
  3. 2019南昌网络赛H The Nth Item(二阶线性数列递推 + 广义斐波那契循环节 + 分段打表)题解
  4. Netty(二)Netty 与 NIO 之前世今生
  5. 【算法】KMP算法
  6. React Hooks: useRef All In One
  7. nest cli bug
  8. queueMicrotask &amp; microtask
  9. how to group date array by month in javascript
  10. taro &amp; Error: spawn taro ENOENT