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. 给网站添加免费Https SSL证书

    基于阿里云的云盾证书服务,系统是centos6.8,web服务器是nginx1.8.0,简单记录下踩坑情况. 申请证书 登录阿里云控制台→安全(云盾)→证书服务→购买证书(https://common ...

  2. ssm lodop打印图片不显示

    在打印预览的时候图片就是不显示 最终解决方案就是修改过滤器

  3. (DP)51NOD 1118 机器人走方格

    M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果. Input 第1行,2个数M,N,中间用空格隔开.( ...

  4. redis的两种备份方式

    Redis提供了两种持久化选项,分别是RDB和AOF. 默认情况下60秒刷新到disk一次[save 60 10000 当有1w条keys数据被改变时],Redis的数据集保存在叫dump.rdb一个 ...

  5. mui 文件上传注意问题

    1. mui 文件上传 key对应后台接收参数名,但对对于多文件上传就没办法了,addFile 的key不能重复 task.addFile( "_www/a.doc", {key: ...

  6. 【洛谷4721】【模板】分治FFT(CDQ分治_NTT)

    题目: 洛谷 4721 分析: 我觉得这个 "分治 FFT " 不能算一种特殊的 FFT ,只是 CDQ 分治里套了个用 FFT (或 NTT)计算的过程,二者是并列关系而不是偏正 ...

  7. MySql 同表复制数据 可以改变数据

    Mysql语法: INSERT INTO 表名 (字段) SELECT 字段 FROM 表名 WHERE 条件: 如果要修改其中某一个字段,在查询语句中:x(要改变的值) as 字段名. eg: IN ...

  8. 项目需求会__前端er定位的思考~

    一.页面展示-----针对前端部分:后台的东西(功能.样式)不考虑! 二.动态效果------能不能实现! 三.接口数据------怎么传数据! 四.兼容性--------兼容到哪个版本浏览器! 五. ...

  9. Vue组件之间通信的三种方式

    最近在看梁颠编著的<Vue.js实战>一书,感觉颇有收获,特此记录一些比价实用的技巧. 组件是MVVM框架的核心设计思想,将各功能点组件化更利于我们在项目中复用,这类似于我们服务端面向对象 ...

  10. springboot与dubbo整合入门(三种方式)

    Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...