education round 20 d Magazine Ad

本文解析了CodeForces竞赛中一道关于字符串分割的问题。通过分析题目要求,给出了使用二分查找来寻找最优字符串宽度的方法,并附带完整代码实现。

D. Magazine Ad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
4
garage for sa-le
Output
Copy
7
Input
Copy
4
Edu-ca-tion-al Ro-unds are so fun
Output
Copy
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,一列字符串。这个字符串中,空格和连字符-都是标志一个单词的结束。然后让你将这个字符串分成最多k块。使得长度最长的内个块的长度尽可能的小。分块时,只能在空格处和连字符处断开.

思路:

一个典型的最大值最小值问题。设长度最长的单词长度为maxn,字符串长度为num.则答案必定在maxn与num之间。那么就二分答案。每次判断mid,此时的mid就是块划分的依据,用sum累加,当加上下一个单词大于mid时,就产生了一个块.最后判断这个块是否成立,成立说明这个划分依据大了,那就r=mid-1,继续判断.最后输出l,就是最佳划分

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N =1e6+5;
char a[N];
int loc[N];
int cnt;
bool judge(int mid,int k)
{
    int cc=0;
    ll sum=0;
    for(int i=0;i<cnt;i++)
    {
        sum+=loc[i];
        if(sum>mid)//每当超过mid,那就产生一个块,块数量cc加1
        {
            cc++;
            sum=loc[i];
            if(cc>=k)//要注意最后的cc如果等于k,那就代表着此时有cc+1个划分快,不满足条件
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    int k;
    cin>>k;
    getchar();
    gets(a);
    int num=strlen(a);
    //cout<<num<<endl;
    int maxn=1;
    cnt=0;
    int sign=-1;
    for(int i=0;i<num;i++)
    {
        if(a[i]==' '||a[i]=='-')
        {
           loc[cnt++]=i-sign;//先预处理出每个单词的长度,注意这个长度是带上空格或者连字符的
           maxn=max(maxn,i-sign);
           sign=i;
        }
    }
    loc[cnt++]=num-sign-1;
    maxn=max(maxn,num-sign-1);
    int l=maxn,r=num;//答案必定在maxn与num之间。
    while(l<=r)//开始二分答案
    {
       //cout<<l<<' '<<r<<endl;
        int mid=(l+r)>>1;
        if(judge(mid,k))
            r=mid-1;
        else
            l=mid+1;
    }
    cout<<l<<endl;
    return 0;
}


先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱动电路,实现LED的调光功能以及DIY调色方案。 本文重点阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和强抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值