【BZOJ 2288: 【POJ Challenge】生日礼物】最大M字段和

115 篇文章 0 订阅
76 篇文章 0 订阅

BZOJ 2288
其实这题题意和HDU 1024我觉得是一样的
所以改了一下1024的代码 可能数据小了就过了
就是求做大M子段和

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1e6+5;
int n,m;
long long arr[N];
//int f[N][N];
long long dp[N],maxn[N];
int main(){
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i = 1;i<=n;++i) scanf("%lld",&arr[i]);
        memset(dp,0,sizeof(dp));
        memset(maxn,0,sizeof(maxn));
        long long nowans = 0;
        for(int i = 1;i<=m;++i)
        {
            nowans = -0x3f3f3f3f3f3f3f3f;
            for(int j = i ;j<=n;++j)
            {
                dp[j] = max(dp[j-1],maxn[j-1]) + arr[j];
                maxn[j-1] = nowans;
                nowans = max(dp[j],nowans);
            }
        }
        printf("%lld\n",max(nowans,1ll*0));
    }
    return 0;
}

7927ms
但是用线段树求就是贪心取出最大子段和 然后*-1放进去 贪心的去取
只要 192ms就过了
其实你*-1放进去以后就不会取到这个了 保证不相交
但是hdu数据好鬼 我根据bzoj题意交了一发hdu 过不了
原因是数据
6 2 -1 -2 -3 -4 -5 -6 (dp n和m换下
dp跑出来-3
线段树跑出来0
心态崩了

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 100025;
int arr[MAX_N],col[MAX_N<<2],lmx[MAX_N<<2],lmn[MAX_N<<2],rmx[MAX_N<<2],rmn[MAX_N<<2],sum[MAX_N<<2],lmnp[MAX_N<<2],rmnp[MAX_N<<2],lmxp[MAX_N<<2],rmxp[MAX_N<<2],mmx[MAX_N<<2],mmn[MAX_N<<2],mxl[MAX_N<<2],mxr[MAX_N<<2],mnl[MAX_N<<2],mnr[MAX_N<<2];
void up(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    if(sum[rt<<1]+lmx[rt<<1|1]>lmx[rt<<1]) lmx[rt] = sum[rt<<1] + lmx[rt<<1|1],lmxp[rt] = lmxp[rt<<1|1];
    else lmx[rt] = lmx[rt<<1],lmxp[rt] = lmxp[rt<<1];
    if(sum[rt<<1]+lmn[rt<<1|1]<lmn[rt<<1]) lmn[rt] = sum[rt<<1] + lmn[rt<<1|1],lmnp[rt] = lmnp[rt<<1|1];
    else lmn[rt] = lmn[rt<<1],lmnp[rt] = lmnp[rt<<1];
    if(sum[rt<<1|1]+rmx[rt<<1]>rmx[rt<<1|1]) rmx[rt] = sum[rt<<1|1] + rmx[rt<<1],rmxp[rt] = rmxp[rt<<1];
    else rmx[rt] = rmx[rt<<1|1],rmxp[rt] = rmxp[rt<<1|1];
    if(sum[rt<<1|1]+rmn[rt<<1]<rmn[rt<<1|1]) rmn[rt] = sum[rt<<1|1]+rmn[rt<<1],rmnp[rt] = rmnp[rt<<1];
    else rmn[rt] = rmn[rt<<1|1],rmnp[rt] = rmnp[rt<<1|1];
    mmx[rt] = mmx[rt<<1],mxl[rt] = mxl[rt<<1],mxr[rt] = mxr[rt<<1];
    if(mmx[rt<<1|1]>mmx[rt]) mmx[rt] = mmx[rt<<1|1],mxl[rt] = mxl[rt<<1|1],mxr[rt] = mxr[rt<<1|1];
    if(rmx[rt<<1]+lmx[rt<<1|1]>mmx[rt]) mmx[rt] = rmx[rt<<1] + lmx[rt<<1|1],mxl[rt] = rmxp[rt<<1],mxr[rt] = lmxp[rt<<1|1];
    mmn[rt] = mmn[rt<<1],mnl[rt] = mnl[rt<<1],mnr[rt] = mnr[rt<<1];
    if(mmn[rt<<1|1] <mmn[rt]) mmn[rt] = mmn[rt<<1|1],mnl[rt] = mnl[rt<<1|1],mnr[rt] = mnr[rt<<1|1];
    if(rmn[rt<<1]+lmn[rt<<1|1]<mmn[rt]) mmn[rt] = rmn[rt<<1]+lmn[rt<<1|1],mnl[rt] = rmnp[rt<<1],mnr[rt] = lmnp[rt<<1|1];

}
void pushnow(int rt)
{
    col[rt]^=1;sum[rt]*=-1;
    lmx[rt]*=-1;rmx[rt]*=-1;mmx[rt]*=-1;mmn[rt]*=-1;lmn[rt]*=-1;rmn[rt]*=-1;
    swap(lmx[rt],lmn[rt]);swap(rmx[rt],rmn[rt]);swap(mmx[rt],mmn[rt]);
    swap(lmxp[rt],lmnp[rt]);swap(rmxp[rt],rmnp[rt]);
    swap(mxl[rt],mnl[rt]);swap(mxr[rt],mnr[rt]);
}
void down(int rt)
{
    if(col[rt])
    {
        col[rt]^=1;
        pushnow(rt<<1);
        pushnow(rt<<1|1);
    }
}
void build(int rt,int l,int r)
{
    col[rt] = 0;
    lmxp[rt] = rmxp[rt] = lmnp[rt] = rmnp[rt] = mxl[rt] = mxr[rt] =mnl[rt] = mnr[rt] = 0;
    sum[rt] = lmx[rt] = lmn[rt] = rmx[rt] = rmn[rt] = mmx[rt] = mmn[rt] = 0;
    if(l==r)
    {
        lmxp[rt] = rmxp[rt] = lmnp[rt] = rmnp[rt] = mxl[rt] = mxr[rt] =mnl[rt] = mnr[rt] = l;
        sum[rt] = lmx[rt] = lmn[rt] = rmx[rt] = rmn[rt] = mmx[rt] = mmn[rt] = arr[l];
        return ;
    }
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    up(rt);
}
void update(int rt,int l,int r,int x,int y)
{
    if(x>r||y<l) return;
    if(x<=l&&r<=y)
    {
        pushnow(rt);
        return ;
    }
    down(rt);
    if(x>mid) update(rt<<1|1,mid+1,r,x,y);
    else if(y<=mid) update(rt<<1,l,mid,x,y);
    else update(rt<<1,l,mid,x,y),update(rt<<1|1,mid+1,r,x,y);
    up(rt);
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
    ll ans = 0;
    build(1,1,n);
    for(int i = 1;i<=m;++i)
    {
        //dbg(mmx[1]);
        if(mmx[1]<=0) break;
        ans+=mmx[1];
        update(1,1,n,mxl[1],mxr[1]);
    }
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值