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 = ;
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, );
met(sum, ); scanf("%d%lld", &n, &x); for(i=; i<=n; i++)
scanf("%lld", &a[i]); for(i=; i<=n; i++)
{
if(sum[i-].x<= || i==)
{
sum[i].x = a[i];
sum[i].Start = i;
}
else
{
sum[i].x = sum[i-].x + a[i];
sum[i].Start = sum[i-].Start;
}
if(sum[i].x>=x)
{
Min = min(Min, i-sum[i].Start+);
X = sum[i].x, Start = sum[i].Start;
while(X>= && Start<=i)
{
X -= a[Start];
Start++;
if(X >= x)
{
sum[i].x = X;
sum[i].Start = Start;
Min = min(Min, i-Start+);
}
}
}
} printf("%d\n", Min!=N?Min:-);
} return ;
} /** 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 **/

6609 - Minimal Subarray Length的更多相关文章

  1. UVALive 6609 Minimal Subarray Length(RMQ-ST+二分)

    题意:给定长度为N的数组,求一段连续的元素之和大于等于K,并且让这段元素的长度最小,输出最小长度即可,若不存在这样的元素集合,则输出-1 题目链接:UVAlive 6609 做法:做一个前缀和pref ...

  2. UVALive 6609 Minimal Subarray Length (查找+构建排序数组)

    描述:给定n个整数元素,求出长度最小的一段连续元素,使得这段元素的和sum >= X. 对整个数组先求出sum[i],表示前i个元素的和,然后依次求出以a[i]为起点的,总和>= X的最小 ...

  3. UVA 12697 Minimal Subarray Length

    Minimal Subarray Length Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...

  4. E - Minimal Subarray Length(连续区间和)

    题目链接 题意:给出n个数,求加和大于x的最短区间的区间长度. 如果前i个数字和为y,那么如果前j数字的和小于等于y-x,那么i-j就是一种可能的情况,我们对于所有的i找出前面最大的j就可以了,因为数 ...

  5. Individual Contest #1 and Private Training #1

    第一次的增补赛,也是第一场个人排位赛,讲道理打的和屎一样,手速题卡了好久还WA了好多发,难题又切不出来,这种情况是最尴尬的吧! Individual Contest #1: Ploblem D: 题意 ...

  6. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  7. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  8. [poj1113][Wall] (水平序+graham算法 求凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

  9. *HDU 1392 计算几何

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. .net 下存取Excel的利器(第三方)

    NPOI 资料: NPOI是什么?能干什么? 个人理解:NPOI是个操作Excel的第三方类库.可以在没有安装Office的情况下,处理Excel文件. 官方网站-NPOI指南:http://www. ...

  2. PAT 1036 跟奥巴马一起编程(15)(代码)

    1036 跟奥巴马一起编程(15)(15 分) 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝"计算机科学教育周& ...

  3. 优化myeclipse启动速度以及解决内存不足问题

    解决myeclipse内存不足问题: 使用 MyEclipse 开发项目后,随着项目文件的增多,以及运行时间的增加,实际上 MyEclipse 所消耗的内存是会一直增大的,有的时候会出现 MyEcli ...

  4. hisat2+stringtie+ballgown

    hisat2+stringtie+ballgown Posted on 2016年11月25日 早在去年九月,我就写个博文说 RNA-seq流程需要进化啦!http://www.bio-info-tr ...

  5. divinglaravel.com

    https://divinglaravel.com/ 这是一个深入 laravel 的学习网站

  6. eclipse构建maven的web项目(转载)

    eclipse构建maven的web项目 分类: java opensource2013-12-25 16:22 43人阅读 评论(0) 收藏 举报 maven框架webappwebeclipse 使 ...

  7. Bing Developer Assistant开发随记

    Thumb很适合用来做拖动效果的,不会让鼠标轻易跑掉. Combo中的选项是当字符串输入并激发事件后自动加入的,可使用IVsUIShell.SetMRUComboText(GuidList.guidO ...

  8. spring学习 八 面向切面编程(AOP)概述

    注:本文大部分参考   --------------------- 本文来自 -望远- 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yanquan345/artic ...

  9. mysql之存储引擎和文件配置

    (查看系统服务,在运行里输入services.msc) 补充:将mysql做成系统服务:mysqld --install 取消:mysqld --romove 在服务中可以直接鼠标操作mysql服务的 ...

  10. 2018.12.23 bzoj4516: [Sdoi2016]生成魔咒(后缀自动机)

    传送门 samsamsam入门题. 题意简述:给出一个串让你依次插入字符,求每次插入字符之后不同子串的数量. 显然每次的变化量只跟新出现的nnn个后缀有关系,那么显然就是maxlenp−maxlenl ...