poj 3061

尺取法板子

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=100000+10;
int a[maxn];
int t,n,S;
int chiqu()
{   int t=0,s=0,sum=0;
  int ans=n+1;
    while(1)
    {
        while(t<n&&sum<S)
        {
            sum+=a[t++];
        }
        if(sum<S)  break;
        ans=min(ans,t-s);
        sum-=a[s++];
    }
    if(ans>n) ans=0;
    return ans;
}
int main()
{
   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&S);
       for(int i=0;i<n;i++)
           scanf("%d",&a[i]);
         printf("%d\n",chiqu());
   }
    return 0;
}
时间: 2024-11-03 21:36:05

poj 3061的相关文章

POJ 3061 (二分+前缀和)

题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点慢. 二分枚举序列长度,如果可行,向左找小的,否则向右找大的. 前缀和预处理之后,可以O(1)内求和. #include "cstdio" #include "cstring" int sum[100005],n,s,a,T; bool check(int x) { i

POJ 3061 Subsequence(Two Pointers)

[题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子区间,更新答案即可. [代码] #include <cstdio> int T,a[100005]; int main(){ scanf("%d",&T); while(T--){ int n,S,s,h,t,ans; scanf("%d%d",&a

POJ 3061 Subsequence(尺取法)

题目链接:http://poj.org/problem?id=3061 题意:给定长度为n的数列整数,以及整数S,求出总和不少于S的连续子序列的长度的最小值.如果解不存在,则输出0. 尺取法:通常是指对数组保存一对下标(起点,终点),然后根据实际情况交替推进两个端点直到解决问题的方法,这个操作很像尺蠼虫故得名. 思路:所以可以先初始化起点s,终点g,再一步一步推进,直到sum>S,然后记录此时的序列长度,再推进s,sum-=num[s],再记录长度,直到sum<S,再推进g,这样的方法,s和g

POJ - 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 o

从具体题目来谈尺取法 POJ 3061 Subsequence

题目链接:http://poj.org/problem?id=3061 先说说最朴素的算法,那就是for嵌套了,复杂度是O(n^3)太慢,想都不用想一定会超时.接下来有的人可能会想到二分搜索的思想,将时间复杂度优化成O(n*logn),我试了一下,可以AC. 但是这都不是今天要说的重点,今天要说的是一个ACM比赛中常用的技巧方法——尺取法.这是一种可以直接将时间复杂度优化到O(n)的算法. 我们先来介绍一下尺取法.尺取法,顾名思义,像尺子一样,一块一块的截取.是不是解释的有点让人纳闷-..没关系

POJ 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 o

POJ 3061 Subsequence(尺取法)

传送门 Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11284 Accepted: 4694 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) ar

POJ 3061 Subsequence 尺取

Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14698   Accepted: 6205 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) ar

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

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