[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. wait与waitpid

    1. 函数原型: #include <sys/wait.h> pid_t wait(int *statloc); pid_t waitpid(pid_t pid, int *statloc ...

  2. .gitignore 文件添加或更新后规则无效的解决方案

    项目已经提交之后,突然想忽略某个文件或目录 A,于是在 .gitignore 里添加了忽略规则.但是提交(commit)之后,发现一旦修改了 A,git 同样会检测到 A 的变化(changes) , ...

  3. hadoop InputFormat 类别

    FileInputFormat是所有使用文件作为数据源的InputFormat的积累.它提供两个功能:一个是定义哪些文件包含在一个作业的输入中:一个为输入文件生成分片的实现.自动将作业分块 作业分块大 ...

  4. 一个朋友 js图表开发问题 用 c和 js 解决

    引言            不求知道一切, 只求发现一件 -- 乔治·西蒙·欧姆 附注:那些存在于梦幻中的事迹,那些儿时梦中的人物,每每看起,都觉得 .哎 .... 岁月 ... 一直在努力 ...  ...

  5. Winform利用委托进行窗体间的传值

    在form1.cs中 1.委托的定义 //定义一个委托 public delegate void AddUsrEventHandler(object sender, AddUsrEventHandle ...

  6. C#多线程编程之:集合类中Synchronized方法与SyncRoot属性原理分析

    我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot属性,他们之间有联系吗?我怎么才能用好他们呢? 以Hashtab ...

  7. Science14年的聚类论文——Clustering by fast search and find of density peaks

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 这是一个比较新的聚类方法(文章中没看见作者对其取名,在这里我姑且称该方法为local density clu ...

  8. IA32系统级架构总览(二)

    系统级架构由寄存器.数据结构.指令组成,这些设计对基本的系统级别的操作提供了支持,比如:内存管理.终端与异常处理.任务管理.多进程控制等. 我们先来看一看寄存器与数据结构的总汇图:现在你可能看不懂,不 ...

  9. Python函数式编程——map()、reduce()

    文章来源:http://www.pythoner.com/46.html 提起map和reduce想必大家并不陌生,Google公司2003年提出了一个名为MapReduce的编程模型[1],用于处理 ...

  10. windows系统中,创建临时环境变量

    以servlet-api.jar 包为例 set classpath=%classpath%;C:\apache-tomcat-6.0.37\lib\servlet-api.jar