尺取法——POJ3061
#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的更多相关文章
- 尺取法 poj3061 poj3320
尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...
- poj3061 Subsequence(尺取法)
https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- poj3061尺取法
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- 【尺取法】POJ3061 & POJ3320
POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...
- poj3061 Subsequence ,尺取法
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- POJ 3061 Subsequence 尺取法
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...
随机推荐
- CH5E09 能量相连【区间DP】
5E09 能量项链 0x5E「动态规划」练习 描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且, ...
- python基础之练习题(二)
九九乘法表 i = 0 #while 九九乘法表 j = 0 while i < 9: i += 1 while j<9: j += 1 sum = i + j total="% ...
- sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误。
sql server 驱动程序在 \Device\RaidPort0 上检测到控制器错误. 错误情况,如下图: 原因分析:硬盘故障 解决办法:进行迁移
- (2.11)Mysql之SQL基础——存储过程与变量
(2.11)Mysql之SQL基础——存储过程 关键字:mysql存储过程 查看存储过程: []SELECT * FROM information_schema.ROUTINES WHERE ROUT ...
- centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课
centos shell编程4[分发系统] 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要 ...
- java-mybaits-00102-mybatis框架原理
1.mybatis是什么? mybatis是一个持久层的框架,是apache下的顶级项目.是一个不完全的ORM框架. mybatis托管到goolecode下,再后来托管到github下(https: ...
- Callable 和Runnable
1:Callable ,方法调用会有返回值. private void callableTest throws ExecutionException, InterruptedException { E ...
- vue基础篇(一)
1.简介 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手 ...
- wordpress的安装及使用
1.如何查看别人的wordpress站点所有的模板 2.如何使用自定义的模板
- 手把手教你学node.js 之使用 eventproxy 控制并发
使用 eventproxy 控制并发 目标 建立一个 lesson4 项目,在其中编写代码. 代码的入口是 app.js,当调用 node app.js 时,它会输出 CNode(https://cn ...