Minimal Subarray Length
来源:互联网 发布:caxa数控车床编程视频 编辑:程序博客网 时间:2024/06/04 18:11
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
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<cstdio>#include<cstring>#include<algorithm>using namespace std;int num[500005];int main(){ int T; scanf("%d",&T); while(T--) { int n,x; scanf("%d%d",&n,&x); for(int i=0;i<n;++i) { scanf("%d",&num[i]); } if(x<=0) { int c=0; for(int i=0;i<n;++i) { if(num[i]>=x){c=1;break;} } if(c)printf("1\n"); else printf("-1\n"); continue; } int ans=1000000000,start=0; long long sum=0; for(int i=0;i<n;++i) { sum=sum+num[i]; if(sum<=0) { sum=0; start=i+1; } if(sum>=x) { int tmp=0; for(int j=i;j>=0;--j) { if(tmp+num[j]>=x) { start=j; break; } tmp+=num[j]; } ans=min(ans,i-start+1); } } if(ans>500001)printf("-1\n"); else printf("%d\n",ans); } return 0;}
0 0
- Minimal Subarray Length
- LA 6609 - Minimal Subarray Length
- 【单调栈思路】Minimal Subarray Length UVALive
- UVALive 6609(Minimal Subarray Length)维护递增序列|RMQ
- UVALive 6609 Minimal Subarray Length (查找+构建排序数组)
- UVALive 6609 Minimal Subarray Length rmq+二分or单调队列
- UVALive 6609 Minimal Subarray Length(最大子段)
- uvalive 6609 - Minimal Subarray Length(离散化+树状数组)
- HOJ 13006 Minimal Subarray Length (单调队列或RMQ加二分)
- UVALive_6609_Minimal Subarray Length(RMQ+二分)
- Maximum Length of Repeated Subarray
- Maximum Length of Repeated Subarray
- Maximum Length of Repeated Subarray
- leetcode 718( Maximum Length of Repeated Subarray)
- Leetcode 718. Maximum Length of Repeated Subarray
- 718. Maximum Length of Repeated Subarray
- 718. Maximum Length of Repeated Subarray
- LeetCode718. Maximum Length of Repeated Subarray(Medium)
- python 如何将字典dict、列表list中的中文正常显示
- 做游戏,学编程(C语言) 7 学习EasyX图形交互功能----flappy bird源代码
- hdu3473 中位数性质? (划分树)
- 2017年4月自考体会
- linux下用rpm 安装jdk
- Minimal Subarray Length
- 解决Tomcat: Can't load IA 32-bit .dll on a AMD 64-bit platform问题
- Hbase集群部署计划---安装系列四
- 803D
- 多线程同步问题-练习题2-笔记整理
- 修改文件的权限
- 爬虫入门(一)
- i++底层问题
- linux mysql导入数据库