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. P2626 斐波那契数列(升级版)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数). 题目描述 ...

  2. MarkDown 编辑器学习

    MarkDown 编辑器学习 是一种简单快键的文字排版工具,可以用于编写说明文档,鉴于其语法简洁明了,且其渲染生成的样式简单美观,很多开发者也用它来写博客,已被国内外很多流行博客平台所支持.生成的文件 ...

  3. java.lang.OutOfMemoryError: Failed to allocate a 3110419 byte allocation with 741152 free bytes and

    在进行SurfaceView的开发时,出现了java.lang.OutOfMemoryError错误: 由于项目是同时显示四路远端传输过来的视频 所以采用的方法是使用:android:hardware ...

  4. oracle11g在CentOS6.9上启动脚本

    #!/bin/bash# chkconfig:2345 99 10# description: Startup Script for oracle Database# /etc/init.d/orac ...

  5. MySQL入门很简单: 3 操作数据库

    登陆:mysq -u root -p 0409 1). 创建, 删除数据库 SHOW DATABASES; 显示已经存在的数据率 CREATE DATABASES 数据库名: 创建数据库 DROP D ...

  6. CNN中卷积的意义

    在传统的神经网络中,比如多层感知机(MLP),其输入通常是一个特征向量.需要人工设计特征,然后将用这些特征计算的值组成特征向量.在过去几十年的经验来看,人工找的特征并不总是好用.有时多了,有时少了,有 ...

  7. theano中tensor的构造方法

    import theano.tensor as T x = T.scalar('myvar') myvar = 256 print type(x),x,myvar 运行结果: <class 't ...

  8. flexslider 图片轮播插件参数

    $(window).load(function() { $('.flexslider').flexslider({ animation: "fade", //String: Sel ...

  9. Search in Rotated Sorted Array——LeetCode

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  10. 页面js中文乱码解决

    <script src="../Content/kindeditor-4.1.5/kindeditor.js" charset="utf-8" >& ...