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/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
随机推荐
- loj #2053 莫队
\(des\) 存在一个长度为 \(n\) 的数字 \(s\), 一个素数 \(P\) \(m\) 次询问一段区间 \([l, r]\) 内的子串构成的数是 \(P\) 的倍数 \(sol\) 对于一 ...
- noi.ac #43 dp计数
\(sol\) 状态 \[f_{i, dis_1, dis_2, dis_3, dis_4}\] 表示到了第 \(i\) 层,其中 \(dis_{1}\) 表示第一根柱子剩下的最靠上的横木到当前 \( ...
- python中range语法
规则:一般不取最后一位 start: 计数从 start 开始.默认是从 0 开始.例如range(5)等价于range(0, 5); stop: 计数到 stop 结束,但不包括 stop.例如:r ...
- CUDA编程之快速入门【转】
https://www.cnblogs.com/skyfsm/p/9673960.html CUDA(Compute Unified Device Architecture)的中文全称为计算统一设备架 ...
- 树莓派PWM
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(26, GPIO.OUT) p = GPIO.PWM(26, ...
- 用sublime cmd 快速打开hosts文件
:: notepad start subl "%systemdrive%\WINDOWS\system32\drivers\etc\hosts" 文章来源:刘俊涛的博客 ...
- sublime text3中如何使用PHP编译系统
[WinError 2] 系统找不到指定的文件 编译错误原因,是因为编译器没有配置 第一步: 添加php可执行程序所在目录到系统环境变量(具体方法此处省略,使用本文下面的说明中的方法,此步骤可以省略 ...
- pip错误 ImportError: No module named 'pip_internal'
ubuntu16.04 - wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate sudo python get-pip.p ...
- ubuntu安装IntelliJ Idea及图标创建
一.下载并解压安装 二.创建桌面程序 1. cd /usr/local/applications/ 2. vi idea.desktop 3. 内容如下 [Desktop Entry] Name=In ...
- Python之BeautifulSoup的使用示例
import requests from bs4 import BeautifulSoup url = 'http://pm25.in/'+city_pinyin r = requests.get(u ...