CodeForces - 1077F1 Pictures with Kittens (easy version) (动态规划)

The only difference between easy and hard versions is the constraints.
Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of nn consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the ii-th picture has beauty aiai.
Vova wants to repost exactly xx pictures in such a way that:

each segment of the news feed of at least kk consecutive pictures has at least one picture reposted by Vova;
the sum of beauty values of reposted pictures is maximum possible.

For example, if k=1k=1 then Vova has to repost all the pictures in the news feed. If k=2k=2 then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.
Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.

题意:给你一个长度为N的序列,问从中取X个值的最大和,要求在原序列中每K个连续的序列里必须最少要有一个值被选择.
思路:1.求长度为N的序列里面去X个值的最大和,我们很快就可以想到用DP的思想。
2.每K个连续序列里必须要有一个值被选择,就代表我们选择的两个相邻的值的位置相隔不能超过K(否则他们两个之间就有一定有连续为K的序列中不含有被选择的值).
3.结合上述1.2的思路,我们可以尝试用DP求最大和,同时用所选择的两个相邻值之间相隔不超过K的条件来维护每次DP更新值的范围.
我用dp [i] [j] 来表示取第i个值时已经取了j个值的最大和。
动态转移方程为
dp [i] [j] = max ( dp [q] [j-1] (i-k<=q<i ) ) + a[i]

AC代码如下:

long long a[210];
long long dp[210][210]={0};
int main() {
	int n,k,x;
	cin>>n>>k>>x;
	for(int i=0;i<n;i++) {
 		cin>>a[i];
  	if(i<k) dp[i][1]=a[i];
 	}
 	for(int i=0;i<n;i++) {
  		for(int l=1;l<=x;l++) {
   			if(dp[i][l]==0) continue;
   			for(int j=i+1;j<=i+k&&j<n;j++) {
    				dp[j][l+1] = max(dp[j][l+1],dp[i][l]+a[j]);
   			}
  		}
 	}
 	long long ans = -1;
 	for(int i=n-k;i<n;i++) {
  		if(dp[i][x]==0) continue;
  	ans = max(ans,dp[i][x]);
 	}
 	cout<<ans<<endl; 
 	return 0; 
}

更新

第二次遇到了这个题 ,突然想到了不同做法 , 我们 可以用记忆化搜索的 方法去做 , 看了一下 时间和 内存 和上面方法差不多 (内存多了 0.2MB) 。
具体看代码


const long long inf = 1e18;
int n,k,x;
int a[220];
long long dp[220][220];
int vis[220][220];

long long dfs(int nums,int now) {
    if(now == x ) { // 找到了第 x 个数
        vis[nums][now] = 1;
        if(nums + k-1 >=n) return dp[nums][now] = a[nums]; // 判断是否能覆盖剩余的所有数
        else return dp[nums][now] = -1;
    }
    if(nums == 0) {
        long long res = -1;
        for(int i= 1;i<min(n+1,1+k);i++) {
            res = max(res,dfs(i,now+1));
        }
        return res;
    }
    if(vis[nums][now] == 1) return dp[nums][now];
    dp[nums][now] = inf;
    long long res = -1;
    for(int i = nums+1 ;i <min(n+1,nums + k+1);i++) { // 和上述思路其实是相同的 即在 接下来的k 个数中选择一个数 .
        res = max(res,dfs(i,now+1));
    }
    dp[nums][now] = (res== -1? -1: res + a[nums]);
    vis[nums][now] = 1;
    return dp[nums][now];
}

int main() {
    cin>>n>>k>>x;
    for(int i=1;i<=n;i++) cin>>a[i];
    cout<<dfs(0,0)<<endl;
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值