zoukankan      html  css  js  c++  java
  • luogu P2034 选择数字 单调队列优化dp 脑残行为,导致wa了很多遍

    #include<map>
    #include<queue>
    #include<time.h>
    #include<limits.h>
    #include<cmath>
    #include<ostream>
    #include<iterator>
    #include<set>
    #include<stack>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define rep_1(i,m,n) for(int i=m;i<=n;i++)
    #define mem(st) memset(st,0,sizeof st)
    int read()
    {
        int res=0,ch,flag=0;
        if((ch=getchar())=='-')             //判断正负
            flag=1;
        else if(ch>='0'&&ch<='9')           //得到完整的数
            res=ch-'0';
        while((ch=getchar())>='0'&&ch<='9')
            res=res*10+ch-'0';
        return flag?-res:res;
    }
    typedef long long ll;
    typedef pair<int,int> pii;
    typedef unsigned long long ull;
    typedef pair<double,double> pdd;
    const int inf = 0x3f3f3f3f;
    const int N=1e5+10;
    #define int long long
    int a[N];
    int f[N];
    int q[N];
    int hh,tt;
    int sum;
    //选几个数
    //任意两个数之间的数字,不能超过k个
    signed main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=1; i<=n; i++)
            cin>>a[i],sum+=a[i];
        hh=1,tt=1;
        q[tt++]=0;
        for(int i=1; i<=n; i++)
        {
            while(hh<tt&&i-q[hh]-1>m)
                hh++;
            f[i]=f[q[hh]]+a[i];
            while(hh<tt&&f[q[tt-1]]>=f[i])
                tt--;
            q[tt++]=i;
        }
        int ans=0;
        for(int i=n-m; i<=n; i++)
            ans=max(ans,sum-f[i]);
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    经济--1...19
    经济
    金融--
    经济--番外篇
    经济--基金问答
    经济--如何买基金?
    PHP面向对象常见的关键字和魔术方法
    php对象中类的继承性访问类型控制
    详解PHP的__set()、__get()、__isset()、unset()四个方法
    子类重载父类的方法“parent:方法名”
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/13036920.html
Copyright © 2011-2022 走看看