• hdu 3415 Max Sum of MaxKsubsequence


    http://acm.hdu.edu.cn/showproblem.php?pid=3415

      不难的单调队列,不过刚开始的时候开的队列不够大,所以数组越界了。不过最不解的是hdu返回的是wa而不是runtime error,搞到我以为是单调队列写错什么了,所以不停debug,debug了好多组数据,最终才想到队列的大小是会超出n的,于是改过来以后就ac了!

      时间相当理想,才46ms!

    下面是我的代码:

    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstdlib>
     4 
     5 const int maxn = 100001;
     6 const int inf = 0x7fffffff;
     7 int q[maxn << 1], ind[maxn << 1], qh, qt;
     8 int begin, end, sv[maxn];
     9 
    10 void scan(int &n){
    11     char ch;
    12 
    13     while ((ch = getchar()) != '-' && ('0' > ch || ch > '9'));
    14     if (ch != '-'){
    15         n = ch - '0';
    16         while ((ch = getchar()) >= '0' && ch <= '9')
    17             n = n * 10 + ch - '0';
    18     }
    19     else{
    20         n = 0;
    21         while ((ch = getchar()) >= '0' && ch <= '9')
    22             n = n * 10 + '0' - ch;
    23     }
    24 }
    25 
    26 int main(){
    27 #ifndef ONLINE_JUDGE
    28     freopen("in", "r", stdin);
    29 #endif
    30     int N;
    31     int n, k, sum, tmp, maxsum, ci;
    32 
    33     scan(N);
    34     while (N--){
    35         scan(n); scan(k);
    36 
    37         qh = sum = begin = q[0] = 0;
    38         qt = 1;
    39         ind[0] = -1;
    40         maxsum = -inf;
    41         end = n;
    42 
    43         for (int i = 0; i < n; i++){
    44             scan(sv[i]);
    45             sum += sv[i];
    46             if (i - ind[qh] > k) qh++;
    47 
    48             tmp = sum - q[qh];
    49             if (tmp > maxsum){
    50                 maxsum = tmp;
    51                 begin = ind[qh] + 1;
    52                 end = i;
    53             }
    54             while (qh < qt && q[qt - 1] >= sum) qt--;
    55             q[qt] = sum;
    56             ind[qt++] = i;
    57         }
    58 
    59         for (int i = 0; i < k; i++){
    60             ci = i + n;
    61 
    62             sum += sv[i];
    63             if (ci - ind[qh] > k) qh++;
    64 
    65             tmp = sum - q[qh];
    66             if (tmp > maxsum){
    67                 maxsum = tmp;
    68                 begin = ind[qh] + 1;
    69                 end = ci;
    70             }
    71             while (qh < qt && q[qt - 1] >= sum) qt--;
    72             q[qt] = sum;
    73             ind[qt++] = ci;
    74         }
    75 
    76         printf("%d %d %d\n", maxsum, begin % n + 1, end % n + 1);
    77     }
    78 
    79     return 0;
    80 }

    ——written by Lyon

  • 相关阅读:
    arpspoof局域网断网攻击
    2019-2020 SEERC 2019
    2019-2020 XX Open Cup, Grand Prix of Korea
    欧拉函数板子
    Syncthing – 数据同步利器
    程序员的修养 -- 如何写日志(logging)
    css基础
    vim永久设置主题
    基金选择
    如何查看ntp端口是否正常
  • 原文地址:https://www.cnblogs.com/LyonLys/p/hdu_3415_Lyon.html
Copyright © 2020-2023  润新知