www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > > 充電吧
[導(dǎo)讀]【CF簡介】提交鏈接:CF 645C題面:C. Enduring Exodus time limit per test 2 seconds memory limit per test 256 me

【CF簡介】

提交鏈接:CF 645C


題面:


C. Enduring Exodus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and hisk cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists ofn rooms located in a row, some of which are occupied.

Farmer John wants to book a set of k?+?1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j?-?i|. Help Farmer John protect his cows by calculating this minimum possible distance.

Input

The first line of the input contains two integers n andk (1?≤?k?<?n?≤?100?000)?— the number of rooms in the hotel and the number of cows travelling with Farmer John.

The second line contains a string of length n describing the rooms. Thei-th character of the string will be '0' if thei-th room is free, and '1' if thei-th room is occupied. It is guaranteed that at leastk?+?1 characters of this string are '0', so there exists at least one possible choice ofk?+?1 rooms for Farmer John and his cows to stay in.

Output

Print the minimum possible distance between Farmer John's room and his farthest cow.

Examples Input

7?2
0100100

Output

2

Input

5?1
01010

Output

2

Input

3?2
000

Output

1

Note

In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.

In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.

In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1.












--------------------------------------------------------------------------------------啦啦啦,我是分割線--------------------------------------------------------------------------------------------------------------

題意:

??? 一個(gè)農(nóng)夫帶著k頭牛去住店,(人一間,每牛一間)已知該旅店共有n間房,其中部分房間已有人住,房間住宿情況由01串表示,0表示空,1表示已有人住,剩余房間足夠容納(k+1)頭牛/人。為了保障牛的安全,希望人住的房間離最遠(yuǎn)的牛的房間位置盡量小,輸出最小距離。


思路:

??? 很明顯為了讓人住的離最遠(yuǎn)的牛最近,那么最后這批人牛住的房間肯定是連續(xù)的(不算原有的住宿人員),且人住的位置離中心點(diǎn)越近越好。首先,枚舉住宿的左區(qū)間,如果左端點(diǎn)已有人住,則跳過該點(diǎn),如果空,則二分以該點(diǎn)為左端點(diǎn),空閑房間數(shù)為k+1的最左位置。隨后在這個(gè)區(qū)間內(nèi),開始尋找空閑的最中心位置(距離遠(yuǎn)的那端盡量?。脜^(qū)間長度奇偶性設(shè)置兩個(gè)指針p1,p2的初始位置,隨后分別往兩端移動(dòng),因?yàn)橐欢ㄓ锌臻e位置,且兩指針同時(shí)移動(dòng),不會(huì)出現(xiàn)越界情況。最后找到一個(gè)解,則計(jì)算最遠(yuǎn)距離,若小于最優(yōu)值,則更新。


代碼:


#include#include#include#includeusing?namespace?std;
char?s[100010];
int?room[100010];
int?main()
{
????int?n,k,len,le,ri,border,ans,pos;
	//讀入
	scanf("%d%d",&n,&k);
	scanf("%s",s);
	room[0]=0;
	for(int?i=1;i<=n;i++)
	{
		//前綴和
		if(s[i-1]-'0')
			room[i]=room[i-1];
		else
			room[i]=room[i-1]+1;
	}
	//設(shè)置一個(gè)肯定會(huì)被更新的最大值
	ans=10e6;
	for(int?i=1;i<=n;i++)
	{
		//該點(diǎn)已有人住
	???if(room[i]==room[i-1])
		???continue;
???????le=i;
	???ri=n;
	???border=-1;
	???//二分右區(qū)間
	???while(le>1;
		???if(room[mid]-room[i-1]>k)
		???{

			???border=mid;
			???ri=mid-1;
		???}
		???else
			???le=mid+1;
	???}
	???//如果能找到k+1個(gè)房間
	???if(border!=-1)
	???{
?????????int?len=(border-i),p1,p2;
		?//根據(jù)區(qū)間長度奇偶性,設(shè)置p1,p2
?????????if(len%2)
		?{
			?p1=(border+i)>>1;
			?p2=(border+i)/2+1;
		?}
		?else
		?{
			?p1=p2=(border+i)>>1;
		?}
		?//尋找最先出現(xiàn)空閑房間
		?while(1)
		?{
			?if((room[p1]!=room[p1-1]))
		????{
				pos=p1;
				break;
			}
			?else?if(room[p2]!=room[p2-1])
			?{
				?pos=p2;
				?break;
			}
			?p1--;
			?p2++;
		?}
?????????//更新最優(yōu)值
		?ans=min(ans,max(pos-i,border-pos));
	???}
	???//說明剩下,已無可能有k+1個(gè)房間
	???else
		???break;
	}
	printf("%dn",ans);
	return?0;
}





本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動(dòng)電源

在工業(yè)自動(dòng)化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動(dòng)力設(shè)備,其驅(qū)動(dòng)電源的性能直接關(guān)系到整個(gè)系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動(dòng)勢抑制與過流保護(hù)是驅(qū)動(dòng)電源設(shè)計(jì)中至關(guān)重要的兩個(gè)環(huán)節(jié),集成化方案的設(shè)計(jì)成為提升電機(jī)驅(qū)動(dòng)性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動(dòng)電源

LED 驅(qū)動(dòng)電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個(gè)照明設(shè)備的使用壽命。然而,在實(shí)際應(yīng)用中,LED 驅(qū)動(dòng)電源易損壞的問題卻十分常見,不僅增加了維護(hù)成本,還影響了用戶體驗(yàn)。要解決這一問題,需從設(shè)計(jì)、生...

關(guān)鍵字: 驅(qū)動(dòng)電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動(dòng)電源的公式,電感內(nèi)電流波動(dòng)大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計(jì) 驅(qū)動(dòng)電源

電動(dòng)汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動(dòng)汽車的核心技術(shù)之一是電機(jī)驅(qū)動(dòng)控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動(dòng)系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動(dòng)汽車的動(dòng)力性能和...

關(guān)鍵字: 電動(dòng)汽車 新能源 驅(qū)動(dòng)電源

在現(xiàn)代城市建設(shè)中,街道及停車場照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動(dòng)電源 LED

LED通用照明設(shè)計(jì)工程師會(huì)遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動(dòng)電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動(dòng)電源的電磁干擾(EMI)問題成為了一個(gè)不可忽視的挑戰(zhàn)。電磁干擾不僅會(huì)影響LED燈具的正常工作,還可能對周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動(dòng)電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動(dòng)電源

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開關(guān)電源

LED驅(qū)動(dòng)電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動(dòng)LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動(dòng)電源
關(guān)閉