Subsequence (POJ - 3061)(尺取思想)
Problem
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
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5Sample Output
2
3
题解: 其实这题我感觉没有完全用到尺取的方法,可能思想有一点,就是从左端开始枚举,如果当前的和是小于m的,就让右端点右移,sum+=a[r],如果一但满足大于等于m,那么就计算一次ans,然后把左端点左移。重复上面直到遍历一遍就可以了。
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
int a[maxn];
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d %d", &n,&m);
for(int i = 0; i < n; i ++)
scanf("%d",&a[i]);
int l = 0,r = 0,sum = 0,ans = n + 1;
while(1)
{
while(r<n&&sum<m)
{
sum=sum+a[r];
r ++;
}
if(sum<m)
{
break;
}
ans = min(r-l,ans);
sum=sum-a[l];
l++;
}
if(ans == n + 1)
{
printf("0\n");
}
else
{
printf("%d\n",ans);
}
}
return 0;
}
Subsequence (POJ - 3061)(尺取思想)的更多相关文章
- Subsequence poj 3061 二分(nlog n)或尺取法(n)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9236 Accepted: 3701 Descr ...
- B - Bound Found POJ - 2566(尺取 + 对区间和的绝对值
B - Bound Found POJ - 2566 Signals of most probably extra-terrestrial origin have been received and ...
- A - Jessica's Reading Problem POJ - 3320 尺取
A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...
- POJ-3061 Subsequence 二分或尺取
题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...
- poi 3061 尺取例题1
题目传送门/res tp poj 白书题 尺取法例题 #include<iostream> #include<algorithm> using namespace std; c ...
- Subsequence POJ - 3061
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22040 Accepted: 9404 Desc ...
- Greedy:Subsequence(POJ 3061)
和最短序列 题目大意:找出一个序列中比至少和S相等的最短子序列(连续的) 本来这道题可以二分法来做复杂度O(NlogN),也可以用一个类似于游标卡尺的方法O(N)来做 先来讲游标卡尺法: 因为子序 ...
- POJ 3061 Subsequence 尺取
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14698 Accepted: 6205 Desc ...
- POJ 3061 Subsequence ( 二分 || 尺取法 )
题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...
随机推荐
- Nginx 路由重写
很多时候我们的真实路由是隐藏的,都经过重写后展现到前台,下面简单写两个我经常用到的几个: 一般在配置*.host(在http里面引入的server配置)的时候会用到每个不同网址的路由重写,每一个rew ...
- C# EntityCollection 和 List 互转
private EntityCollection<T> ToEntityCollection<T>(this List<T> list) where T : cla ...
- 怎样绑定this
有三种方法: 1. Function.prototype.call(); 2. Function.prototype.apply(); 3. Function.prototype.bind(); ...
- Nopcommerce4.2解析——安装
Nopcommerce是一个DotNet领域异常凶残的一个开源电商系统,最先版本4.2,下面我们会逐步分析他的各个模块,为我们的二次开发做准备,应该会写一个系列. 首次运行nop页面会自动跳转到安装页 ...
- css:display:grid布局
简介 CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,主要目的是改变我们基于网格设计的用户接口方式.如我们所知,CSS 总是用于网页的样式设置,但它并没有起到 ...
- java轻松玩转httpget和httppost
废话不多少说,直接上代码 //get请求 public static void HttpClientGet(String url) throws Exception { // 获取http客户端 Cl ...
- 1.volatile关键字 内存可见性
Java JUC 简介 在 Java 5.0 提供了 java.util.concurrent (简称JUC )包,在此包中增加了在并发编程中很常用的实用工具类,用于定义类似于线程的自定义子系统,包括 ...
- 案例:selenium实现登录百度(如有验证码,需要手动输入)
func.py https://www.cnblogs.com/andy9468/p/10899508.html baidu_login.py中(如有验证码,需要手动输入) # 导入webdriver ...
- 取出List<Map<String,Object>>里面Map的key:value值
1.取出Map其中一个属性的值 Map map = new HashMap(); map.put("key1", "value1"); map.put(&quo ...
- 查看mysql的bin-log日志
1.查看有哪些binlog show binary logs; show master logs; 2.如何查看log_bin中的内容 show binlog events; 查看第一个binlog的 ...