zoukankan      html  css  js  c++  java
  • 6609

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4620

     

     

    You are given an integer sequence of length N and another value X. You have to find a contiguous
    subsequence of the given sequence such that the sum is greater or equal to X. And you have to find
    that segment with minimal length.
    Input
    First line of the input file contains T the number of test cases. Each test case starts with a line
    containing 2 integers N (1 ≤ N ≤ 500000) and X (−109 ≤ X ≤ 109
    ). Next line contains N integers
    denoting the elements of the sequence. These integers will be between −109
    to 109
    inclusive.
    Output
    For each test case output the minimum length of the sub array whose sum is greater or equal to X. If
    there is no such array, output ‘-1’.
    Sample Input
    3
    5 4
    1 2 1 2 1
    6 -2
    -5 -6 -7 -8 -9 -10
    5 3
    -1 1 1 1 -1
    Sample Output
    3
    -1
    3

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    const int N = 510000;
    const int INF = 0x3fffffff;
    typedef long long LL;
    #define met(a,b) (memset(a,b,sizeof(a)))
    
    struct node
    {
        LL x;
        int Start;
    } sum[N];
    
    LL a[N];
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            int n, i, Min=N, X, Start;
            LL x;
    
            met(a, 0);
            met(sum, 0);
    
            scanf("%d%lld", &n, &x);
    
            for(i=1; i<=n; i++)
                scanf("%lld", &a[i]);
    
            for(i=1; i<=n; i++)
            {
                if(sum[i-1].x<=0 || i==1)
                {
                    sum[i].x = a[i];
                    sum[i].Start = i;
                }
                else
                {
                    sum[i].x = sum[i-1].x + a[i];
                    sum[i].Start = sum[i-1].Start;
                }
                if(sum[i].x>=x)
                {
                    Min = min(Min, i-sum[i].Start+1);
                    X = sum[i].x, Start = sum[i].Start;
                    while(X>=0 && Start<=i)
                    {
                        X -= a[Start];
                        Start++;
                        if(X >= x)
                        {
                            sum[i].x = X;
                            sum[i].Start = Start;
                            Min = min(Min, i-Start+1);
                        }
                    }
                }
            }
    
            printf("%d
    ", Min!=N?Min:-1);
        }
    
        return 0;
    }
    
    
    /**
    
    300
    5 4
    1 2 1 2 1
    6 -2
    -5 -6 -7 -8 -9 -10
    5 3
    -1 1 1 1 -1
    8 6
    1 1 1 1 1 2 3 4
    6 5
    4 -3 4 -1 2 2
    6 6
    -5 1 2 4 1 3
    6 5
    4 -3 4 -1 -2 2
    6 5
    -1 -1 -2 3 -2 5
    4 5
    3 -2 4 1
    8 6
    1 1 1 1 1 3 1 2
    8 6
    1 1 1 1 1 3 2 1
    8 6
    1 1 1 1 1 3 1 1
    
    
    **/
  • 相关阅读:
    [LeetCode] 75. 颜色分类(荷兰国旗)
    [LeetCode] 347. 前K个高频元素
    CMU-14445 数据库原理 汇总
    MIT-6.824 操作系统 汇总
    发布一个基于协程和事件循环的c++网络库
    记录一次gdb debug经历
    彻底弄懂UTF-8、Unicode、宽字符、locale
    CPU使用率原理及计算方式
    TCP使用注意事项总结
    STL-vector
  • 原文地址:https://www.cnblogs.com/YY56/p/5473135.html
Copyright © 2011-2022 走看看