Minimal Subarray Length

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 6609
64-bit integer IO format: %lld      Java class name: Main

 
 

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

解题:先求和。维护一个队列,下标单调,值也单调。对于i,j.如果sum[i] <= sum[j] && i > j ,那么i肯定比j好。去掉j.

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
LL sum[maxn] = {};
int n,x,inc[maxn];
int main() {
int t,i,lt,rt,ans;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&x);
for(i = ; i <= n; i++){
scanf("%lld",sum+i);
sum[i] += sum[i-];
}
lt = ;
inc[] = rt = ;
ans = n+;
for(i = ; i <= n; i++){
while(lt < rt && sum[i] <= sum[inc[rt-]]) rt--;
inc[rt++] = i;
while(lt + < rt && sum[i] - sum[inc[lt]] >= x){
ans = min(ans,i-inc[lt]);
lt++;
}
}
ans == n+?puts("-1"):printf("%lld\n",ans);
}
return ;
}

UVA 12697 Minimal Subarray Length的更多相关文章

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

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

  2. 6609 - Minimal Subarray Length

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

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

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

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

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

  5. uva.10020 Minimal coverage(贪心)

    10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...

  6. 【区间覆盖问题】uva 10020 - Minimal coverage

    可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...

  7. UVa 10020 - Minimal coverage(区间覆盖并贪心)

    Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...

  8. uva 10020 Minimal coverage

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

随机推荐

  1. 从缓冲上看阻塞与非阻塞socket在发送接收上的区别(转载)

    转自:http://blog.chinaunix.net/uid-24517549-id-4044877.html   首先socket在默认情况下是阻塞状态的,这就使得发送以及接收操作处于阻塞的状态 ...

  2. E20180120-hm

    derive vt. 得到,导出; 源于,来自; (从…中) 提取; hierarchy  n. [计] 分层,层次; 等级制度; 统治集团; 天使的级别或等级; inheritance  n. 继承 ...

  3. 虚拟机安装cenos7后ifcfg看网卡无inet地址掩码等信息

    在虚拟机安装centos7,进入系统使用ifconfig命令时,只有lo网卡( 127.0.0.1的ip地址)和eno16777736网卡,而且此网卡没有inet地址.掩码等信息. 这时候查看/etc ...

  4. php insteadof 作用

    PHP5的另一个新成员是instdnceof关键字.使用这个关键字可以确定一个对象是类的实例.类的子类,还是实现了某个特定接口,并进行相应的操作.在某些情况下,我们希望确定某个类是否特定的类型,或者是 ...

  5. 继承static的注意点

    继承static的注意点 singleton模式会使用 <?php class Auth { protected static $_instance = null; /** * 单用例入口 * ...

  6. python 模块-easygui.buttonbox

    2018-03-0315:43:11 ): Yes_or_No = easygui.buttonbox("是否良品?", choices=['Yes', 'No', '退出']) ...

  7. android开发小内容

    EditText弹出输入数字:android:inputType="phone"

  8. Computed Properties vs Property Requirements - protocol

    In addition to stored properties, classes, structures, and enumerations can define computed properti ...

  9. UI布局术语

    horizontal, vertical top, left, bottom, right UIEdgeInsets margin与padding Interpreting Values Values ...

  10. DropDownList 递归绑定分子公司信息

    /// <summary> /// 绑定下拉框 /// </summary> /// <param name="ddl">绑定控件名称</ ...