【单调栈思路】Minimal Subarray Length UVALive - 6609

Think:
1知识点:单调栈思路
2题目分析&&反思:比赛时自己用模拟用队列来维护对于当前结点的前面累加和符合大于等于k的最短长度,但是一直Wrong Answer,晚上稍微平息了一点心态,搜了搜题解,发现也是一种维护单调递增序列的思想,和自己比赛时和队友讨论的思路基本一致,之后改了改自己比赛时候写的代码,还是Wrong Answer,内心有点累崩的感觉,希望自己能够知耻后勇,今天比赛队伍基本垫底,赛后听了yj的话,有点伤感,自己一定要加倍努力,成为队伍中的强有力支撑

vjudge题目链接

建议参考博客

以下为Wrong Answer代码——暂未找到错误点

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;

int op, tp, link[501400], a[501400];

int main(){
    int T, n, k, i, ans;
    long long sum;
    scanf("%d", &T);
    while(T--){
        bool flag = false;
        ans = inf;
        scanf("%d %d", &n, &k);
        for(i = 0; i < n; i++){
            scanf("%d", &a[i]);
            if(a[i] >= k){
                flag = true;
                ans = 1;
            }
        }
        if(!flag){
            op = tp = 0, sum = 0;
            for(i = 0; i < n; i++){
                if(sum <= 0){
                    op = 0;
                    tp = 0;
                    sum = 0;
                }
                sum += a[i];
                link[tp++] = a[i];
                while(1)
                {
                    bool cnt = false;
                    while(sum >= k && tp > op)
                    {
                        ans = min(ans, tp-op);
                        sum -= link[op];
                        op++;
                        cnt = true;
                    }
                    while(link[op] <= 0 && cnt)
                    {
                        sum = sum - link[op];
                        op++;
                    }
                    if(!cnt)
                        break;
                }
            }
        }
        if(ans == inf) printf("-1\n");
        else printf("%d\n", ans);
    }
    return 0;
}

以下为Accepted代码——借鉴前辈代码,谢谢前辈

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;
const int N = 5e5 + 4e2;

int a[N], sum[N], increase[N];

int main(){
    int T, n, k, i, ans, l, r;
    scanf("%d", &T);
    while(T--){
        scanf("%d %d", &n, &k);
        sum[0] = 0;
        for(i = 1; i <= n; i++){
            scanf("%d", &a[i]);
            sum[i] = sum[i-1] + a[i];
        }
        l = r = 0, ans = inf;
        increase[r++] = 1;
        for(i = 2; i <= n; i++){
            while(l < r && sum[increase[r-1]] >= sum[i])
                r--;
            increase[r++] = i;
            while(r > l + 1 && sum[i] - sum[increase[l]] >= k){
                ans = min(ans, i - increase[l]);
                l++;
            }
        }
        if(ans == inf)
            printf("-1\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值