#include <iostream>   //nlogn复杂度的写法
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int moder = 1e9 + ;
const int maxn = ; int a[maxn]; int main()
{
int t;
cin >> t;
while(t--)
{
int n,s;
scanf("%d%d",&n,&s);
for(int i=;i <= n;i++)
scanf("%d",&a[i]); for(int i=;i <= n;i++)
{
a[i] = a[i] + a[i-];
}
if(a[n] < s) printf("0\n");
else
{
int res = n;
for(int i=;a[n]-a[i] >= s;i++)
{
int t = lower_bound(a+,a+n+,s+a[i]) - a; // - a 非 - a - 1
res = min(res,t-i);
}
printf("%d\n",res);
}
}
return ;
}
/*2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5 */

O(n)复杂度的写法

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int moder = 1e9 + ;
const int maxn = ; int a[maxn]; int main()
{
int t;
cin >> t;
while(t--)
{
int n,s;
scanf("%d%d",&n,&s);
for(int i=;i <= n;i++)
scanf("%d",&a[i]);
int i=,j=;
int sum=;
int res = n+;
for(;;)
{
while(i <= n && sum < s)
{
sum += a[i];
i++;
}
if(sum < s) break;
res = min(res,i-j);
sum = sum - a[j];
j++;
}
if(res > n) res = ;
printf("%d\n",res);
}
return ;
}
/*2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5 */

——

尺取法——POJ3061的更多相关文章

  1. 尺取法 poj3061 poj3320

    尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...

  2. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  3. poj3061 poj3320 poj2566尺取法基础(一)

    poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...

  4. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

  5. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  6. poj3061尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  7. 【尺取法】POJ3061 & POJ3320

    POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...

  8. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  9. POJ 3061 Subsequence 尺取法

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

随机推荐

  1. 爬虫Scrapy框架

    安装scrapy 在安装过程中报错:解决方案 通过在https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted,然后下载: CP后是python 版本,32 ...

  2. centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,curl,ping ,telnet,traceroute ,dig ,nc,nmap,host,nethogs 第十六节课

    centos linux系统日常管理3  服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,cur ...

  3. SQL Server分区键列必须是主键一部分

    SQL Server分区键列必须是主键一部分. 必须把分区列包含在主键/唯一约束/唯一索引的键列中. USE tempdb GO -- 测试表 CREATE TABLE dbo.tb( id int, ...

  4. C#知识点备忘

    1.结构体不能用判断符号==判断是否为null,结构体是值类型,不论采用new与否,结构体中的值类型都已经赋了初值. 2.整数相除: a=; b=: c=a/b; 结果c= 如果想得到double型需 ...

  5. 有关ros::spin()和ros::spinonce()若干感受

    ros::spinonce()一般与loop_rate.sleep()同时出现,用来控制处理回调函数的频率,并且没有消息就收来时,就会程序堵塞,不会占用CPU资源. ros::spin(),用于回调函 ...

  6. 统计编程的框架与R语言统计分析基础——摘(2)统计分析之线性回归

    一.线性回归 1.简单线性回归 a. > x = women > x height weight 1 58 115 2 59 117 3 60 120 4 61 123 5 62 126 ...

  7. (14)如何使用Cocos2d-x 3.0制作基于tilemap的游戏:第二部分

    引言 程序截图: 这篇教程是<如何使用Cocos2d-x 3.0制作基于tilemap的游戏>的第二部分.在上一个教程中,我们创建了一个简单的基于tiled地图的游戏,里面有一个忍者在沙漠 ...

  8. 【知识总结】Java类初始化顺序说明

    微信公众号:努力编程的小猪如有问题或建议,请公众号留言 Java类初始化顺序说明 一个类中包含如下几类东西,他们前后是有顺序关系的 静态属性:static 开头定义的属性 静态方法块: static ...

  9. Core Java 5

    p273~p276: 1.获取异常的更多信息:e.getMessage(). 2.得到异常的实际类型:e.getClass().getName(). 3.当异常之间不存在子类关系,并且异常的处理机制( ...

  10. POI之Excel导出

    1,在maven的pom文件中添加依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId& ...