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 ;
}

最新文章

  1. mongodb安装及基础命令
  2. mysql按汉语拼音首字母排序
  3. 【转】C#程序打包安装部署之添加注册表项
  4. Ubuntu下安装可视化SVN客户端Rabbitvcs
  5. The secret code
  6. IntellijIdea中常用的快捷键
  7. JAVA多线程synchronized详解
  8. Linux shell入门基础(八)
  9. LeetCode_Combination Sum II
  10. iOS中UISearchBar(搜索框)使用总结
  11. 移动端开发(四):swiper.js
  12. linux下操作gpio寄存器的方法
  13. Java 第三章 选择结构
  14. java--银行业务调度系统
  15. URI和URL的区别 【转】
  16. 清北学堂4.28Day1(重大更新详见贪心例一)
  17. vs2019 cdkey 秘钥
  18. session与cookie的区别是什么?如果客户端禁用了cookie功能,将会对session有什么影响?
  19. chrome浏览器下载内容存放位置
  20. C#集合类型大揭秘

热门文章

  1. SQL Server 添加描述
  2. bzoj Strange Way to Express Integers【excrt】
  3. bzoj [Usaco2010 Hol]cowpol 奶牛政坛【树链剖分】
  4. android_app c++框架
  5. 基于ASP.Net Core开发一套通用后台框架记录-(总述)
  6. [C++ STL] 常用算法总结
  7. [转]ASP.NET MVC Domain Routing
  8. Javascript DOM 编程艺术(第二版)读书笔记——基本语法
  9. python自动化--模块操作之re、MySQL、Excel
  10. jdbc 实现分页