zoukankan      html  css  js  c++  java
  • Poj 3061 Subsequence(二分+前缀和)

    Subsequence
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 12333 Accepted: 5178
    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
    Southeastern Europe 2006

    /*
    题目大意:
    要求n个数总和不小于s的连续子序列的长度的最小值.
    二分+前缀和查询. 
    */
    #include<iostream>
    #include<cstdio>
    #define MAXN 100001
    using namespace std;
    int n,m,s[MAXN],sum[MAXN];
    bool check(int x)
    {
        for(int i=1;i<=n-x+1;i++){
            if(sum[x+i-1]-sum[i-1]>=m) return true;
        }
        return false;
    }
    int erfen(int l,int r)
    {
        int mid,ans=0;
        while(l<=r){
            int mid=(l+r)>>1;
            if(check(mid)) ans=mid,r=mid-1;
            else l=mid+1;
        }
        return ans;
    }
    int main()
    {
        int t;scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++) scanf("%d",&s[i]),sum[i]=s[i]+sum[i-1];
            printf("%d
    ",erfen(0,n+1));
        }
        return 0;
    }
  • 相关阅读:
    bzoj 1031: [JSOI2007]字符加密Cipher
    python re模块实现计算器
    python sys模块和序列化模块
    python time时间模块
    python 发红包的小程序
    python ranndom模块及生成验证码
    python os模块练习题
    python os模块常用命令
    快速排序,归并排序
    选择排序和插入排序
  • 原文地址:https://www.cnblogs.com/nancheng58/p/6070780.html
Copyright © 2011-2022 走看看