ACM常用基础——尺取法

39 篇文章 0 订阅
10 篇文章 0 订阅

尺取法:简单点来理解,就是在对一组数据处理的时候,每次选取适当的标尺(标尺左端,右端分别对应数组的下标i,j);在满足题目条件之间,不断往其中加入数据(标尺内容+=a[j] ;  j++) ,直到满足所给条件。这时固定右端 j 不动,右移左端 i ,还是满足条件的话,继续右移左端  i ,直到满足所给条件;之后继续移动左端 i,如此循环往复,直到右端 j 到达数组末尾;

简单例子:

代码:

#include<iostream>  
#include<algorithm>  
using namespace std;
int n; 
int a[200];
int main()
{
	int n,max,sum;
	cin>>n>>max;
        for(int k=0;k<n;k++)
	    cin>>a[k];
        int i=0,j=0,sum=0,ans=n+1;
	while(1)
	{
		while(j<n&&sum<max) //sum<max时不断循环,使得取得满足条件的序列
		sum += a[j++];
		if(sum < max)	break;  //若j>=n且sum小于max则输出
		ans=min(j-i,ans);      //记录最小的间距
		sum-=a[i++];           //i向右移动
	}
	cout<<ans<< endl;
	return 0;
}

例题:Codeforces Round #364 div.2 C. They Are Everywhere

Sergei B., the young coach of Pokemons, has found the big house which consists of nflats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.

There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.

Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.

Input

The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.

The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.

Output

Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.

Examples

Input

3
AaA

Output

2

Input

7
bcAAcbc

Output

3

Input

6
aaBCCe

Output

5

Note

In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.

In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.

In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.

题目大意:一共n个房子,里面有n个神奇宝贝(有重复的),求 存在所有种类神奇宝贝 的最小区间;

如样例3: 6 aaBCCe  所求区间应该是 aBCCe 所以结果是5;(想想其实和求最小区间和的题意思一样)

我的解法:1 :用set先求出总的种类数,保存到int  max;

                    2 :用一个map<char,int> popc来维护一个标尺,当标尺内的神奇宝贝种类数(popc.size())达到max时,移动左端;还满足条件就继续移动左端,否则移动右端。就是尺取法。

                    3 :往里面加元素,就popc[元素]++;删除即popc[元素]--;这里要注意map的特性:当它里面元素的个数减为0的时候,储存这个数据的区域还在;即这时候map的size不会自动减一:如图:

以下是两种风格的尺取做法,请自行对比阅读:

代码1:

#include<iostream>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
char a[200000]; 
int main(){
	int n;
	while(cin>>n){
		char c;
	    set<char> cc;
	    map<char,int> popc;
	    for(int i=0;i<n;i++){    //下标是0~n-1 
	   	    cin>>a[i];
	   	    cc.insert(a[i]); 
	    }
	    int max=cc.size();       //一共有多少不重复的元素;	
	    int l=0,r=0,ans=n+1;     //要找一个小值,先把ans赋予一个比较大的值; 
	    
            popc[a[0]]++;           //先把第一位存进去 
	    while(l<=r&&r<n){
	    	if(popc.size()==max){     //满足标尺条件的时候 
	    	    ans=min(r-l+1,ans);   //记录间距 
		    popc[a[l]]--;         //l右移 
              	    if(popc[a[l]]==0){
			 popc.erase(a[l]); }
	    		  ++l; }
			else{
			   ++r;	
			 popc[a[r]]++;        //r右移 
			}
		}
		cout<<ans<<endl; 
	}
	return 0;
}

代码2:

#include<iostream>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
char a[200000]; 
int main(){
	int n;
	while(cin>>n){
		char c;
	    set<char> cc;
	    map<char,int> popc;
	    for(int i=0;i<n;i++){    //下标是0~n-1 
	   	    cin>>a[i];
	   	    cc.insert(a[i]); 
	    }
	    int max=cc.size();       //一共有多少不重复的元素;	
	    int l=0,r=0,ans=n+1;     //要找一个小值,先把ans赋予一个比较大的值; 
	    
		while(true){
	    	while(r<n && popc.size()<max)
	    	{ popc[a[r]]++;
			  r++;   //r右移 
			}
			if(popc.size()<max) break;  //r=n的时候,标尺内的元素数量还小于max,跳出循环 
			
			ans=min(r-l,ans);        //ans记录最小尺度;
			popc[a[l]]--;            //l右移
			if(popc[a[l]]==0){       //判断下标尺内a[l]的个数,为0就删除这个元素的储存区域 
			   popc.erase(a[l]); 	 //因为map的元素个数为0后,这个元素的储存区域还在
                                     //map的size()不会自动减一; 
			}
			l++;  
		}
		cout<<ans<<endl; 
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值