Teamwork(DP)

题目

  • 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≤104) are all standing in a row, conveniently numbered 1…N in order. Cow ii 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≤103), 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.

  • input
    7 3
    1
    15
    7
    9
    2
    5
    10

  • output
    84

题意

每头牛有一个skill值,当K头牛组队时,这个队伍中的每头牛skill值都会等于队伍中的最高值,但是只能和相邻的牛组队
求如何组队可以使全部队伍的skill值总和最大

思路

DP,DP[i]记录队伍长度为i时的skill总和最大值
第j头牛加入時,他可以和前面的组队,需要比较他加入后需要和前面的几头牛组队可以获得最大skill值
DP[i+1]=max( DP[i]+max(skill[j])*1 , DP[i-1]+max(skill[j], skill[j-1])*2 , DP[i-2]+max(skill[j], skill[j-1], skill[j-2])*3 , …)直到最后一队满编
将max(skill[ i~j ])的值预处理,DP的時候O(1)查询,不然会超时

代码

#include <bits/stdc++.h>
using namespace std;

int cow[10010];
int max_skill[10010][1010];
int dp[10010];

int main() {
    int N,K;
    while(scanf("%d%d",&N,&K)!=EOF) {
        for(int i=1; i<=N; ++i)
            scanf("%d",cow+i);

        int num=0;
        int maxc=0;
        //将max(skill[ i~j ])的值预处理
        for(int i=1; i<=N; ++i) {
            for(int j=i; (num=i-j+1)<=K and j>0; --j) {
                maxc=max(maxc,cow[j]);
                max_skill[i][num]=maxc;
            }
            maxc=0;
        }
		//DP
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=N; ++i) {
            for(int j=i; (num=i-j+1)<=K and j>0; --j) {
				dp[i]=max(dp[i],dp[i-num]+num*max_skill[i][num]);
            }
        }
		cout << dp[N]<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值