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

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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int a[MS];
int sum[MS];
int N,S; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&S);
sum[]=;
a[]=;
for(int i=;i<=N;i++)
{
scanf("%d",&a[i]);
sum[i]=a[i]+sum[i-];
}
if(sum[N]<S)
{
printf("0\n");
continue;
}
int ans=N;
for(int i=;sum[i-]+S<=sum[N];i++)
{
int index=lower_bound(sum+i,sum+N,sum[i-]+S)-sum;
ans=min(ans,index-i+);
}
printf("%d\n",ans);
}
return ;
}

尺取大法好啊

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int a[MS];
int N,S; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&S);
for(int i=;i<N;i++)
scanf("%d",&a[i]);
int sum=;
int s=,t=;
int ans=N+;
while()
{
while(t<N&&sum<S)
sum+=a[t++];
if(sum<S)
break;
ans=min(ans,t-s);
sum-=a[s++];
}
if(ans>N)
printf("0\n");
else
printf("%d\n",ans);
}
return ;
}

Subsequence poj 3061 二分(nlog n)或尺取法(n)的更多相关文章

  1. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

  2. POJ 3061 Subsequence【二分答案】||【尺取法】

    <题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...

  3. poj 3061(二分 or 尺取法)

    传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...

  4. Subsequence POJ - 3061

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22040   Accepted: 9404 Desc ...

  5. poj 2566Bound Found(前缀和,尺取法)

    http://poj.org/problem?id=2566: Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissi ...

  6. POJ 3320 Jessica's Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  7. poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用

    jessica's Reading PJroblem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9134   Accep ...

  8. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...

  9. Greedy:Subsequence(POJ 3061)

      和最短序列 题目大意:找出一个序列中比至少和S相等的最短子序列(连续的) 本来这道题可以二分法来做复杂度O(NlogN),也可以用一个类似于游标卡尺的方法O(N)来做 先来讲游标卡尺法: 因为子序 ...

随机推荐

  1. 【Maven】Maven下载源码和Javadoc的方法

    1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...

  2. 使用sem_post信号量进行线程同步

    写了一小段程序,测试一下线程同步的问题,如下: #include <stdio.h> #include <string.h> #include <semaphore.h& ...

  3. Scene View Navigation

    [Scene View Navigation] Hold the right mouse button to enter Flythrough mode. This turns your mouse ...

  4. Labview中定时函数之间的区别

    第一个定时函数(时间延迟):在VI中插入时间延迟,指定在运行调用VI之前延时的秒数.默认值为1.000. 第一个定时函数(等待(ms)):等待指定长度的毫秒数,并返回毫秒计时器的值.该函数进行异步系统 ...

  5. JavaIO(06)文件复制

    文件复制一般是采用两种方式进行操作: 1:将源文件中的内容全部读取到内存中,并一次性的写入到目标文件中:(不常用这种方式) 2:不将源文件中的内容全部读取进来,而是采用边读边写的方式:   实例01: ...

  6. FZU2143Board Game(最小费用流)

    题目大意是说有一个B矩阵,现在A是一个空矩阵(每个元素都为0),每次操作可以将A矩阵相邻的两个元素同时+1,但是有个要求就是A矩阵的每个元素都不可以超过K,求 这个的最小值 解题思路是这样的,新建起点 ...

  7. [Oracle]配置path使oracle备份/导入数据命令exp/imp起作用

    将E:\Oracle11g\app\Administrator\product\11.2.0\dbhome_1\bin;路径添加如path

  8. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息]

    三.使用Azure AD管理用户信息 在上一章我们采用OpenID的方案和Azure AD交互进行身份验证,本章节我们继续了解如何在Azure AD中创建用户,列出用户信息,修改用户信息和删除用户信息 ...

  9. [ASP.NET]更简单的方法:FormsAuthentication登录ReturnUrl使用绝对路径

    转自:http://www.cnblogs.com/dudu/p/formsauthentication-returnurl-absoluteuri.html [ASP.NET]更简单的方法:Form ...

  10. JavaScript 各种遍历方式详解,有你不知道的黑科技

    http://segmentfault.com/a/1190000003968126 为了方便例子讲解,现有数组和json对象如下 var demoArr = ['Javascript', 'Gulp ...