codeforces 1197D. Yet Another Subarray Problem 动态规划
题意 :给你n个数,还有m和k,让你找一个区间使这个公式的值最大 其中⌈x⌉是对x向上取整,l和r是所选的区间的左右下标。
思路 :定义一个dp[i][j],表示第i个数作为右端点,区间长度对m取余为j的最大值。那么就可以得到转移方程:
dp[i][j%m]=max(a[i]-k,dp[i-1][0]+a[i]-k );///当j等于1时
dp[i][j%m]=dp[i-1][(j-1+m)%m]+a[i];///当j不等于1时
余数等于1时就相当于从余数为零再加一个a[i] 那么K也要多减一个,其他时候因为j-1和j 向上取整相同就不需要多减K。
You are given an array a1,a2,…,ana1,a2,…,an and two integers mm and kk.
You can choose some subarray al,al+1,…,ar−1,aral,al+1,…,ar−1,ar.
The cost of subarray al,al+1,…,ar−1,aral,al+1,…,ar−1,ar is equal to ∑i=lrai−k⌈r−l+1m⌉∑i=lrai−k⌈r−l+1m⌉, where ⌈x⌉⌈x⌉ is the least integer greater than or equal to xx.
The cost of empty subarray is equal to zero.
For example, if m=3m=3, k=10k=10 and a=[2,−4,15,−3,4,8,3]a=[2,−4,15,−3,4,8,3], then the cost of some subarrays are:
- a3…a3:15−k⌈13⌉=15−10=5a3…a3:15−k⌈13⌉=15−10=5;
- a3…a4:(15−3)−k⌈23⌉=12−10=2a3…a4:(15−3)−k⌈23⌉=12−10=2;
- a3…a5:(15−3+4)−k⌈33⌉=16−10=6a3…a5:(15−3+4)−k⌈33⌉=16−10=6;
- a3…a6:(15−3+4+8)−k⌈43⌉=24−20=4a3…a6:(15−3+4+8)−k⌈43⌉=24−20=4;
- a3…a7:(15−3+4+8+3)−k⌈53⌉=27−20=7a3…a7:(15−3+4+8+3)−k⌈53⌉=27−20=7.
Your task is to find the maximum cost of some subarray (possibly empty) of array aa.
Input
The first line contains three integers nn, mm, and kk (1≤n≤3⋅105,1≤m≤10,1≤k≤1091≤n≤3⋅105,1≤m≤10,1≤k≤109).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).
Output
Print the maximum cost of some subarray of array aa.
Examples
input
7 3 10
2 -4 15 -3 4 8 3
output
7
input
5 2 1000
-13 -4 -9 -20 -11
output
0
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <map>
#include <cmath>
using namespace std ;
typedef long long ll;
const ll mod =1e9+7;
const int manx= 3e5+100;
ll a[manx];
ll dp[manx][12];
int main()
{ll n,m,k;scanf("%lld%lld%lld",&n,&m,&k);for(int i=1; i<=n; i++)scanf("%lld",&a[i]);ll ans= 0;for(int i=1; i<=n; i++){for(int j=1; j<=m&&j<=i; j++){if(j==1)dp[i][j%m]=max(a[i]-k,dp[i-1][0]+a[i]-k );elsedp[i][j%m]=dp[i-1][(j-1+m)%m]+a[i];ans=max(dp[i][j%m],ans );}}printf("%lld\n",ans);return 0;
}
codeforces 1197D. Yet Another Subarray Problem 动态规划相关推荐
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem(dp 最大子区间)
D. Yet Another Subarray Problem time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Codeforces 1257 C. Dominated Subarray
Codeforces 1257 C. Dominated Subarray https://codeforces.com/contest/1257/problem/C 思路:从数组的开始将每个出现过的 ...
- CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)
CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数) 题目大意:这题目啊,贼难理解- 定义一个数列是"好的&quo ...
- Codeforces Round #186 (Div. 2) Problem D 动态规划
题意:一条路上有n(n<=300)个洞,m(m<=100000)个公司,第i个公司可以修复连续区间 Li - Ri 内的洞,花费为Vi.问至少修复k个洞,最小花费是多少? 分析:先处理出一 ...
- Codeforces Beta Round #10 D. LCIS 动态规划
D. LCIS 题目连接: http://www.codeforces.com/contest/10/problem/D Description This problem differs from o ...
- codeforces#253 D - Andrey and Problem里的数学知识
这道题是这种,给主人公一堆事件的成功概率,他仅仅想恰好成功一件. 于是,问题来了,他要选择哪些事件去做,才干使他的想法实现的概率最大. 我的第一个想法是枚举,枚举的话我想到用dfs,但是认为太麻烦. ...
- Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读
http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...
- Codeforces 513G1 or 513G2 Inversions problem DP
题目大意: 就是现在初始给定一个n个数的排列, 每次随机地选取任意的一个段的数进行反转, 问k次随机翻转之后逆序对的数量的期望 G1难度题目链接:http://codeforces.com/conte ...
- Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学
- This is not playing but duty as allies of justice, Nii-chan! - Not allies but justice itself, Onii ...
最新文章
- zencart分类页产品页去掉url中的id号
- 看博客学学Android(十三)
- JS关于提交的RSA加密算法
- 当我们在聊 Serverless 时你应该知道这些
- 轨迹匹配地图 python_基于地图的视觉定位(一)
- Python Django 自定义Manager重写objects.create()方法代码示例
- 人工神经网络_图像加载(数据挖掘入门与实践-实验10)
- 程序员的十个层次 你属于哪一层?小菜看后
- [C++STL]string容器用法介绍
- Silverlight 动态调用 WebService
- STM32之SPI从机例程
- 关于近期对自己的总结
- Android 8.0 学习 (26)---Android8.0 Power Menu 添加截屏选项
- LeetCode算法题-Number Complement(Java实现-五种解法)
- CGPathAddArc
- SIR模型的应用(2) - Influence maximization in social networks based on TOPSIS(3)
- python 校验邮箱格式、手机号格式
- 我眼中的无影云桌面‖云桌面使用者角度
- gazebo仿真环境搭建+配置+小车运动仿真
- 如果不开心,请看一下
热门文章
- web中的$多种意思
- 聚焦2019世界人工智能大会:看马斯克、马云“唇枪舌战”,谁更胜一筹?
- ecshop 简单修改 可做淘宝客网站
- Web安全攻防 信息收集篇(仅供交流学习使用,请勿用于非法用途)
- c语言 适配器模式例子,NodeJS设计模式总结【单例模式,适配器模式,装饰模式,观察者模式】...
- 养车记账本小程序开发教程
- [HITCON 2016]Leaking沙箱逃逸学习
- jeesite4 图片上传总结
- cmd操作txt文件
- Xcode8 支持 iOS7及以下版本