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) 分析 : 有关子序列和的问题,都可以考虑采用 ...
随机推荐
- c#获取本月有哪些周六、周日
最近项目中有用到本月所有的周六,周日,特此分享一下! 算法思路:写一个循环,条件为本月开始日期.本月截至日期,通过循环获取第一个周六,加一天就是周日,每增加六天就是下一个周六,依次类推,循环到月末 代 ...
- 开启HSTS让浏览器强制跳转HTTPS访问
开启HSTS让浏览器强制跳转HTTPS访问 来源 https://www.cnblogs.com/luckcs/articles/6944535.html 在网站全站HTTPS后,如果用户手动敲入网站 ...
- 关于linux中关在共享文件的NFS 提示错误解决办法
0. 查看挂载情况命令 : findmnt 1. 如果在客户机上遇到如下这样的提示错误,有可能的原因是因为没有安装nfs-utils 只需要yum install nfs-utils 就解决了 ...
- Python UDP 通信
UDP_Client.py import socket # 创建一个 UDP 套接字 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DG ...
- Linux的关机和重启命令
Linux有如下的关机和重启命令:shutdown, reboot, halt, poweroff,那么它们有什么区别呢? shutdown - 建议使用的命令 shutdown是最常用也是最安全的关 ...
- Cryptography -- 密码学
Introduction to Cryptography Cryptography enables you to store sensitive information or transmit it ...
- MySQL数据库的启动与停止
有时候我们在任务管理器中无意识的结束了数据库的程序,再用可视化工具连接 时就会显示NO CONNECTION的情况,这样就需要重启数据库服务. 1. 我的电脑右键->管理->服务和应用程序 ...
- vue源码实现的整体流程解析
一.前言 最近一直在使用vue做项目,闲暇之余查阅了一些关于vue实现原理的资料,一方面对所了解到的知识做个总结,另外一方面希望能对看到此文章的同学有所帮助.本文如有不足之处,还请过往的大佬批评指正. ...
- Django drf:版本控制、django的缓存
一.版本控制 二.django的缓存 一.版本控制 1.内置版本控制类 from rest_framework.versioning import QueryParameterVersioning,A ...
- PAT Basic 1078 字符串压缩与解压 (20 分)
文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符没有重复,就原样输出.例如 ...