码迷,mamicode.com
首页 > 其他好文 > 详细

Subsequence poj3061 (二分)

时间:2016-08-19 12:36:16      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

 

http://poj.org/problem?id=3061

 

题意:找出一个连续子序列比m大,求最短符合题意的连续子序列的长度为多少?

 

 

技术分享
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 110000;
typedef long long LL;
int a[maxn];
int n, m;

int Judge(int len)
{
    for(int i=1; i+len-1<=n; i++)
    {
        if(a[i+len-1]-a[i-1]>=m)
            return 1;
    }

    return 0;
}


int main()
{
    int T, num;

    scanf("%d", &T);

    while(T --)
    {
        scanf("%d %d", &n, &m);

        memset(a, 0, sizeof(a));

        for(int i=1; i<=n; i++)
        {
            scanf("%d", &num);
            a[i]=a[i-1]+num;
        }

       int l=1, r=n, mins=0;

       while(l<=r)
       {
           int mid=(l+r)/2;

           if(Judge(mid))
           {
               mins=mid;
               r=mid-1;
           }
           else
            l=mid+1;
       }

       printf("%d\n", mins);
    }
    return 0;
}
View Code

 

Subsequence poj3061 (二分)

标签:

原文地址:http://www.cnblogs.com/daydayupacm/p/5786847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!