入门题
Subsequence
题面
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24139 Accepted: 10212
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
Source
Southeastern Europe 2006

题目(思维)分析
剖析这个题目会发现它就是毛毛虫爬行模型,即尺取法
尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案。之所以需要掌握这个技巧,是因为尺取法比直接暴力枚举区间效率高很多,尤其是数据量大的
时候,所以尺取法是一种高效的枚举区间的方法,一般用于求取有一定限制的区间个数或最短的区间等等。当然任何技巧都存在其不足的地方,有些情况下尺取法不可行,无法得出正确答案。
使用尺取法时应清楚以下四点:
1、 什么情况下能使用尺取法? 2、何时推进区间的端点? 3、如何推进区间的端点? 4、何时结束区间的枚举?
尺取法通常适用于选取区间有一定规律,或者说所选取的区间有一定的变化趋势的情况,通俗地说,在对所选取区间进行判断之后,我们可以明确如何进一步有方向地推进区间端点以求解满足条件的区间,如果已经判断了目前所选取的区间,但却无法确定所要求解的区间如何进一步得到根据其端点得到,那么尺取法便是不可行的。首先,明确题目所需要求解的量之后,区间左右端点一般从最整个数组的起点开始,之后判断区间是否符合条件在根据实际情况变化区间的端点求解答案。

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
typedef struct{
	int Start;///Start表示毛毛虫的初始位置
	int End;///End表示毛毛虫的终止位置
}P;
P seq;///seq表示毛毛虫本身
int n,s,a[maxn],sum,cnt=maxn;///a[]数组存放的是坐标上的值
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
	cnt=maxn;
	scanf("%d%d",&n,&s);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	seq.Start=seq.End=1;///开始毛毛虫起始坐标为(1,1)
	sum=a[1];///开始毛毛虫的value值
		for(int i=seq.End+1;i<=n;i++){///毛毛虫向前移动一步之后的操作
			seq.End=i;///毛毛虫移动一步之后终止位置发生改变
			sum+=a[i];///毛毛虫的value值更新
			if(sum<s){///毛毛虫的value值没有达到要求就再往前走一步
				continue;
			}
			while(sum>=s){///毛毛虫的value值达到要求的操作
			cnt=min(cnt,seq.End-seq.Start+1);///毛毛虫的最短长度更新
			sum=sum-a[seq.Start];///毛毛虫的value开始变化
			seq.Start+=1;///毛毛虫的初始位置开始加1
			}
		}
        if(cnt==maxn){printf("%d\n",0);}
        else{
	printf("%d\n",cnt);
        }
    }
    return 0;
}

附加lower_bound与upper_bound

几个月之后尝试再写一遍,思路变得清晰了

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[100005];
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,ans=100005,left=0,right=1,sum=0;
		ll s;
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sum=a[1];
		while(right<=n){
			if(sum>=s){
				ans=min(ans,right-left);
				left++;
				sum-=a[left];
			}
			else{
				right++;
				sum+=a[right];
			}
		}
		if(ans==100005){
			printf("0\n");
		}
		else{
			printf("%d\n",ans);
		}
	}
	return 0;
}

Jessica’s Reading Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18662 Accepted: 6450
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
Source
POJ Monthly–2007.08.05, Jerry

///(1)利用set找出一共有多少知识点
///(2)尺取法开始从两端进行滚动
///{ 开始start=end=1; map[a[1]]++;cnt++;
/// 一直向右爬行 每次都判断map[a[i]]是否等于0;然后判断cnt是否++
///map<int,int> 知识点-->出现的次数
///爬到cnt=n的时候就开始while循环缩}
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
const int maxn=1e6+5;
int a[maxn];
typedef struct{
	int Start;
	int End;
}P;
P seq;
int main(){
    int n,Size,cnt;
    int ans=maxn;
    map<int,int>c;
    set<int>all;
    scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);///a[i]表示第i页的知识点
		all.insert(a[i]);
	}
	Size=all.size();
	seq.Start=seq.End=1;
	cnt=1;
	c[a[1]]++;
	for(int i=seq.End+1;i<=n;i++){
		seq.End=i;
		if(c[a[i]]==0) {
			cnt++;
		}
		c[a[i]]++;
		if(cnt==Size){
			ans=min(ans,seq.End-seq.Start+1);
			while(1){
				c[a[seq.Start]]--;
                                seq.Start++;
				if(c[a[seq.Start-1]]==0){
					cnt--;
					break;
				}
				ans=min(ans,seq.End-seq.Start+1);
			}
		}
	}
	cout<<ans<<endl;

    return 0;
}

进阶题
POJ2566

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const ll inf=1e15+5;
const int maxn=1e5+5;
struct P{
	ll val;
	int loc;
}a[maxn];
bool cmp(P a, P b){
	return a.val<b.val;
}
ll myabs(ll x){
    return x>=0? x:-x;
}
int main(){
	int n;
	ll k,tmp;
	while(scanf("%d%lld",&n,&k),n&&k){
		a[0].val=a[0].loc=0;
		for(int i=1;i<=n;i++){
			scanf("%lld",&tmp);
			a[i].val=a[i-1].val+tmp;
			a[i].loc=i;
		}
		sort(a,a+n+1,cmp);
		while(k--){
		ll ans=inf;
		pair<int,int>q;
		ll num;
		int left=0,right=1;
		scanf("%lld",&num);
		while(right<=n){
			if(left==right){
				right++;
				continue;
			}
			ll ans1=a[right].val-a[left].val;
			if(myabs(ans1-num)<myabs(ans-num)){
				ans=ans1;
				q.first=a[left].loc;
				q.second=a[right].loc;
			}
			if(ans1<num){
				right++;
			}
			else if(ans1>num){
				left++;
			}
			else{
				break;
			}
		}
		if(q.first<q.second){
			printf("%lld %d %d\n",ans,q.first+1,q.second);
		}
		else{
			printf("%lld %d %d\n",ans,q.second+1,q.first);
		}
		}
	}
	return 0;
}