Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11224   Accepted: 4660

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

尺取法做法:一直向前增加num[r],直到不能增加,再判断r-l,然后再一直向前减去num[l],然后判断是否小于s。一直循环。70+ms

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int N=100010;
int list[N];
inline int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
int main (void)
{
int t,n,s,i,j,temp,sum,ans;
scanf("%d",&t);
while (t--)
{
memset(list,0,sizeof(list));
scanf("%d%d",&n,&s);
sum=0;
for (i=1; i<=n; i++)
{
scanf("%d",&list[i]);
}
int l,r,dx;
l=r=1;
temp=0;
dx=N;
while (1)
{
while (r<=n&&temp<s)//r向前递增
{
temp+=list[r++];
}
if(temp<s)
break;
dx=min(r-l,dx);
temp-=list[l++];//l向前递增
}
if(dx==N)//特判
puts("0");
else
printf("%d\n",dx);
}
return 0;
}

二分查找做法:建立另外一个数组sufix来储存前缀和,然后题目变成了保证sufix[r]-sufix[l-1]>=S这样的条件下求r-l的最小值,显然r>=l。将这个式子移项得到sufix[r]>=S+sufix[l]。

就是说当遍历l的时候要使r最小,然后就用自带的lowerbound函数来求。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int N=100010;
int list[N];
int sufix[N];
int main (void)
{
ios::sync_with_stdio(false);
int t,n,s,i,j,temp,sum,ans;
cin>>t;
while (t--)
{
cin>>n>>s;
for (i=1; i<=n; i++)
{
cin>>list[i];
sufix[i]=sufix[i-1]+list[i];
}
int dx=N;
if(sufix[n]<s)//特判
{
cout<<0<<endl;
continue;
}
for (i=0; sufix[i]+s<=sufix[n]; i++)//由于是公式内为l-1,因此i要从0开始,即
{
int t=lower_bound(sufix+i+1,sufix+n+1,s+sufix[i])-(sufix+i);
dx=min(t,dx);
}
cout<<dx<<endl;
}
return 0;
}

  

POJ——3061Subsequence(尺取法或二分查找)的更多相关文章

  1. POJ:3061-Subsequence(尺取法模板详解)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18795 Accepted: 8043 Descript ...

  2. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  3. POJ 1064 Cable master(二分查找+精度)(神坑题)

    POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...

  4. POJ 3320 尺取法(基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

  5. poj 2100(尺取法)

    Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 ...

  6. POJ 3320 (尺取法+Hash)

    题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...

  7. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  8. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  9. [ACM] poj 1064 Cable master (二分查找)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 Des ...

随机推荐

  1. SAP标准培训课程C4C10学习笔记(一)第一单元

    C4C10:SAP Hybris Cloud for Customer Administration 课程目录: 第一单元是C4C的简介. 作为SAP推出的一个SaaS(Software as a s ...

  2. [uva816]AbbottsRevenge Abbott的复仇(经典迷宫BFS)

    这题思路就普通的BFS加上一个维度朝向,主要是要注意输入,输出,以及细节的处理 #include<cstdio> #include<cstring> #include<q ...

  3. group - 用户组文件

    DESCRIPTION(描述) /etc/group 是一个ASCII码的文件,它定义了用户所属的组.文件中每行包括一条记录,其格式如下: group_name:passwd:GID:user_lis ...

  4. volatile引发的一系列血案

    最早读<深入理解java虚拟机>对于volatile部分就没有读明白,最近重新拿来研究并记录一些理解 理解volatile前需要把以下这些概念或内容理解: 1.JMM内存模型 2.并发编程 ...

  5. 《剑指offer》39题—数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  6. mysql grant 用户权限说明

    mysql grant 用户权限说明 Mysql 有多个个权限?经常记不住,今天总结一下,看后都能牢牢的记在心里啦!! 很明显总共28个权限:下面是具体的权限介绍:转载的,记录一下: 一.权限表 my ...

  7. cocos2d-x之CCCardinalSplineBy

    CCCardinalSplineBy概念 这个类是样条曲线动作,其创建函数CCCardinalSplineBy::create(float duration, cocos2d::CCPointArra ...

  8. 【转】MFC消息映射详解(整理转载)

    消息:主要指由用户操作而向应用程序发出的信息,也包括操作系统内部产生的消息.例如,单击鼠标左按钮,windows将产WM_LBUTTONDOWN消息,而释放鼠标左按钮将产生WM_LBUTTONUP消息 ...

  9. (转)可简化iOS 应用程序开发的6个Xcode小技巧

    Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...

  10. 蓝牙nrf52832的架构和开发(转载)

    相比TI的CC254X.DIALOG的DA1458X,nordic推出的nrf51822和nrf52832在架构和开发商都有自己独特的地方.这几颗产品都是蓝牙低功耗芯片.DA1458X使用OTP硬件架 ...