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. OR查询

    OR查询包含:$or和$in $or可以在多个键中查询任意给定的值:$in可以指定不同类型的条件和值. 查询MasertID小于3或者MasterSort等于3的文档: db.SysCore.find ...

  2. velocity 高亮显示

    velocity模板在eclipse中高亮显示的链接 http://download.eclipse.org/eclipse/updates/4.4http://veloeclipse.googlec ...

  3. windows cmd启动heritrix

    首先下载heritrix-1.14.4.zip文件,可以在http://sourceforge.net/projects/archive-crawler/files/heritrix3/下载.下载后解 ...

  4. 背包问题matlab程序

    clearclca=0.95k=[5;10;13;4;3;11;13;10;8;16;7;4];k=-k;d=[2;5;18;3;2;5;10;4;11;7;14;6];restriction=46; ...

  5. CCNET+MSBuild+SVN实时构建的优化总结

    本文不是介绍如何使用CCNET+MSBuild+SVN构建自动编译系统,相关的内容可以从很多地方获取,可以再园子里搜一下. 随着我们的SVN库日益壮大,容量达到10G,几十G 甚至更大时,我们发现自动 ...

  6. OC-之NSDate、万年历

    万年历计算星期-基姆拉尔森公式 #include /* * 基姆拉尔森计算公式* W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7*/int week(int ...

  7. 第二十节,基本数据类型,集合set、综合应用新数据更新老数据

    基本数据类型,集合set.综合应用新数据更新老数据 创建两个字典新数据,更新原始数据,a为原始数据,b为新数据 1,分别获取到a字典和b字典的key(键),将两个字典的键分别转换成两个集合 2,找出a ...

  8. hibernate对象的状态以及生命周期

    瞬时状态:session中没有,数据库中没有 持久状态:session中有,数据库中有 游离状态:session中没有,数据库中有 get和load都是用来提取数据的 get和load的区别: get ...

  9. Linux基础命令---压缩与打包

    GZIP: 普通文件打包成gzip文件:gzip filename(问题:如何测试一个文件是否是gzip文件?) gzip文件解压成普通文件:gzip -d filename(副作用:原始gz文件会被 ...

  10. 《Head.First设计模式》的学习笔记(9)--外观模式

    意图:为子系统中的一组接口提供一个一致的界面,Facade 模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 结构: 例子: 假设你有一套杀手级的家庭影院系统,内含DVD播放器.投影仪.自 ...