尺取法+例题

 尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案.
 一般用于求取有一定限制的区间个数或最短的区间等等.
 [注]: 在对所选取区间进行判断之后,我们可以明确如何进一步有方向地推进区间端点以求解满足条件的区间,如果已经判断了目前所选取的区间,但却无法确定所要求解的区间如何进一步得到根据其端点得到,那么尺取法便是不可行的。
 首先,明确题目所需要求解的量之后,区间左右端点一般从最整个数组的起点开始,之后判断区间是否符合条件在根据实际情况变化区间的端点求解答案。

POJ 3061

POJ 3061

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了,那么不需要在向后推进右端点了,因为其和也肯定大于等于S但长度更长,所以,当区间和小于S时右端点向右移动,和大于等于S时,左端点向右移动以进一步找到最短的区间,如果右端点移动到区间末尾其和还不大于等于S,结束区间的枚举。
 这个题目区间和明显是有趋势的:单调变化,所以根据题目要求很容易求解,但是在使用之间需要对区间前缀和进行预处理计算。
emsp;POJ不能识别万能头文件

#include<iostream>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
int t,ans=INF;
ll sum,cur_sum=0;
int main(){
cin>>t;
while(t--){
cin>>n>>sum;
ll *p=new ll[n+10];
for(int i=0;i<n;i++)
cin>>p[i];
int l=0,r=0;
ans=INF,cur_sum=0;
while(1){
while(r<n&&cur_sum<sum)
cur_sum+=p[r++];
if(cur_sum<sum) break;
ans=min(ans,r-l);
cur_sum-=p[l++];
}
if(ans==INF)
ans=0;
cout<<ans<<endl;
}
return 0;
}
POJ 3320

POJ 3320

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 little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.
A very hard-working boy had manually indexed for her each page of Jessica’s text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica’s text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1

Sample Output

2

分析:
 和上面的题一样的思路,如果一个区间的子区间满足条件,那么在区间推进到该处时,右端点会固定,左端点会向右移动到其子区间,且其子区间会是更短的,只是需要存储所选取的区间的知识点的数量,那么使用map进行映射以快速判断是否所选取的页数是否覆盖了所有的知识点。


#include <cstdio>

#include <algorithm>

#include <cstring>

#include <set>

#include <map>

#define MAX 1000010

#define LL long long

#define INF 0x3f3f3f3f



using namespace std;

int a[MAX];

map <int, int> cnt;

set <int> t;

int p, ans = INF, st, en, sum;



int main()

{

scanf("%d", &p);

for (int i = 0; i < p; i++) scanf("%d", a+i), t.insert(a[i]);

int num = t.size();

while (1){

while (en<p && sum<num)

if (cnt[a[en++]]++ == 0) sum++;

if (sum < num) break;

ans = min(ans, en-st);

if (--cnt[a[st++]] == 0) sum--;

}

printf("%d\n", ans);

return 0;

}
POJ 2566

POJ 2566

Description

Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: “But I want to use feet, not meters!”). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We’ll not go into details, but researchers found out that a signal encodes two integer values. These can be found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest to t.
 You are given the sequence of n integers and the non-negative target t. You are to find a non-empty range of the sequence (i.e. a continuous subsequence) and output its lower index l and its upper index u. The absolute value of the sum of the values of the sequence from the l-th to the u-th element (inclusive) must be at least as close to t as the absolute value of the sum of any other non-empty range.

Input

The input file contains several test cases. Each test case starts with two numbers n and k. Input is terminated by n=k=0. Otherwise, 1<=n<=100000 and there follow n integers with absolute values <=10000 which constitute the sequence. Then follow k queries for this sequence. Each query is a target t with 0<=t<=1000000000.

Output

For each query output 3 numbers on a line: some closest absolute sum and the lower and upper indices of some range where this absolute sum is achieved. Possible indices start with 1 and go up to n.

Sample Input

5 1
-10 -5 0 5 10
3
10 2
-9 8 -7 6 -5 4 -3 2 -1 0
5 11
15 2
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
15 100
0 0

Sample Output

5 4 4
5 2 8
9 1 1
15 1 15
15 1 15

分析:
明显,借用第一题的思路,既然要找到一个子区间使得和最接近t的话,那么不断地找比当前区间的和更大的区间,如果区间和已经大于等于t了,那么不需要在去找更大的区间了,因为其和与t的差值更大,然后区间左端点向右移动推进即可。所以,首先根据计算出所有的区间和,
排序之后按照上面的思路求解即可。


#include <cstdio>

#include <algorithm>

#include <cstring>

#define INF 0x3f3f3f3f

#define LL long long

#define MAX 100010

using namespace std;



typedef pair<LL, int> p;

LL a[MAX], t, ans, tmp, b;

int n, k, l, u, st, en;

p sum[MAX];



LL myabs(LL x)

{

return x>=0? x:-x;

}



int main()

{

while (scanf("%d %d", &n, &k), n+k){

sum[0] = p(0, 0);

for (int i = 1; i <= n; i++){

scanf("%I64d", a+i);

sum[i] = p(sum[i-1].first+a[i], i);

}

sort(sum, sum+1+n);

while (k--){

scanf("%I64d", &t);

tmp = INF; st = 0, en = 1;

while(en <= n){

b = sum[en].first-sum[st].first;

if(myabs(t-b) < tmp){

tmp = myabs(t-b);

ans = b;

l = sum[st].second; u = sum[en].second;

}

if(b > t) st++;

else if(b < t) en++;

else break;

if(st == en) en++;

}

if (u < l) swap(u, l);

printf("%I64d %d %d\n", ans, l+1, u);

}

}

return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值