UVA 12697 Minimal Subarray Length
Minimal Subarray Length
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的更多相关文章
- E - Minimal Subarray Length(连续区间和)
题目链接 题意:给出n个数,求加和大于x的最短区间的区间长度. 如果前i个数字和为y,那么如果前j数字的和小于等于y-x,那么i-j就是一种可能的情况,我们对于所有的i找出前面最大的j就可以了,因为数 ...
- 6609 - Minimal Subarray Length
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 6609 Minimal Subarray Length (查找+构建排序数组)
描述:给定n个整数元素,求出长度最小的一段连续元素,使得这段元素的和sum >= X. 对整个数组先求出sum[i],表示前i个元素的和,然后依次求出以a[i]为起点的,总和>= X的最小 ...
- UVALive 6609 Minimal Subarray Length(RMQ-ST+二分)
题意:给定长度为N的数组,求一段连续的元素之和大于等于K,并且让这段元素的长度最小,输出最小长度即可,若不存在这样的元素集合,则输出-1 题目链接:UVAlive 6609 做法:做一个前缀和pref ...
- uva.10020 Minimal coverage(贪心)
10020 Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose t ...
- 【区间覆盖问题】uva 10020 - Minimal coverage
可以说是区间覆盖问题的例题... Note: 区间包含+排序扫描: 要求覆盖区间[s, t]; 1.把各区间按照Left从小到大排序,如果区间1的起点大于s,则无解(因为其他区间的左起点更大):否则选 ...
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the min ...
- uva 10020 Minimal coverage
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
随机推荐
- bzoj 1833: [ZJOI2010]count 数字计数【数位dp】
非典型数位dp 先预处理出f[i][j][k]表示从后往前第i位为j时k的个数,然后把答案转换为ans(r)-ans(l-1),用预处理出的f数组dp出f即可(可能也不是dp吧--) #include ...
- bzoj 1774: [Usaco2009 Dec]Toll 过路费【排序+Floyd】
非常迷的一道题啊 我觉得挺对的版本只得了30 总之就是Floyd·改,开两个数组,一个是d[i][j]就是普通的只有边权的最短路,a[i][j]是题目要求的那种 具体改的地方是把枚举中转点的地方把中转 ...
- php编译安装参数详解
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/loc ...
- Sql 存储过程动态添加where条件
)= '2,3' )= '' ) if(@bussHallId is not null) set @strWhere = @strWhere + ' and bh.ID in ('+@bussHall ...
- Python,报错NameError: name 'math' is not defined
1 #-*- coding : utf-8 -*- 2 import math 3 4 def move(x, y, step, angle=0): 5 nx = x + step * math.co ...
- python模块中的__all__属性
转自:http://blog.csdn.net/sxingming/article/details/52903377 python模块中的__all__属性,可用于模块导入时限制,如:from mod ...
- Modbus测试工具ModbusPoll与Modbus Slave使用方法
感谢https://blog.csdn.net/byxdaz/article/details/77979114原创,由于CSDN经常调整,故再编辑收藏,并修改了部分BUG. 一.介绍 Modbus P ...
- R语言学习 - 线图一步法
首先把测试数据存储到文件中方便调用.数据矩阵存储在line_data.xls和line_data_melt.xls文件中 (直接拷贝到文件中也可以,这里这么操作只是为了随文章提供个测试文件,方便使用. ...
- autocad 注册表
序号 版本号1 版本号2 描述 1 R15.0 1:804 2002中文版 2 R15.0 1:409 2002英文版 3 R16.0 201:804 2004中文版 4 R16.0 201:409 ...
- 日常开发需要掌握的Git命令
本人待的两家公司,一直都是用的SVN,Git我只是自己私下学习和开发小项目的时候用过,工作一直没有使用过,但还是要学的... Git是最好的分布式版本控制系统 工作流程 SVN和Git的区别 SVN是 ...