CF1197D Yet Another Subarray Problem
思路:
使用动态规划,在经典的最大子段和解法基础上进行扩展。dp[i][j]表示以第i个数为结尾,并且长度模m等于j的所有子段的最大cost。
实现:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = ;
ll a[N], dp[N][];
int main()
{
int n; ll m, k;
while (cin >> n >> m >> k)
{
memset(dp, , sizeof dp);
for (int i = ; i <= n; i++) cin >> a[i];
ll res = ;
dp[][] = -k;
for (int i = ; i < m; i++) dp[][i] = -INF;
for (int i = ; i <= n; i++)
{
for (int j = ; j < m; j++)
{
if (j == (m == ? : ))
{
dp[i][j] = max(dp[i - ][] + a[i] - k, a[i] - k);
}
else
{
dp[i][j] = max(dp[i - ][(j - + m) % m] + a[i], a[i] - k);
}
res = max(res, dp[i][j]);
}
}
cout << res << endl;
}
return ;
}
CF1197D Yet Another Subarray Problem的更多相关文章
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- 动态规划法(八)最大子数组问题(maximum subarray problem)
问题简介 本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp
D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...
- D. Yet Another Subarray Problem 思维 难 dp更好理解
D. Yet Another Subarray Problem 这个题目很难,我比赛没有想出来,赛后又看了很久别人的代码才理解. 这个题目他们差不多是用一个滑动窗口同时枚举左端点和右端点,具体如下: ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】
一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...
- CodeForces 1197D Yet Another Subarray Problem
Time limit 2000 ms Memory limit 262144 kB Source Educational Codeforces Round 69 (Rated for Div. 2) ...
- CodeForces 1197 D Yet Another Subarray Problem
题面 不得不说CF还是很擅长出这种让人第一眼看摸不着头脑然后再想想就发现是个SB题的题的hhh(请自行断句). 设sum[]为前缀和数组,那么区间 [l,r]的价值为 sum[r] - sum[l-1 ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
随机推荐
- shell脚本的参数传递使用
1.params.sh源码如下 #!/bin/bash # author:daokr # url:www.daokr.com echo "Shell 传递参数实例!"; echo ...
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- Comet OJ - Contest #8题解
传送门 \(A\) 咕咕咕 const int N=1005; char s[N][N];int len[N],n,id; inline bool cmp(R int j,R int k){ R in ...
- vue中全局filter和局部filter怎么用?
需求: 将价值上加上元单位符号(全局filter) <template> <div> 衣服价格:{{productPrice|formatTime}} </div> ...
- nginx+keepalived高可用实战
1.整体架构图如下 2.环境准备 今天所配置的是keepalived+nginx 的负载均衡 下载keepalived软件 [root@LB01 tools]# wget http://www.kee ...
- spring事务传播行为的思考
1.问题 @TransactionConfiguration(transactionManager = "txManager", defaultRollback = false) ...
- Ibatis自动解决sql注入机制
疑问1:为什么IBatis解决了大部分的sql注入?(实际上还有部分sql语句需要关心sql注入,比如like) 之前写Java web,一直使用IBatis,从来没有考虑过sql注入:最近写php( ...
- mysql 连接服务器报ERROR 1130 ,mysql服务正常运行
今天办公电脑的mysql罢工了,折腾了半天,记录一下. 错误如下: ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to ...
- 利用原生JS实时监听input框输入值
传送门https://www.cnblogs.com/lantinggumo/p/7636715.html 传送门https://www.cnblogs.com/nailc/p/8572226.htm ...
- 苹果IPhone真机开发调试
需要 在苹果开发网站 加入真机的UDID, 并在Profile中勾选该手机