[POJ3061]Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15908   Accepted: 6727

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

Source

 
题目大意:问最小区间长度K使得序列中有长度为K的连续子序列相加大于等于S,不够输出0
试题分析:二分序列长度,滑动窗口
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
#define LL long long
const int MAXN=1000001;
const int INF=999999;
const int mod=999993;
int N,S;
int a[1000001];
int tmp; bool check(int k){
long long sum=0;
for(int i=1;i<=k;i++){
sum+=a[i];
}
if(sum>=S) return true;
for(int i=2;i+k-1<=N;i++){
sum-=a[i-1];sum+=a[i+k-1];
if(sum>=S) return true;
}
return false;
} int main(){
int T=read();
while(T--){
N=read(),S=read();long long k=0;
for(int i=1;i<=N;i++) a[i]=read(),k+=a[i];
if(k<S) {printf("0\n");continue;}
int l=1,r=N;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) r=mid-1;
else l=mid+1;
}
printf("%d\n",l);
}
}

【二分】Subsequence的更多相关文章

  1. Subsequence poj 3061 二分(nlog n)或尺取法(n)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9236   Accepted: 3701 Descr ...

  2. Poj 3061 Subsequence(二分+前缀和)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...

  3. 【二分答案nlogn/标解O(n)】【UVA1121】Subsequence

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  4. Subsequence(暴力+二分)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10875   Accepted: 4493 Desc ...

  5. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  6. CSU1553 Good subsequence —— 二分 + RMQ/线段树

    题目链接: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 Description Give you a sequence of n n ...

  7. 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  8. Intel Code Challenge Final Round D. Dense Subsequence 二分思想

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. CSU 1553 Good subsequence(RMQ问题 + 二分)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 Description Give you a sequence of n nu ...

随机推荐

  1. centos6.5下安装svn并且实现多项目管理配置方案

    #安装SVN服务器 yum install subversion #在home下创建svn根目录 mkdir /home/svn #在 /home/svn下创建pro1 , pro2, pro3 三个 ...

  2. Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总

    In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try ...

  3. python基础===中文手册,可查询各个模块

    http://python.usyiyi.cn/translate/python_352/index.html

  4. MariaDB 层常用业务

    前言  -  简单准备一下前戏 前面写过几篇mariadb 数据的随笔, 多数偏C/C++层面. 这次分享一下平时开发中, 处理的一些数据层面的业务. 对于MariaDB, 不做过多介绍. 如果你有U ...

  5. 微信小程序验证码获取倒计时

    wxml <button disabled='{{disabled}}' bindtap="goGetCode">{{code}}</button> js ...

  6. [总结]可重用cell的定义方式

    1.简介 为了提高tableview中cell的加载速度通常可以使用cell重用的方式来实现,即我们向上拖动cell的时候,上部份消失的cell可以重复的被下部分出现的cell重用. 2.说明 一般c ...

  7. Spring Boot with Docker

    翻译自:https://spring.io/guides/gs/spring-boot-docker/ Spring Boot with Docker 这篇教程带你一步步构建一个Docker镜像用来运 ...

  8. Python VUE 基础知识

    一 什么是VUE 它是一个构建用户界面的JavaScript框架,自动生成(js,css,HTML文件) 二 如何使用VUE 1.  应用vues.js <script src="vu ...

  9. redis之(七)redis的集合类型的命令

    [一]增加/删除元素 --->命令:SADD key member [member...] --->向集合键中添加一个,或多个元素.如果键不存在,则创建.如果元素存在,则忽略不执行.返回值 ...

  10. BZOJ 1901: Zju2112 Dynamic Rankings 区间k大 带修改 在线 线段树套平衡树

    之前写线段树套splay数组版..写了6.2k..然后弃疗了.现在发现还是很水的..嘎嘎.. zju过不了,超时. upd:才发现zju是多组数据..TLE一版才发现.然后改了,MLE...手写内存池 ...