POJ 3061 Subsequence ( 尺取法)
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
2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
Sample Output
2
3
分析:
由于所有的元素都大于零,如果子序列[s,t)满足As+···+At-1>=S,那么对于所有的t<t'一定有As+···+At'-1>=S。此外对于区间[s,t)上的总和来说如果令
sum(i) = A0+A1+···+Ai-1;
那么
As+As-1+···+At-1=sum(t)-sum(s)
那么预先以O(n)的时间计算好sum的话,就可以以O(1)的时间计算区间上的总和。这样一来,子序列的起点确定以后,便可以用二分搜索快速地确定使序列和不小于S的结尾t的最小值。
int b[100009];
int sum[100009];///保存的是前i个元素的和
void solve1()
{
for(int i=0; i<n; i++)
{
sum[i+1]=sum[i]+b[i+1];
}
if(sum[n]<S)///解不存在
{
printf("0\n");
return ;
}
int res=n;
for(int s=0; sum[s]+S<=sum[n]; s++)
{
///利用二分搜索求出t
int t=lower_bound(sum+s,sum+n,sum[s]+S)-sum;
res=min(res,t-1);
}
printf("%d\n",res);
}
这个算法的时间复杂度比较大,我们可以用尺取法来解决。
尺取法
我们设以As开始总和最初大于S时的连续子序列为As+···+At-1,这时
As+1+···+At-2<As+···+At-2<S
所以从As+1开始总和最初超过S的连续子序列如果是As+1+···+At'-1的话,则必然有t<=t'。利用这一性质便可以设计出如下算法:
1.以 s=t=sum=0初始化
2.只要依然有sum<S,就不断将sum增加At,并将t加1
3.如果(2)中无法满足sum>=S则终止。否则的话,更新res=min(res,t-s)。
4.将sum减去As,s增加1然后回到(2)。
#include<stdio.h>
#include<iostream>
using namespace std;
int n,S,a[100009];
void solve()
{
int res=n+1;
int s=0,t=0,sum=0;
for(;;)
{
while(t<n&&sum<S)
{
sum+=a[t++];
}
if(sum<S) break;///此时跳出整个for循环,也就意味着当起始点s
///逐渐往后移动的时候,剩下的点不能满足和大于S
res=min(res,t-s);///res表示的是满足和大于S的最小的间距
sum-=a[s++];
}
if(res>n)///也就是说没有满足条件的间距
res=0;
printf("%d\n",res);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&S);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
solve();
}
return 0;
}

POJ 3061 Subsequence ( 尺取法)的更多相关文章
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3061 Subsequence(尺取法)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18145 Accepted: 7751 Desc ...
- POJ 3061 Subsequence 尺取法
转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...
- POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9050 Accepted: 3604 Descr ...
- poj 3061 题解(尺取法|二分
题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...
- POJ 3061 Subsequence 尺取
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14698 Accepted: 6205 Desc ...
- POJ 3061 Subsequence 二分或者尺取法
http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...
- 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- POJ 3061 Subsequence【二分答案】||【尺取法】
<题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...
随机推荐
- 设置启动窗体Program.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...
- Linux面试题汇总答案(转)
转自:小女生的Linux技术~~~Linux面试题汇总答案~~ 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的 ...
- 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp
题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...
- [洛谷P3627][APIO2009]抢掠计划
题目大意:给你一张$n(n\leqslant5\times10^5)$个点$m(m\leqslant5\times10^5)$条边的有向图,有点权,给你起点和一些可能的终点.问从起点开始,到任意一个终 ...
- BZOJ Lydsy5月月赛 ADG题解
题目链接 BZOJ5月月赛 题解 好弱啊QAQ只写出三题 A 判断多干个数乘积是否是某个数的倍数有很多方法,比较常用的是取模,但这里并不适用,因为模数不定 会发现数都比较小,所以我们可以考虑分解质因子 ...
- CF1073E Segment Sum 自闭了
CF1073E Segment Sum 题意翻译 给定\(K,L,R\),求\(L\)~\(R\)之间最多不包含超过\(K\)个数码的数的和. \(K<=10,L,R<=1e18\) 我 ...
- bzoj2058: [Usaco2010 Nov]Cow Photographs(逆序对)
题目大意:给出n个数的序列,每次可以交换相邻的两个数,问把序列变成编号i在编号i+1左边,编号1在编号n右边(一个环)最少需要多少步.如:35421最少交换两次变为34512. 一开始看到这题,只会O ...
- 【简单算法】22.删除链表的倒数第N个节点
题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: ->->->->, 和 n = . 当删除了倒数第二个节点后,链表变为 -& ...
- MongoDB基操
基本概念 database 数据库 包含多个collection collection 集合 包含多个文档document(类JSON对象) document 文档 一个文档对象中包含多个key-va ...
- js正则表达式,判断字符串是否以数字组结尾,并取出结尾的数字
js正则表达式,判断字符串是否以数字组结尾,并取出结尾的数字 <!DOCTYPE html> <html> <head> <meta charset=&quo ...