Team work [洛谷] (DP) /*rank*/

原题传送门


题面:

Team Work

                             time limit per test 1 second
                          memory limit per test 256 megabytes
                                   input standard input
                                  output standard output

Problem Description

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≤1e4) 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≤1e3), 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 1e5.

Output

Please print the highest possible sum of skill levels FJ can achieve by grouping appropriate consecutive sets of cows into teams.

Sample Input

7 3
1 15 7 9 2 5 10

Sample Output

84
noteIn

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(0 <= n <= K)头牛可以划分为一个小组,使其中skill level最高的一头作为代表,使n头牛的skill level都为该牛的skill level,求出这N头牛合理规划后的skill level的最大总和.

题目分析

可以认为第 i 头牛为小组的最后一头牛,然后向前查找 j (1 <= j <= K)头牛中最大值,并记录结果.(限制条件有 i-j > 0)
记SkillMax为 i-j ~ i (1 <= j <= K)头牛中的最大值.
得出递推公式dp[ i+j ] = max(dp[ i ] , dp[ i-j ] + j*SkillMax),那么dp[ N ]即为所求答案.

具体代码

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
int cow[maxn];
int dp[maxn];

int main()
{
    int N, K;
    scanf("%d%d", &N, &K);
    for(int i = 1; i <= N; i++)
    {
        scanf("%d", &cow[i]);
    }
    for(int i = 1; i <= N; i++)//枚举第i头牛为小组最后一头牛
    {
        int MaxSkill = -1;
        for(int j = 1; j <= min(i, K); j++)//枚举小组内牛的数量
        {
            MaxSkill = max(MaxSkill, cow[i-j+1]);//+1是为了保证第i头牛的值也能被算入maxskill的对比中
            dp[i] = max(dp[i], dp[i-j]+MaxSkill*j);
        }
    }
    printf("%d\n", dp[N]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值