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. sql优化原则

    尽量使用性能高的比如left join等,尽量减少数据查询的column,尽量不要查询冗余数据,like的话“%%”是没有 办法使用索引的,但是“%”是可以使用索引的 having以前一直不知道到底真 ...

  2. 【Echarts每天一例】-1

    官方网址:http://echarts.baidu.com/doc/example/line1.html 使用百度echarts官方实例:http://ask.csdn.net/questions/1 ...

  3. C#第七天

    一 1.继承:我们可能会在一些类中,写一些重要的成员,将这些重复的成员单独的封装到一个类中,作为这些类的父类. Student    Teacher     Driver               ...

  4. 总结一下C++各个版本之间的功能扩充

    活到老,学到老.   C++ 98 我们学习和教材中常见的. C++ 03 主要是对98版本进行了bug修复. C++ 11 引入的新功能请参见:         http://www.cpluspl ...

  5. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  6. bug--Unable to add window –token is not valid; is your activity running?

    错误原因是Dialog在show的时候必须要有一个activity作为窗口载体,上面的日志的意思是承载Dialog的activity已经被销毁了,不存在了 解决方法: 1.粗暴一点直接try catc ...

  7. 《C++ Primer》之面向对象编程(二)

    构造函数和复制控制 每个派生类对象由派生类中定义的(非 static)成员加上一个或多个基类子对象构成,当我们构造.复制.赋值和撤销一个派生类对象时,也会构造.复制.赋值和撤销这些基类子对象. 构造函 ...

  8. WCF配置文件的问题(位置)

    引用过了远程的WCF服务,会自动生成配置文件,但是这个配置的位置,尽量放在applicationSettings的前面 刚才测试了,貌似放后面,会报错(执行的时候,这个问题,需要继续试验) (待验证) ...

  9. java fork-join框架应用和分析

    http://shmilyaw-hotmail-com.iteye.com/blog/1897636 java fork-join框架应用和分析 博客分类: concurrency multithre ...

  10. Mysql:输出到文件

    mysql>tee /home/a.txt mysql>show processlist; mysql>exit tee命令能重定向输出,同时屏幕会同步显示.