尺取法

问题

方法的思想

The idea is to check elements in a way that’s reminiscent of movements of a caterpillar.

The caterpillar crawls through the array. We remember the front and back positions of the

caterpillar, and at every step either of them is moved forward.

分析

基本思想就是让 catepillar 表示 和不大于 s 的连续子数组

Each position of the caterpillar will represent a different contiguous subsequence in which

the total of the elements is not greater than s. Let’s initially set the caterpillar on the first

element. Next we will perform the following steps:

  • if we can, we move the right end (front) forward and increase the size of the caterpillar;
  • otherwise, we move the left end (back) forward and decrease the size of the caterpillar.

In this way, for every position of the left end we know the longest caterpillar that covers

elements whose total is not greater than s. If there is a subsequence whose total of elements

equals s, then there certainly is a moment when the caterpillar covers all its elements.

代码

  1. /**
  2. * Caterpillar Method
  3. * (s, t) move forward
  4. * O(N) amortized time
  5. */
  6. bool existed(vector<int> &vec, int target)
  7. {
  8. if(vec.empty()) return false;
  9. int front(0), sum(0);
  10. for(int back(0); back<vec.size(); ++back) {
  11. while(front < vec.size() && sum + vec[front] <= target) {
  12. sum += vec[front];
  13. ++ front;
  14. }
  15. if(sum == target) return true;
  16. sum -= vec[back];
  17. }
  18. return false;
  19. }
  1. def caterpillarMethod(A, s):
  2. n = len(A)
  3. front, total = 0, 0
  4. for back in xrange(n):
  5. while (front < n and total + A[front] <= s):
  6. total += A[front]
  7. front += 1
  8. if total == s:
  9. return True
  10. total -= A[back]
  11. return False

类似的题目

Minimum window substring

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the
length of 1.

  1. int lengthOfLongestSubstring(string str) {
  2. if(str.size() < 2) return str.size();
  3. vector<int> hash(256);
  4. int res(0);
  5. int front(0), back(0);
  6. for(; back<str.size(); ++back) {
  7. while(front < str.size() && hash[str[front]] == 0) {
  8. ++hash[str[front]];
  9. ++ front;
  10. }
  11. res = max(res, front-back);
  12. --hash[str[back]];
  13. }
  14. return res;
  15. }

有 n 根棍子,计算能够组成的三角形的数目(棍子可以重用)。

具体地说,we have to count the number of triplets at indices x < y < z, such that Ax <= Ay <= Az, 且 Ax +Ay > Az

  1. def triangles(A):
  2. n = len(A)
  3. result = 0
  4. for x in xrange(n):
  5. z = 0
  6. for y in xrange(x + 1, n):
  7. while (z < n and A[x] + A[y] > A[z]):
  8. z += 1
  9. result += z - y - 1
  10. return result

更多题目见 codility training center

尺取法,布布扣,bubuko.com

时间: 2024-10-04 01:33:00

尺取法的相关文章

luogu 1712 区间(线段树+尺取法)

题意:给出n个区间,求选择一些区间,使得一个点被覆盖的次数超过m次,最小的花费.花费指的是选择的区间中最大长度减去最小长度. 坐标值这么大,n比较小,显然需要离散化,需要一个技巧,把区间转化为半开半闭区间,然后线段树的每一个节点表示一个半开半闭区间. 接着我们注意到需要求最小的花费,且这个花费只与选择的区间集合中的最大长度和最小长度有关. 这意味着如果最大长度和最小长度一定,我们显然是需要把中间长度的区间尽量的选择进去使答案不会变的更劣. 不妨把区间按长度排序,枚举每个最小长度区间,然后最大区间

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

hihocoder-1483区间价值 (二分+尺取法)

题目链接: 区间价值 给定n个数A1...An,小Ho想了解AL..AR中有多少对元素值相同.小Ho把这个数目定义为区间[L,R]的价值,用v[L,R]表示. 例如1 1 1 2 2这五个数所组成的区间的价值为4. 现在小Ho想知道在所有的的v[L,R](1 <= L <= R <= n)中,第k小的值是多少. Input 第一行一个数T(T<=10),表示数据组数. 对于每一组数据: 第一行两个数n,k(1<=n<=200,000,1<=k<=n*(n+1

【转】毛虫算法&mdash;&mdash;尺取法

转自http://www.myexception.cn/program/1839999.html 妹子满分~~~~ 毛毛虫算法--尺取法 有这么一类问题,需要在给的一组数据中找到不大于某一个上限的"最优连续子序列" 于是就有了这样一种方法,找这个子序列的过程很像毛毛虫爬行方式,我管它叫毛毛虫算法,比较流行的叫法是"尺取法". 喏,就像图里的妹纸一样~ 还是举个栗子: Poj3061 给长度为n的数组和一个整数m,求总和不小于m的连续子序列的最小长度 输入 n = 1

51nod1127(尺取法)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1127 题意:中文题诶- 思路:尺取法 维护一个队列,若当前队首的元素在后面出现了,那么我们就将其删除,若当前队列里含有26个字母,我们就记录其size. 取所有size里面的最小值就是我们要的答案... 代码: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string>

BestCoder Round #86 二,三题题解(尺取法)

第一题太水,跳过了. NanoApe Loves Sequence题目描述:退役狗 NanoApe 滚回去学文化课啦! 在数学课上,NanoApe 心痒痒又玩起了数列.他在纸上随便写了一个长度为 nnn 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值. 他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少. 输入描述 第一行为一个正整数 T,表示数据组数. 每组数

POJ 3320 尺取法,Hash,map标记

1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识点 必须说,STL够屌.. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio>

HDU 5672 String 尺取法追赶法

String Problem Description There is a string S.S only contain lower case English character.(10≤length(S)≤1,000,000)How many substrings there are that contain at least k(1≤k≤26) distinct characters? Input There are multiple test cases. The first line

hdu-5672 String(尺取法)

题目链接: String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 有一个 10\leq10≤长度\leq 1,000,000≤1,000,000 的字符串,仅由小写字母构成.求有多少个子串,包含有至少k(1 \leq k \leq 26)k(1≤k≤26)个不同的字母? 输入描述 输入包含多组数据. 第一行有一个整数T (1\leq T\leq 10)T(1≤T≤1

poj 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Accepted: 2824 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent littl