Problem - 3777 Page Count

Problem - 3777  Page Count

 

Problem Description

When you execute a word processor's print command, you are normally prompted to specify the pages you want printed. You might, for example, enter:

 

10-15,25-28,8-4,13-20,9,8-8

The expression you enter is a list of print ranges, separated by commas.

 

Each print range is either a single positive integer, or two positive integers separated by a hyphen. In the latter case we call the first integer low and the second one high. A print range for which low > high is simply ignored. A print range that specifies page numbers exceeding the number of pages is processed so that only the pages available in the document are printed. Pages are numbered starting from 1.

 

Some of the print ranges may overlap. Pages which are common to two or more print ranges will be printed only once. (In the example given, pages 13, 14 and 15 are common to two print ranges.)

Input

The input will contain data for a number of problem instances. For each problem instance there will be two lines of input. The first line will contain a single positive integer: the number of pages in the document. The second line will contain a list of print ranges, as defined by the rules stated above. End of input will be indicated by 0 for the number of pages. The number of pages in any book is at most 1000. The list of print ranges will be not be longer than 1000 characters.

Output

For each problem instance, the output will be a single number, displayed at the beginning of a new line. It will be the number of pages printed by the print command.

Sample Input

30

10-15,25-28,8-4,13-20,9,8-8

19

10-15,25-28,8-4,13-20,9,8-8

0

Sample Output

17

12

 

 

 

这道题可以用两种方法  当测试数据比较弱是可以用暴力的方法  开1000的bool数组  

每读入一段就把相应的页面设置成true  最后扫描一遍就可得到总数  这种做法时间复杂度为 n方

 

 

下面介绍nlog(n)的做法 

首先读入时按照要求做预处理,然后按照区间起点进行排序,

排序后从最小的起点开始如下操作:

 

1,记下最下段(a,b)的起始点a,终点b。

 

2,向下找段(c,d)

当 a < c < b 时维护起始点为a,终点为b=max(b,d);然后重复操作 2

当 c > b 时可确定(a,b)属于打印段 sum加上此段长度 ;然后从(c,d)开始重复操作 1

直到结束  得到sum即为结果

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值