poj 3061 Subsequence
题目连接
http://poj.org/problem?id=3061
Subsequence
Description
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.
Input
The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.
Output
For each the case the program has to print the result on separate line of the output file.if no answer, print 0.
Sample Input
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
Sample Output
2
3
二分。。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using std::min;
using std::lower_bound;
const int Max_N = ;
int arr[Max_N], sum[Max_N];
void solve(int n, int s) {
int res = n;
if (s > sum[n] || s < sum[]) { puts(""); return; }
for (int i = ; sum[i] + s <= sum[n]; i++) {
int t = lower_bound(sum + i, sum + n, sum[i] + s) - sum;
res = min(res, t - i);
}
printf("%d\n", res);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n, s;
while (~scanf("%d", &t)) {
while (t--) {
scanf("%d %d", &n, &s);
for (int i = ; i < n; i++) {
scanf("%d", &arr[i]);
sum[i + ] = sum[i] + arr[i];
}
solve(n, s);
}
}
return ;
}
poj 3061 Subsequence的更多相关文章
- POJ - 3061 Subsequence(连续子序列和>=s的最短子序列长度)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence(Two Pointers)
[题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子 ...
- POJ 3061 Subsequence 二分或者尺取法
http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...
- poj 3061 Subsequence 二分 前缀和 双指针
地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和 ...
- POJ 3061 Subsequence(尺取法)
题目链接: 传送门 Subsequence Time Limit: 1000MS Memory Limit: 65536K 题目描述 给定长度为n的数列整数以及整数S.求出总和不小于S的连续子 ...
- Poj 3061 Subsequence(二分+前缀和)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...
- [ACM] POJ 3061 Subsequence (仿真足)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8403 Accepted: 3264 Descr ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9050 Accepted: 3604 Descr ...
随机推荐
- python基础set
1.set set是一个无序的不重复的集合 li=[11,22,33,11] s=set(li) print(s) {11,22,33} set提供的方法 1.add(self, *args, ** ...
- 【PL/SQL练习】基本的PL/SQL语句
1.无变量匿名快 begin dbms_output.put_line('Hello World'); end; 2.有变量的匿名块,定义变量: declare v_ename ); v_sal ,) ...
- C# 调用控制台程序,并获取输出写入文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用 ...
- ittype导师课程 - SAP顾问课程,云课堂,个性辅导
ittype致力于成为最专业的<企业管理研究+管理信息化需求研究+解决方案+项目+人才>全生态链服务与功能平台,平台网址:www.ittype.com. 平台由导师+学员模式,导师教导学员 ...
- 使用TypeScript如何提升JavaScript编程效果?
TypeScript是个什么鬼?和JavaScript有什么关系? TypeScript是由微软开发的一种可快速入门的开源的编程语言,是JavaScript的一个超集,且向这个语言添加了可选的静态类型 ...
- PeopleCode 处理压缩文件
PeopleSoft中对文件附件的处理都是单个文件处理的,虽然在8.52版本新增了MAddAttachment(URLDestination, DirAndFilePrefix, Prompts, ...
- DataGridView控件中添加ComboBox下拉列表框的实现
//ComboBox控件拖放到DataGridView控件的某个位置 //添加年龄下拉框 private void BindAge() { //我这里添加的是静态数据,一般都是从数据库读出来的,这里就 ...
- js 实现栈
function Stack() { this.dataStore = []; this.top = 0; this.push=push; this.pop=pop; this.peek=peek; ...
- 史上最全Vim快捷键键位图(入门到进阶)
经典版 下面这个键位图应该是大家最常看见的经典版了. 对应的简体中文版 其实经典版是一系列的入门教程键位图的组合结果,下面是不同编辑模式下的键位图. 入门版 基本操作的入门版. 进阶版 增强版 下图是 ...