hdu 3415 Max Sum of Max-K-sub-sequence 单调队列优化DP

题目链接: https://www.cnblogs.com/Draymonder/p/9536681.html

同上一篇文章,只是 需要记录最大值的开始和结束的位置

#include <iostream>
#include <string.h>
#include <cmath>
using namespace std;
const int N = 1e5 + 10;

int n,k;
int s[N<<1],sum[N<<1];
int Q[N<<1];

int main ()
{
    freopen("in.txt","r",stdin);
    int T; scanf("%d",&T);
    while (T--) {
        memset(Q,0,sizeof(Q));
        memset(sum,0,sizeof(sum));
        scanf("%d %d", &n, &k);
        for(int i=1; i<=n; i++)
            scanf("%d",&s[i]), s[i+n]=s[i];
        n<<=1;
        for(int i=1;i<=n;i++)
            sum[i] = sum[i-1]+s[i]; //printf("%d ",sum[i]);
        // for(int i=1;i<=n;i++)
        //     printf("%d ", sum[i]);
        // puts("");
        int mx = -N, ans1=n+1,ans2=-1;
        for(int i=1;i<=n;i++) {
            if(i <= k) {
                if(sum[i] > mx) {
                    mx = sum[i];
                    ans1=1,ans2=i;
                }
            }
        }
        //printf("%d %d\n",ans1, ans2);
        int st=0,ed=0;
        for(int i=1; i<=n; i++)    {
            //[i-k+1,i] 区间长度为k
            while (st < ed && Q[st] < i-k)
                st++;
            if(st < ed) {
                int ans = sum[i] - sum[Q[st]];
                if(mx < ans) {
                    mx =ans;
                    ans1 = Q[st]+1;
                    ans2 = i;
                }
            }
            while (st < ed && sum[i] <= sum[Q[ed-1]])
                ed--;
            Q[ed++] = i;
            //cout << i<<" "<<ans1 <<" "<<ans2<<endl;
        }
        printf("%d %d %d\n", mx, ans1>n/2?ans1-n/2:ans1, ans2>n/2?ans2-n/2:ans2);

    }
    return 0;
}

原文地址:https://www.cnblogs.com/Draymonder/p/9536938.html

时间: 2024-10-13 22:31:11

hdu 3415 Max Sum of Max-K-sub-sequence 单调队列优化DP的相关文章

HDU 4122 Alice&#39;s mooncake shop 单调队列优化dp

Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4122 Description The Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest festival celebrated by Ch

hdu 3401 单调队列优化+dp

http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5188    Accepted Submission(s): 1776 Problem Description Recently, lxhgww is addicted to stoc

HDU 4374 One hundred layer(单调队列优化dp)

题意:有一个n*m的矩阵,每格有一个分数,一个人站在(1,x)位置,在每一行中,他只能朝一个方向走(向左或向右),且最多走t步,问走到最后第n行得到的最大分数. 思路:不难想到状态转移方程dp[i][j] = max(dp[i-1][k]+sum[i][j]-sum[i][k-1]),(k<j) 移项得 dp[i][j]-sum[i][j] = max(dp[i-1][k] - sum[i][k-1]); 方程右侧与i,j无关,所以可以用单调队列维护max(dp[i-1][k] - sum[i]

hdu 3401(单调队列优化dp)

注意:这题题意是有操作的天数相隔要大于w 然后列出状态转移方程就可以发现,可以用优点队列优化啦. 构造状态dp[i][j]表示第i 天拥有 j只股票的时候,赚了多少钱 状态转移有: 1.从前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.从前i-W-1天买进一些股: dp[i][j]=max(dp[i-W-1][k]-(j-k)*AP[i],dp[i][j]) 3.从i-W-1天卖掉一些股: dp[i][j]=max(dp[i-W-1][k]+(k-j)*

hdu 5945 Fxx and game 单调队列优化dp

Fxx and game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Problem Description Young theoretical computer scientist Fxx designed a game for his students. In each game, you will get three integers X,k,t.In each s

HDU 3401 Trade dp 单调队列优化

戳这里:3401 题意:给出第 i 天的股票买卖价格(APi,BPi),以及每天股票买卖的数量上限(ASi,BSi),要求任两次交易需要间隔 W 天以上,即第 i 天交易,第 i + W + 1 天才能再交易,求最多能赚多少钱 思路:dp[i][j] = max(dp[i - 1][j], max(dp[f][k] - (j - k) * APi[i]), max(dp[f][k] + (k - j) * BPi[i])); 从式子中观察出,若两天都持有股票数 j 时,之后的那一天所赚的钱不小于

hdu 2191 (多重背包的单调队列优化)

多重背包单调队列优化是思想是.普通的dp为 dp[i][j]=max{dp[i-1][j-k*v[i]]+k*w[i]}; 其实你可以发现对能更新j是j和一个剩余类.也就是 0, v[i],2v[i],3v[i] ,4v[i]... 1 ,1+v[i],1+2v[i],1+3v[i] ........... v[i]-1,2*v[i]-1...... 更新值存在一个剩余类中,组与组之间不存在更新.那么实际上我们可以写dp写好这样 dp[b+x*v[i]]=max{ dp[b+k*v[i]]+(x

HDU 4374 One hundred layer DP的单调队列优化

One hundred layer Problem Description Now there is a game called the new man down 100th floor. The rules of this game is: 1.  At first you are at the 1st floor. And the floor moves up. Of course you can choose which part you will stay in the first ti

HDU 3401 Trade(单调队列优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股.两次操作(买入或卖出)中间必须相差W天.炒股时间为n.任意时间手中的股票不大于MaxP.求最大收益. dp[i][j]代表第i天手上有j股的最大收益,dp[i][j]=max(dp[i-1][j],dp[i-W][k]+(j-k)*ap[i],dp[i-W][k]+(k-j)*bp[i]); dp