FOJ 2013 动态规划:求最长子区间

2023-11-02 11:58

本文主要是介绍FOJ 2013 动态规划:求最长子区间,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

Problem 2013 A short problem
Accept: 364    Submit: 1104
Time Limit: 1000 mSec    Memory Limit : 32768 KBProblem Description
The description of this problem is very short. Now give you a string(length N), and ask you the max sum of the substring which the length can't small than M.Input
The first line is one integer T(T≤20) indicates the number of the test cases. Then for every case, the first line is two integer N(1≤N≤1000000) and M(1≤M≤N).Then one line contains N integer indicate the number. All the number is between -10000 and 10000.Output
Output one line with an integer.Sample Input
2
5 1
1 -2 -2 -2 1
5 2
1 -2 -2 -2 1Sample Output
1
-1Source
FOJ有奖月赛-201103

前提知识:

hdu 1003题解:
这题其实很hdu的1003题十分相似
先详细介绍一下1003
hdu1003比这题少了一个长度要大于m的限定条件,但是其实还是一样的
假设数字 1 -2 -2 -2 1
那么以1结尾就有:

a[1] a [ 1 ]

那么以2结尾的就有
a[1]a[2]ora[2] a [ 1 ] a [ 2 ] o r a [ 2 ]

那么以3结尾的就有
a[1]a[2]a[3]ora[2]a[3]ora[3] a [ 1 ] a [ 2 ] a [ 3 ] o r a [ 2 ] a [ 3 ] o r a [ 3 ]

那么以4结尾的就有
a[1]a[2]a[3]a[4]ora[2]a[3]a[4]ora[3]a[4]ora[4] a [ 1 ] a [ 2 ] a [ 3 ] a [ 4 ] o r a [ 2 ] a [ 3 ] a [ 4 ] o r a [ 3 ] a [ 4 ] o r a [ 4 ]

因为我们是求最值只要把最值保存下来就好了.
得到转移公式
dp[i]=max(dp[i1]+a[i],a[i]); d p [ i ] = m a x ( d p [ i − 1 ] + a [ i ] , a [ i ] ) ;

现在是FOJ2013这题的题解

首先我们列出1到m的前缀和

a1a2a...3am a 1 a 2 a 3 . . . a m

下一项我们可以知道是
a1a2a...3amam+1ora2a...3amam+1 a 1 a 2 a 3 . . . a m a m + 1 o r a 2 a 3 . . . a m a m + 1

同理推出去,得到转移方程
dp[i]=max(dp[i1]+a[i],sum[i]sum[im]) d p [ i ] = m a x ( d p [ i − 1 ] + a [ i ] , s u m [ i ] − s u m [ i − m ] )

需要记录的是ans但是ans不一定是dp[n]因为最后可能断开,所以我们要找其中最大的dp

#ifdef local
#include    <ctime>
#endif
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
#define rep(i,e) for(int i=0;i<e;i++)
#define rep1(i,e) for(int i=1;i<=e;i++)
#define repx(i,x,e) for(int i=x;i<=e;i++)
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define test(a) cout<<a<<endl
#define test2(a,b) cout<<a<<" "<<b<<endl
#define test3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
typedef unsigned long long ull;
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 10;
const int mod = 10000007;
int cnt = 1;
long long  dp[N];
long long sum[N];
int a[N];
void work(){int n,m;scdd(n,m);for(int i =1;i<=n;i++){scd(a[i]);sum[i]=sum[i-1]+a[i];}dp[m-1]=sum[m-1];ll ans=-inf;for(int i=m;i<=n;i++){dp[i]=max(dp[i-1]+a[i],sum[i]-sum[i-m]);if(ans<dp[i]){ans=dp[i];}}printf("%lld\n",ans);
}
int main() {int debug = 0;
#ifdef localdebug = 1;
#endifif (debug) {freopen("in.txt", "r", stdin);//freopen("out", "w", stdout);//printf("debuging output:\n");}int t;scd(t);while(t--)work();
}

这篇关于FOJ 2013 动态规划:求最长子区间的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/330427

相关文章

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

动态规划---打家劫舍

题目: 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。 给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。 思路: 动态规划五部曲: 1.确定dp数组及含义 dp数组是一维数组,dp[i]代表

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

软考系统规划与管理师考试证书含金量高吗?

2024年软考系统规划与管理师考试报名时间节点: 报名时间:2024年上半年软考将于3月中旬陆续开始报名 考试时间:上半年5月25日到28日,下半年11月9日到12日 分数线:所有科目成绩均须达到45分以上(包括45分)方可通过考试 成绩查询:可在“中国计算机技术职业资格网”上查询软考成绩 出成绩时间:预计在11月左右 证书领取时间:一般在考试成绩公布后3~4个月,各地领取时间有所不同

uva 10131 最长子序列

题意: 给大象的体重和智商,求体重按从大到小,智商从高到低的最长子序列,并输出路径。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vect

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

代码随想录冲冲冲 Day39 动态规划Part7

198. 打家劫舍 dp数组的意义是在第i位的时候偷的最大钱数是多少 如果nums的size为0 总价值当然就是0 如果nums的size为1 总价值是nums[0] 遍历顺序就是从小到大遍历 之后是递推公式 对于dp[i]的最大价值来说有两种可能 1.偷第i个 那么最大价值就是dp[i-2]+nums[i] 2.不偷第i个 那么价值就是dp[i-1] 之后取这两个的最大值就是d

hdu4267区间统计

题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import

hdu4417区间统计

给你一个数列{An},然后有m次查询,每次查询一段区间 [l,r] <= h 的值的个数。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamRead

hdu3333区间统计

题目大意:求一个区间内不重复数字的和,例如1 1 1 3,区间[1,4]的和为4。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;