[ACM] techniques commonly used scale borrowing

The method is repeated advance of the beginning and end of the range, the minimum interval to seek to meet the conditions of the foot is called emulated.


Foot four elements of borrowing:

  • When using a ruler emulated?
  • When will promote the endpoint?
  • How to promote the endpoint?
  • Enumeration end zone when?

Here a few examples to illustrate how the foot borrowing should be used:

POJ 3061 Subsequence [portal]

A given length n of a sequence of positive integers, and the integer S. Determines the sum S is not less than a contiguous subsequence of minimum length. If the solution does not exist, the output 0

Analysis: As the sequence are integers, we set up on behalf of the two endpoints of a head (st), a representative of the end (en) which are two key endpoints foot borrowing, the initial values ​​are 0 (ie, the beginning of the array), and then we with an infinite loop to traverse the array, when the end of the end is less than the sum en S and less than n, the sum accumulated and accumulated en, en until the sum is greater than, or greater than S n so far. Analyzing the relationship between the sum S then, if this interval is less than S, and break out of the endless loop. Otherwise, take the minimum length ans en-st, and then subtracting the sum of the value of the beginning of the end, continue to the next step until the last ......

AC Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 1<<30
#define sscc ios::sync_with_stdio(false)
const int MAXN = 100000+5;
typedef unsigned long long ull; 

int n,S;
int a[MAXN];

void solve(){
	int sum=0;
	int ans=INF;
	int st=0,en=0;
	while(1){
		while(en<n&&sum<S)
			sum+=a[en++];
		if(sum<S) break;
		ans=min(ans,en-st);
		sum-=a[st++];
	}
	if(ans==INF) ans=0;
	cout << ans << endl;
}

int main(){
	sscc;	 
	int T;
	cin >> T;
	while(T--){
		mem(a,0);
		cin >> n >> S;
		for(int i=0;i<n;i++)
			cin >> a[i];
		solve();
	}
	return 0;
}

POJ 3320 Jessica's Reading Problem [portal]

There are textbooks P Pages, i happen to have a knowledge ai, the book may be referred to the same ground several times, so she hopes to read some pages of all the knowledge points are covered.

Analysis: The above topics, like, two endpoints, with set to record the total number of knowledge points, with a map stored knowledge map number to appear, and then follow the above board on it.

Note: use cin, cout time out ......

AC Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 1<<30
#define sscc ios::sync_with_stdio(false)
const int MAXN = 100000+5;
typedef unsigned long long ull; 

int main(){
//	sscc;
	set<int> s;
	int P;
	int a[MAXN];
	scanf("%d",&P);
	for(int i=0;i<P;i++){
		scanf("%d",&a[i]);
		s.insert(a[i]);
	}
	int n=s.size();

	int st=0,en=0,ans=INF,num=0;
	map<int,int> count;
	while(1){
		while(en<P&&num<n)
			if(count[a[en++]]++ ==0)
				num++;
		if(num<n) break;
		ans=min(ans,en-st);
		if(--count[a[st++]]==0)
			num--;
	}
	printf("%d\n",ans);
	return 0;
}

 

Published 177 original articles · won praise 282 · views 30000 +

Guess you like

Origin blog.csdn.net/lesileqin/article/details/100828072