6609 - Minimal Subarray Length
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的更多相关文章
- UVALive 6609 Minimal Subarray Length(RMQ-ST+二分)
题意:给定长度为N的数组,求一段连续的元素之和大于等于K,并且让这段元素的长度最小,输出最小长度即可,若不存在这样的元素集合,则输出-1 题目链接:UVAlive 6609 做法:做一个前缀和pref ...
- UVALive 6609 Minimal Subarray Length (查找+构建排序数组)
描述:给定n个整数元素,求出长度最小的一段连续元素,使得这段元素的和sum >= X. 对整个数组先求出sum[i],表示前i个元素的和,然后依次求出以a[i]为起点的,总和>= X的最小 ...
- UVA 12697 Minimal Subarray Length
Minimal Subarray Length Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA ...
- E - Minimal Subarray Length(连续区间和)
题目链接 题意:给出n个数,求加和大于x的最短区间的区间长度. 如果前i个数字和为y,那么如果前j数字的和小于等于y-x,那么i-j就是一种可能的情况,我们对于所有的i找出前面最大的j就可以了,因为数 ...
- Individual Contest #1 and Private Training #1
第一次的增补赛,也是第一场个人排位赛,讲道理打的和屎一样,手速题卡了好久还WA了好多发,难题又切不出来,这种情况是最尴尬的吧! Individual Contest #1: Ploblem D: 题意 ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [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 ...
- [poj1113][Wall] (水平序+graham算法 求凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...
- *HDU 1392 计算几何
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- redis的五种存储类型的具体用法
String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 $redis-> ...
- 学习C语言以及C语言基础调查
学习声乐的心得 你有什么技能比大多人(超过90%以上)更好? 就我个人而言,在所有的兴趣之中,做得比较好的应该属于声乐. 针对这个技能的获取你有什么成功的经验? 我对于声乐处始于兴趣,成功的经 ...
- ajax异步请求该嵌套还是并列?
因为要查询两个数据库表才能确定我所需要的数据范围,所以前台js得发两次ajax请求.问题就是,这两个请求是嵌套着写:{发,接{发,接}}:还是并列着写:{发,接},{发,接} 好? 答案:如果2次aj ...
- 下载win10
http://www.xitongtiandi.net/win10yuanban/2039.html#download
- mysql Error_code: 1593
mysql slave_IO_running no 查看data目录下的.err文件提示:mysql Error_code: 1593 解决方法: 1.检查 data目录下auto.cnf 文件中 ...
- mysql 压缩版安装
环境介绍:win2008_x64+mysql5.7.10 64位 1.将压缩包解压到d:\\mysql目录,并将mysql目录中的my-default.ini 重命名为my.ini 2.将my.in ...
- Eigen中的map
Map类用于通过C++中普通的连续指针或者数组 (raw C/C++ arrays)来构造Eigen里的Matrix类,这就好比Eigen里的Matrix类的数据和raw C++array 共享了一片 ...
- dubbo学习(zz)
dubbo学习 博客分类: 开源软件 Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站 ...
- Silverlight保存大数据到WCF出现Not Found问题
Silveright调用WCF保存大数据时,直接在客户端报“ 远程服务器返回了错误:Not Found ”问题,少量是没问题,如下图: 后面在这个文章中找到解决办法: http://www.cnblo ...
- Android Studio 老提示adb问题
Android Studio 老提示adb问题,restart后任然无解,最后发现某手机助手软件占用端口... 解决步骤: C:\Users\xxx>netstat -ano | findstr ...