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

4 篇文章 0 订阅
2 篇文章 0 订阅

题目:

Problem 2013 A short problem
Accept: 364    Submit: 1104
Time Limit: 1000 mSec    Memory Limit : 32768 KB
 Problem 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 1
 Sample Output
1
-1
 Source
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 local
    debug = 1;
#endif
    if (debug) {
        freopen("in.txt", "r", stdin);
        //freopen("out", "w", stdout);
        //printf("debuging output:\n");
    }
    int t;
    scd(t);
    while(t--)
        work();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值