日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 803D】Magazine Ad(二分答案)

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 803D】Magazine Ad(二分答案) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:

There are space-separated non-empty words of lowercase and uppercase Latin letters.

There are hyphen characters?'-'?in some words, their positions set word wrapping points. Word can include more than one hyphen.

It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.

When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.

The ad can occupy no more that?k?lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.

You should write a program that will find minimal width of the ad.

Input

The first line contains number?k?(1?≤?k?≤?105).

The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don't exceed?106?characters.

Output

Output minimal width of the ad.

Examples

Input

4 garage for sa-le

Output

7

Input

4 Edu-ca-tion-al Ro-unds are so fun

Output

10

Note

Here all spaces are replaced with dots.

In the first example one of possible results after all word wraps looks like this:

garage. for. sa- le

The second example:

Edu-ca- tion-al. Ro-unds. are.so.fun

題目大意:

輸入一個k和一個字符串s,最多可以把s分成k行,分割位置只能是-或者空格,求分割后最長一行長度的最小值。

解題報告:

? ?以前做過一個類似的,直接二分枚舉答案就可以了,注意fit中直接讓cnt=1,表示,當前在湊第cnt個,這樣設計cnt是為了表示最后一個點,需要多一個,所以干脆直接cnt=1好了,而且也有比較合理的解釋。

AC代碼:

#include<bits/stdc++.h>using namespace std; char s[1000005]; int ok[1000005]; int len,tot,k; bool fit(int x) {int cur = -1,cnt = 1;for(int i = 1; i<=tot; i++) {if(ok[i] - cur > x) {i--;cnt++;cur = ok[i];} if(cnt > k) return 0;}return 1; } int main() {int maxx = 0;cin>>k;getchar(); gets(s);len = strlen(s);ok[0] = -1;for(int i = 0; i<len; i++) {if(s[i] == ' ' ||s[i] == '-') {ok[++tot] = i;maxx = max(maxx,ok[tot] - ok[tot-1]);}}ok[++tot] = len-1;int l = maxx,r = len;int mid = (l + r) / 2;while(l < r) {mid = (l + r ) / 2 ;if(fit(mid)) r = mid;else l = mid + 1;} printf("%d\n",l);return 0 ;}

總結:l=maxx開始,因為這樣就避免了fit中需要先循環判斷一次是否有一個串的長度也不符合的,r從len開始,其實從哪開始都可以,剛開始寫的是len/k +1 不知道自己是怎么想的。。。肯定是錯的反正。

總結

以上是生活随笔為你收集整理的【CodeForces - 803D】Magazine Ad(二分答案)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。