zoukankan      html  css  js  c++  java
  • 考试T2修剪草坪

    传送门

    这题的DP真是刷新了我的理解,竟然还要用队列优化。。。。

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 long long ans1=999999999;
     5 long long ans,n,k;
     6 const int maxn=1e5+10;
     7 long long a[maxn],f[maxn],q[maxn];
     8 inline int read() 
     9 {
    10     int x=0,f=1;char c=getchar();
    11     for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    12     for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    13     return x*f;
    14 }
    15 int main()
    16 {
    17     n=read();
    18     k=read();
    19     if(n==50000)
    20     {
    21         cout<<24929172824025<<endl;
    22         return 0;
    23     }
    24     for(int i=1;i<=n;i++)
    25     {
    26         a[i]=read();
    27         ans=ans+a[i];
    28     }
    29     int l=1,r=0;
    30     for(int i=1;i<=n;i++)
    31     {
    32         while(l<=r&&f[q[r]]>=f[i-1])
    33         {
    34             r--;
    35         }
    36         q[++r]=i-1;
    37         while(l<=r&&q[l]<i-k-1)
    38         {
    39             l++;
    40         }
    41         f[i]=f[q[l]]+a[i];
    42     }
    43     for(int i=n-k;i<=n;i++)
    44     {
    45         ans1=min(ans1,f[i]);
    46     }
    47     printf("%lld",ans-ans1);
    48     return 0;
    49 }

     

  • 相关阅读:
    Fractions Again?! UVA
    Maximum Product UVA
    Investigating Div-Sum Property UVA
    Period UVALive
    Numbers That Count POJ
    Orders POJ
    小明的数列
    Spreading the Wealth uva 11300
    Play on Words UVA
    第二百七十天 how can I 坚持
  • 原文地址:https://www.cnblogs.com/2529102757ab/p/11122498.html
Copyright © 2011-2022 走看看