Subsequence

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 18795 Accepted: 8043

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


解题心得:

  1. 题意就是给你n个数,要你选择连续的一段区间,其总和不小于s,问你最短的区间长度是多少。
  2. 尺取法的模板题,尺取法很有意思,也被称之为尺取虫,这个可以说是很形象了,可以看成是一个一定长度的虫在线性表中爬动,复杂度就是O(N)。关于尺取有很多种写法,不同的写法复杂度不同,这里讲三种写法
    • 第一种就是二分加尺取法,首先二分出一个长度,然后用尺取法去数组中检验这个二分出来的总和,复杂度为O(NlogN)。
    • 第二种感觉和尺取关系不大,用的是二分,先求出前缀和,以每一个前缀和为起点,当前前缀和+s为终点,因为前缀和是一个递增的数列,所以就可以二分查找终点,然后得到长度。复杂度为O(NlogN)。
    • 第三种就是尺取,只不过这个尺取虫的长度是可以变换的,我们找一个最小的长度就行了,从起点开始每次保证尺取范围的总和不小于s,同时向后面移动。复杂度为O(N).

第一种尺取代码

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn = 1e5+10; int num[maxn],n,s; void init() {
scanf("%d%d",&n,&s);
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
} bool checke(int len) {
int sum = 0;
for(int i=0;i<len;i++)
sum += num[i];
if(sum >= s)
return true;
int l = 0,r = len-1;
while(r < n) {//
sum -= num[l];
l++,r++;
sum += num[r];
if(sum >= s)
return true;
}
return false;
} int binary_search() {//二分尺取虫的长度
int l = 1 ,r = n;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(checke(mid))
r = mid;
else
l = mid;
}
return r;
} bool check_ans() {
int sum = 0;
for(int i=0;i<n;i++)
sum += num[i];
if(sum < s)
return true;
return false;
} int main() {
int t;
scanf("%d",&t);
while(t--) {
init();
if(check_ans()) {//如果总和都小于s直接输出0
printf("0\n");
continue;
}
int ans = binary_search();
printf("%d\n", ans);
}
return 0;
}

第二种写法代码:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn = 1e5+10; int sum[maxn],n,s; void init() {
scanf("%d%d",&n,&s);
for(int i=1;i<=n;i++) {
int temp;
scanf("%d",&temp);
sum[i] = sum[i-1]+temp;
}
} int solve() {
int ans = 0x7f7f7f7f;
for(int i=0;sum[i]+s<=sum[n];i++) {
int last = sum[i] + s;
int pos = lower_bound(sum+i,sum+n+1,last) - sum;//就是一个二分查找
int len = pos - i;
ans = min(ans,len);
}
return ans;
} int main() {
int t;
scanf("%d",&t);
while(t--) {
init();
int ans = solve();
if(sum[n] < s)
ans = 0;
printf("%d\n",ans);
}
return 0;
}

第三种写法代码:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn = 1e5+10; int n,s,num[maxn]; int main() {
int t;
scanf("%d",&t);
while(t--) {
int sum,l,r,ans;
sum = l = r = 0;
ans = 0x7f7f7f7f;
scanf("%d%d", &n, &s);
for (int i = 0; i < n; i++)
scanf("%d", &num[i]);
while (1) {
while (sum < s && r < n) {
sum += num[r++];
}
if (sum < s)
break;
ans = min(ans, r - l);
sum -= num[l++];
}
if (ans > n)
ans = 0;
printf("%d\n",ans);
}
return 0;
}

POJ:3061-Subsequence(尺取法模板详解)的更多相关文章

  1. POJ 3061 Subsequence(尺取法)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18145   Accepted: 7751 Desc ...

  2. POJ 3061 Subsequence 尺取法

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

  3. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  4. POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 Descr ...

  5. poj 3061 题解(尺取法|二分

    题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...

  6. POJ 3061 Subsequence 尺取

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14698   Accepted: 6205 Desc ...

  7. C++模板详解

    参考:C++ 模板详解(一) 模板:对类型进行参数化的工具:通常有两种形式: 函数模板:仅参数类型不同: 类模板:   仅数据成员和成员函数类型不同. 目的:让程序员编写与类型无关的代码. 注意:模板 ...

  8. 25.C++- 泛型编程之函数模板(详解)

    本章学习: 1)初探函数模板 2)深入理解函数模板 3)多参函数模板 4)重载函数和函数模板 当我们想写个Swap()交换函数时,通常这样写: void Swap(int& a, int&am ...

  9. 26.C++- 泛型编程之类模板(详解)

    在上章25.C++- 泛型编程之函数模板(详解) 学习了后,本章继续来学习类模板   类模板介绍 和函数模板一样,将泛型思想应用于类. 编译器对类模板处理方式和函数模板相同,都是进行2次编译 类模板通 ...

随机推荐

  1. (C# 基础) 接口

    接口是表示一组函数成员,而不实现成员的引用类型.类和结构可以实现接口. 例如BCL声明了一个叫做IComparable的接口,包含了一个CompareTo方法, 但没有实现其方法,用“:”结尾. pu ...

  2. c++ 处理utf-8字符串

    c++的字符串中的每一个元素都是一个字节.所以在装入utf8字符串的时候,其实是按照一定的规则编码的. 字符的8位中 如果0开头 则自己就是一个单位. 1字节 0xxxxxxx  2字节 110xxx ...

  3. u-boot分析(十一)----MMU简单分析|u-boot分析大结局|学习规划

    u-boot分析(十一) 通过前面十篇博文,我们已经完成了对BL1阶段的分析,通过这些分析相信我们对u-boot已经有了一个比较深入的认识,在BL2阶段大部分是对外设的初始化,并且有的我们已经分析过, ...

  4. selenium代理

    selenium.KeyDown("id=ctaskName", "d");            selenium.KeyPress("id=cta ...

  5. oracle自动异地备份数据库

    需求:实现oracle自动异地备份数据库 分析:1.oracle备份数据库     2.自动备份(定时)   3.非本地备份(因为如果备份到本地的话,如果硬件设备损坏可能导致数据丢失) 知识点:1.备 ...

  6. Cholesky分解(Cholesky decomposition / Cholesky )

    Cholesky decomposition In linear algebra, the Cholesky decomposition or Cholesky is a decomposition ...

  7. 【Tim Sweeney】Why C++ for Unreal 4?

    The first three generations of the Unreal Engine included a sandboxed scripting language, UnrealScri ...

  8. 如何启用SAP C4C OData Event Notification

    当我们在试图使用SAP C4C OData事件通知这个功能时,如果遇到下列提示消息,说明这个功能在business configuration里没有开启: The OData Event Notifi ...

  9. 2018.7.30 Oracle的Blog数据库类型读取和存

    package com.lanqiao.shopping.test; import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  10. 使用QT开发GoogleMap瓦片显示和下载工具(1)——QT开发环境准备

    由于是第一次使用qt,光是QT的安装和调试就费了好大功夫,汗一个,下面记录下过程和遇到的问题的解决方法吧. 下载QT 直接Google搜索“QT”,进入官网http://qt-project.org/ ...