1. 程式人生 > >牛客網多校訓練 訓練技巧

牛客網多校訓練 訓練技巧

bits 輸入 targe 單個 sdn namespace 能力 tps i+1

題目描述 來源

常州大學組織了新生寒假訓練一共N天,每天訓練可以獲得的訓練效果是Ei。但是如果連續訓練超過K天,萌新們會受不了而被勸退。 現在負責人想知道,如何安排能保證萌新不會被勸退並且能獲得最大的訓練效果。

輸入描述:

第一行:兩個用空格隔開的整數:N和K,1≤N≤100000,1≤K≤N
第二行到N+1行:第i+1行有一個整數,表示第N天的訓練效果是Ei,(0 <= Ei <= 1,000,000,000)

輸出描述:

第一行:單個整數,表示最大的能力之和
示例1

輸入

5 2 
1
2
3
4 
5

輸出

12

說明

(除了第三天以外每天都在訓練,總訓練效果為1+2+4+5=12)

備註:

1≤n≤100,000
代碼來源
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 1e5+11;
const LL inf = 0x3f3f3f3f3f3f3f3f ;
int q[N],st,ed;//q數組中的元素滿足遞增規律,作為單調棧
LL dp[N],a[N];
int main(){
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
         LL sum=0;
         for(int
i=1;i<=n;i++) scanf("%lld",&a[i]),sum+=a[i]; for(int i=1;i<=n;i++){ while(i-q[st]>k+1) st++; dp[i]=a[i]+dp[q[st]]; while(st<=ed && dp[q[ed]]>dp[i]) --ed; q[++ed]=i; } LL ans=inf; for(int i=n-k;i<=n;i++) ans=min(ans,dp[i]); printf(
"%lld\n",sum-ans); } return 0; }

牛客網多校訓練 訓練技巧