Magazine Ad CodeForces - 803D(二分 + 貪心,第一次寫博客)

来源:https://www.cnblogs.com/DreamACMer/archive/2019/02/27/10446858.html
-Advertisement-
Play Games

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-s ...


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段,求分割後的最小寬度(寬度就是分割後最長的字元串的長度)
思路:博主是個菜雞,一開始沒啥思路,學姐講題的時候才想到用二分。。。。。。。。
  經過不斷地啃博客(正經臉),算是明白一點如何從 二分 入手這道題。
  把每一段字元串 按照一個長度 x 進行分割,得到的字元串長度不能超過 x ,看最後得到的行數是否小於k;
  如果按長度x進行分割得到的行數小於k 那麼按長度 x+1 進行分割所得到的行數必定小於k。
  註意 最小寬度 定義,這個時候我們就可以用二分,不斷地去逼近這個最小寬度,就可以得到答案。
  最後註意的就是二分的上下界,上界就是初始字元串的長度s.size(),下界可以是s.size()/k(這個博主就不解釋了)。
AC代碼:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 vector<int>v;
 4 string s;
 5 int k;
 6 bool check(int x){
 7     int line = 0;// 記錄以x切割時可以得到的行數     
 8     int len = 0; 
 9     for(int i = 0 ;  i < v.size();  i++){
10         if(v[i] > x){
11             return 0;// 如果v[i] > x 說明存在v[i]讓x無法表示(說明x取小了) 
12         }
13         if(v[i] + len > x){
14             line++;
15             len = v[i];
16         }else if(v[i] + len == x){
17             line++;
18             len = 0;
19         }else if(v[i] + len < x){
20             len += v[i];
21         }// 將字元串 以不大於x 的長度分割
22         // line 記錄進行分割後得到的總行數 
23     }
24     if(len > 0) line++;// 註意分割最後的一小段字元串是否忽略 
25     return line <= k;// 根據x分割所得到的總行數不能大於k 
26 }
27 
28 int main(){
29     while(~scanf("%d",&k)){
30         v.clear();
31         getchar();
32         getline(cin,s);
33         int j = 0;
34         for(int i = 0 ; i < s.size(); i++){
35             if(s[i] == ' ' || s[i] == '-'){
36                 v.push_back(j + 1);
37                 j = 0;
38             }else j++;
39         }
40         v.push_back(j);// 不要忘記末尾的字元串長度 
41         int l = s.size()/k, r = s.size();
42         while(r - l > 0){
43             int mid = (r + l)/ 2;
44             if(check(mid)){
45                 r = mid;
46             }else{
47                 l = mid + 1;
48             }
49         }
50         printf("%d\n",l);
51     }
52     return 0;
53 }
Magazine Ad

一個從很久以前就在做的夢。

 

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 定義: CDN 即內容分佈網路,(Content Delivery Netwrok) ,是構築在現有Internet上的一種先進的流量分配網路,其目的是通過在現有的Internet中增加一層新的網路架構,將網站的內容發佈到最接近用戶的網路“邊緣”,使用戶可以就近取得所需的內容,提高用戶訪問網站的相應 ...
  • 文件鎖 當多個進程或多個程式都想要修同一個文件的時候,如果不加控制,多進程或多程式將可能導致文件更新的丟失。 例如進程1和進程2都要寫入數據到a.txt中,進程1獲取到了文件句柄,進程2也獲取到了文件句柄,然後進程1寫入一段數據,進程2寫入一段數據,進程1關閉文件句柄,會將數據flush到文件中,進 ...
  • 從周三課開始總算輕鬆了點,下午能在宿舍研究點題目啥的打一打,還好,剛開學的課程還算跟得上,剛開學的這些課程也是複習以前學過的知識,下半學期也不敢太划水了,被各種人寄予厚望之後瑟瑟發抖,只能努力前行了~自己好多地方還做得不夠好,真的是要提升的方面太多了,加油吧~ 今日興趣新聞: 網易回應裁員:公司確實 ...
  • URI :Uniform Resource Identifier,統一資源標識符;URL:Uniform Resource Locator,統一資源定位符; URL是一種具體的URI,即URL可以用來標識一個資源,而且還指明瞭如何locate這個資源。URI是以一種抽象的,高層次概念定義統一資源標識 ...
  • 伺服器端的jsp文件運行: jsp文件 >java文件 >class文件 客戶端接受class文件:這就決定了 1.伺服器端要能生產正確的class文件,要不然,客戶端就不能正確運行 2.jsp文件的basePath的含義 3.伺服器的項目部署在webapps,生產的class文件在work,所以文 ...
  • 做一個代碼統計工具: 要求: 1.如果是文件,就直接統計文件行數,並列印結果 2.判斷是否是目錄,是就遍歷統計目錄下所有的文件文件統計規則: 1.開頭是#_*_或者#encoding的需要算作代碼統計 2.註釋#,'''或者"""判斷某一行開頭是"""或者'''就不計 3.空行不統計 4.統計當前文 ...
  • 第一次寫博客,今天就不寫什麼技術類的博客了。主要想寫點東西記錄一下每天都發生了什麼,不想讓自己每天都渾渾噩噩的過日子,就當是在寫日記吧。 今天是入職的第二天,和老闆兩個人在公司,希望老闆快點招到幾個人,讓我也快點能進入項目當中,進入狀態。 今天也把自己的網站的問題也算解決好了,(首頁無法訪問,808 ...
  • 報錯: You are trying to add a non-nullable field 'BookName' to BookInfo without a default; we can't do that (the database needs something to populate ex ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...