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

Sample Output

题目链接:http://poj.org/problem?id=3061

*******************************************

题意:T组实例,每组实例给出n和s,接着n个数。求连续子序列和大于等于s的最短子序列长度。

分析:有点点模拟的意思,形象点说,就像你堆积木,超过一定值S后我们就把最下面的几块积木去掉。具体方法是从前面开始不断累加,当和值超过S时减少前面的数值,然后记录下刚好>=S的ans值,如此重复直到序列尾,输出最小的ans。

AC代码1:

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 1200000
#define INF 0x3f3f3f3f int dp[N];
int a[N]; int main()
{
int n,m,T,i,j; scanf("%d", &T); while(T--)
{
scanf("%d %d", &n, &m);
memset(dp,,sizeof(dp)); for(i=;i<=n;i++)
{
scanf("%d", &a[i]);
dp[i]=dp[i-]+a[i];
} int minn=INF,i=;;
for(j=;j<=n;j++)
{
if(dp[j]-dp[i-]<m)
continue ;
while(dp[j]-dp[i]>=m)
i++;
minn=min(minn,j-i+);
} if(minn==INF)
printf("0\n");
else
printf("%d\n", minn);
}
return ;
}

AC代码2:(运用二分函数)

这里用到二分函数是找到刚好>=s的那个ans

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 1200000
#define INF 0x3f3f3f3f int dp[N];
int a[N]; int main()
{
int n,m,T,i; scanf("%d", &T); while(T--)
{
scanf("%d %d", &n, &m);
memset(dp,,sizeof(dp)); for(i=;i<=n;i++)
{
scanf("%d", &a[i]);
dp[i]=dp[i-]+a[i];
} int minn=INF;
for(i=;i<=n;i++)
if(dp[n]-dp[i-]>=m)
{
int ans=lower_bound(dp,dp+n,dp[i-]+m)-dp;///二分函数
minn=min(ans-i+,minn);
} if(minn==INF)
printf("0\n");
else
printf("%d\n", minn);
}
return ;
}

POJ - 3061 Subsequence(连续子序列和>=s的最短子序列长度)的更多相关文章

  1. poj 3061 Subsequence 二分 前缀和 双指针

    地址 http://poj.org/problem?id=3061 解法1 使用双指针 由于序列是连续正数 使用l r 表示选择的子序列的起始 每当和小于要求的时候 我们向右侧扩展 增大序列和 每当和 ...

  2. poj 3061 Subsequence

    题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 &l ...

  3. POJ 3061 Subsequence 二分或者尺取法

    http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...

  4. POJ 3061 Subsequence(尺取法)

    题目链接: 传送门 Subsequence Time Limit: 1000MS     Memory Limit: 65536K 题目描述 给定长度为n的数列整数以及整数S.求出总和不小于S的连续子 ...

  5. Poj 3061 Subsequence(二分+前缀和)

    Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...

  6. [ACM] POJ 3061 Subsequence (仿真足)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8403   Accepted: 3264 Descr ...

  7. POJ 3061 Subsequence(尺取法)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18145   Accepted: 7751 Desc ...

  8. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

  9. POJ 3061 Subsequence 尺取法

    转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...

随机推荐

  1. tableviewcell 中使用autolayout自适应高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { [ce ...

  2. logrotate日志轮转配置文档

    转自:http://blog.163.com/bull_linux/blog/static/2138811422013101334544349/ 使用:    logrotate CONF_FILE+ ...

  3. runtime官方文档

    OC是一种面向对象的动态语言,作为初学者可能大多数人对面向对象这个概念理解的比较深,而对OC是动态语言这一特性了解的比较少.那么什么是动态语言?动态语言就是在运行时来执行静态语言的编译链接的工作.这就 ...

  4. 一把刀系统维护工具箱 v1.6 绿色版

    软件名称: 一把刀系统维护工具箱 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win8 / Win7 / Vista / WinXP 软件大小: 13.7MB 图片预览: 软件简介: 一把 ...

  5. MVC view视图获取Html.RenderAction方式带来的参数

    通过Html.RenderAction这种方式传递的参数,在view视图中获取要使用viewContext上下文来获取:Html.ViewContext.RouteData.Values[" ...

  6. php练习1

    <?php$conn=mysqli_init();if(!conn){ echo "mysqli_init error"; exit(0);}$ret=mysqli_real ...

  7. Thrift源码解析--TBinaryProtocol

    本文为原创,未经许可禁止转载. 关于Tprotocol层都是一些通信协议,个人感觉内容较大,很难分类描述清楚.故打算以TBinaryProtocol为例,分析客户端发请求以及接收服务端返回数据的整个过 ...

  8. iosNSMutableAttributedString 简单操作

    // 打印系统中所有字体的类型名字    NSArray *familyNames = [UIFont familyNames];    for(NSString *familyName in fam ...

  9. 一个基于DpperHelper的t4模板

    自定义模板,空的类(目的是和t4生成的模板分开,以免被覆盖) ExtensionDAL <#@ template debug="false" hostspecific=&qu ...

  10. python 文件夹操作

    常用的一些 import os for d, cd, fl in os.wald('.'): for f in fl: sufix = os.path.splitext(f)[1][1:] if su ...