D. Yet Another Subarray Problem 思维 难 dp更好理解
D. Yet Another Subarray Problem
这个题目很难,我比赛没有想出来,赛后又看了很久别人的代码才理解。
这个题目他们差不多是用一个滑动窗口同时枚举左端点和右端点,具体如下:
首先枚举0~m,这个是说更新的位置,如果是1 当m==3 就更新1 4 7 10...
如果是2,当m==3 就更新 2 6 8 11....
最后都会被更新的。
核心代码
for (int j = ; j < n - i; ++j) {
s += sum[j];
if (j % m == ) s -= k;//这个在判断是不是经过了一个区间,如果经过了就-k
ans = max(ans, s - MIN);//这个是在判断这个时候枚举的区间左端点和区间右端点是不是可以更新答案
if (j % m == m - ) MIN = min(MIN, s);//这个就是在更新区间左端点,只有特定的时刻才可以更新,
//只有在区间左端点才可以,因为我要更新的是下一个区间的右端点,如果提前更新了,那么就会被提前用,可能更新不该更新的东西。
//左端点是由s来更新的,所以不需要考虑k的问题,
//这个左端点只有特定时候才可以更新,因为我们枚举了每一个区间为m的起点,意思就是说我们确定了每一个区间
//当j%m==m-1就是说到了区间的右端点,只有这个时候才可以更新,因为这个时候和我们想更新的是下一个区间的右端点,
//所以这个才满足两点之间相隔了若干个k,这样子就可以一 前缀相减+k的个数相减*k。
}
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 3e5 + ;
typedef long long ll;
ll n, m, ans, k, a[maxn], sum[maxn]; int main()
{
ans = ;
scanf("%lld%lld%lld", &n, &m, &k);
for (int i = ; i < n; i++) scanf("%lld", &a[i]);
for(int i=;i<m;i++)//枚举第一个区间的起点
{
for (int j = i; j < n; j++) sum[j - i] = a[j];
ll mins = , s = ;
for(int j=;j<n-i;j++)
{
s += sum[j];
if (j%m == ) s -= k;
ans = max(ans, s - mins);
if (j%m == m - ) mins = min(mins, s);
}
}
printf("%lld\n", ans);
}
后来上网看了题解发现这个题目还可以用dp写,感觉dp好理解很多。
dp[i][j] 表示前以 i 为右端点,对m取余为 j 时的最大值。
所以转移方程就很好写了
j== 0 dp[i][j]=max(dp[i-1][m-1]+a[i]-k,a[i]-k)
j!=0 dp[i][j]=dp[i-1][j-1]+a[i]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5 + ;
ll dp[maxn][];
ll a[maxn];
int main()
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n; i++) scanf("%lld", &a[i]);
for(int i=;i<=n;i++)
{
for (int j = ; j < m; j++) dp[i][j] = -inf64;
}
dp[][m - ] = ;
ll ans = ;
for(int i=;i<=n;i++)
{
for(int j=;j<m;j++)
{
if (j == ) dp[i][j] = max(dp[i - ][m - ] + a[i] - k, a[i] - k);
else dp[i][j] = dp[i - ][j - ] + a[i];
ans = max(ans, dp[i][j]);
}
}
printf("%lld\n", ans);
return ;
}
dp
D. Yet Another Subarray Problem 思维 难 dp更好理解的更多相关文章
- 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 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...
- 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的和最大的非空连续子数组.比如 ...
- ZOJ Problem Set - 3822Domination(DP)
ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 【数学+分块】
一.题目 D. Yet Another Subarray Problem 二.分析 公式的推导时参考的洛谷聚聚们的推导 重点是公式的推导,推导出公式后,分块是很容易想的.但是很容易写炸. 1 有些地方 ...
- 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集
[题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...
- CodeForces 1197D Yet Another Subarray Problem
Time limit 2000 ms Memory limit 262144 kB Source Educational Codeforces Round 69 (Rated for Div. 2) ...
随机推荐
- ElasticSearch 常用查询语句
为了演示不同类型的 ElasticSearch 的查询,我们将使用书文档信息的集合(有以下字段:title(标题), authors(作者), summary(摘要), publish_date(发布 ...
- AJ学IOS(26)UI之iOS抽屉效果小Demo
AJ分享,必须精品 先看效果 实现过程 第一步,把三个view设置好,还有颜色 #warning 第一步 - (void)addChildView { // left UIView *leftView ...
- Linux环境下django初入
python -m pip install --upgrade pip 终端中 一. 创建项目: 1.django-admin startproject mysite(第一种比较好) 2.django ...
- stand up meeting 12/2/2015
part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 将logic部分提供的delete接口接入,与logic,数据库部分沟通实现了add/delete按钮的复用:解决UI部 ...
- 9.回文数-LeetCode
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: 从左向右读, ...
- secureCRT常用操作
直接在网上搜索下载 连接 选择协议 Telnet Telnet连接时,进入输入密码 串口 连接串口后,查看本机连接的串口号 新建的时候,选择相同的串口号 RTS需要勾选掉 外观字体 选项 => ...
- [linux][MongoDB] mongodb学习(一):MongoDB安装、管理工具、
参考原文:http://www.cnblogs.com/kaituorensheng/p/5118226.html linux安装完美实现! 1. mongoDB安装.启动.关闭 1.1 下载安装包 ...
- django-admin和manage.py用法
官网文档地址:django-admin和manage.py 金句: 所有的天赋,都来自于你对你喜欢的某种事物的模仿与学习,否则你就不会有这种天赋. 开篇话: 我们在Django开发过程中,命令行执行最 ...
- 2层感知机(神经网络)实现非线性回归(非线性拟合)【pytorch】
import torch import numpy import random from torch.autograd import Variable import torch.nn.function ...
- 一不小心实现了RPC
前言 随着最近关注 cim 项目的人越发增多,导致提的问题以及 Bug 也在增加,在修复问题的过程中难免代码洁癖又上来了. 看着一两年前写的东西总是怀疑这真的是出自自己手里嘛?有些地方实在忍不住了便开 ...