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. Python列表类型及常用操作

    Python列表类型 1.用途: 存放多个值,可以根据索引存取值 2.定义方式: 在[ ]内用逗号分割开多个任意类型的值 l=['yven','law','lyf'] #l=list(['yven', ...

  2. Vue.js - Day1

    什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站,还可以开发手机App, Vue语法也是可以用于进行手机App开发的,需要借助于We ...

  3. 浅谈position、table-cell、flex-box三种垂直(水平)居中技巧

    一.首先是喜闻乐见的position方法,经典且万能,用法如下: 父元素{ position:relative; } 子元素{ position:absolute; top:50%; left:50% ...

  4. poi读取excel的辅助类

    补充:对于这个工具已经转为一个工程项目,采用的是saxreader方式,支持大数据文件的读取.具体可以参照  github上的源码,使用可以简单参照wiki.项目wiki地址https://git.o ...

  5. MySQL入门很简单: 6 视图

    1. 视图含义作用 视图是虚拟的表,是从数据率中一个或多个表中导出来的表:  数据库中只存放了视图的定义,没有存放视图中的数据,数据在原先的表中:  一旦表中的数据发生变化,显示在视图中的数据也会发生 ...

  6. IOS 自定义代理delegate方法

    创建一个自定义代理 @class MJTgFooterView; /** 1.协议名称: 控件类名 + Delegate 2.代理方法普遍都是@optional 3. */ @protocol MJT ...

  7. CPU体系结构

    http://blog.csdn.net/liuxc0116/article/details/17004313 1.算术逻辑单元ALU(Arithmetic Logic Unit)ALU是运算器的核心 ...

  8. 数长方形有多少个?POJ(1693)

    题目链接:http://poj.org/problem?id=1693 解题报告: 随机选两根横的,再找一下与这两根横线相交的竖线有多少根,m,那么就有(m-1)*m/2个长方形. #include ...

  9. MapReduce计算每年最大值测试样例生成程序

    Demo.java package com.java; import java.io.BufferedWriter; import java.io.File; import java.io.FileW ...

  10. Ubuntu 12.04 the system is running in low-graphics mode

    1.出现问题如图所示: 2.解决方案: Ctrl + Alt + F1 df -h 输入密码,到了这一步,也是可以使用terminal,那么没有图形界面也是可以的 cd /etc/X11 sudo c ...