SGU[127] Telephone directory

Description

描述

CIA has decided to create a special telephone directory for its agents. The first 2 pages of the directory contain the name of the directory and instructions for agents, telephone number records begin on the third page. Each record takes exactly one line and consists of 2 parts: the phone number and the location of the phone. The phone number is 4 digits long. Phone numbers cannot start with digits 0 and 8. Each page of the telephone directory can contain not more then K lines. Phone numbers should be sorted in increasing order. For the first phone number with a new first digit, the corresponding record should be on a new page of the phone directory. You are to write a program, that calculates the minimal number P pages in the directory. For this purpose, CIA gives you the list of numbers containing N records, but since the information is confidential, without the phones locations.

CIA想要为它的代理商们创建一个新的电话簿。电话簿的前2页包含了电话簿的名字以及给代理商的说明,电话记录从第3页开始。每条记录占一行,并且包含两部分:电话号码、电话归属地。电话号码是4位数字。电话号码不以0和8开头。电话簿的每页只能包含不超过K行。电话号码必须以递增的方式存放。对于新的首位电话号码必须重新开始新的一页。你需要写一个程序,来计算电话簿的最少页数P。为了达到这个目的,CIA将会给你提供一系列的N条记录,但是为了保密,并不会给出电话归属地。

Input

输入

The first line contains a natural number K (0 < K < 255) - the maximum number of lines that one page can contain. The second line contains a natural N (0 < N < 8000) - number of phone numbers supplied. Each of following N lines contains a number consisting of 4 digits - phone numbers in any order, and it is known, that numbers in this list cannot repeat.

第一行包含一个自然数K(0 < K < 255)——电话簿每页包含的最大行数。

第二行包含一个自然数N(0 < N < 8000)——电话的总数目。

接下来N行每行为一个4位数字——以任意顺序给出的不重复的电话号码。

Output

输出

First line should contain a natural number P - the number of pages in the telephone directory.

第一行包含一个自然数P——电话簿的最少页数。

Sample Input

样例输入

5
10
1234
5678
1345
1456
1678
1111
5555
6789
6666
5000

Sample Output

样例输出

5

Analysis

分析

这是一道水题。事实上,我们只需要电话号码的第一位,因此,读入数据以后直接除以1000即可,然后记录一下以各个数字开头的电话号码有几个。

如果不能被K整除,那么就需要Num / K + 1页,否则只需要Num / K页,这样处理的同时也满足了题目中每个新的首位电话号码另起一页的要求。

需要注意的是,最后要把题目中提到的电话簿开头2页加上去。

Solution

解决方案

#include <iostream>
#include <memory.h>

using namespace std;

const int MAX = 16;
const int HEX = 1000;

int K, N;
int pData[MAX];

int main()
{
	while(cin >> K >> N)
	{
		int nTmp, ans = 0;
		for(int i = 1; i <= N; i++)
		{
			cin >> nTmp;
			pData[nTmp / HEX]++;
		}
		for(int i = 1; i <= 9; i++)
		{ ans += pData[i] / K + (pData[i] % K != 0); }
		cout << ans + 2 << endl;
	}
	return 0;
}

被101卡了好久,一直做不出来,往后看看,这道水题居然排在了它的后面。

时间: 2024-08-10 06:19:02

SGU[127] Telephone directory的相关文章

SGU 127

此题略坑- -电话号码到第三页才开始记录- -所幸发现得早没有WA- - #include "stdio.h" #include "string.h" int main(){ int ans=0,i,k,n,tmp,cnt[10]; memset(cnt,0,sizeof(cnt)); scanf("%d",&k); scanf("%d",&n); for(i=1;i<=n;i++){ scanf(&qu

sgu100~199题解

老东西了..发上来吧.. Sgu题解系列  南开中学邹事成 100:A+B略 101:Domino 给n块多米诺骨牌,每张骨牌两端各有从1到6的一个数字,现在要把这些骨牌排成一列,使相邻的两块骨牌相对的面所写的数字一样. 可以把每一块多米诺骨牌想象成一条边,把面上写的数字抽象成点,比如一块骨牌正面写的1反面写的2就想象成连了一条从1到2的边,那么这就是求一条有重边的欧拉回路了,dfs一下即可. 102:Coprimes给定n求从1到n中与n互质的数的个数. 可以把n质因数分解后直接代入欧拉函数.

【字源大挪移—读书笔记】 第二部分:字根

[2] 字根:[2.1]表示[否定]的字根.[2.2]表示[方位]的字根.[2.3]表示[程度]的字根.[2.4]表示[状态]的字根.[2.5]表示[现象]的字根.[2.6]表示[身体]的字根.[2.7]表示[姿势]的字根.[2.8]表示[心,心里活动]的字根.[2.9]表示[行为动作]的字根.[2.10]表示[感官动作]的字根.[2.11]表示[感觉]的字根.[2.12]表示[生命]的字根.[2.13]表示[死亡]的字根.[2.14]表示[社会]的字根 [2.1]表示[否定]的字根 -neg-

文本文件从磁盘读取、写入

本文转自CSDN博客:http://blog.csdn.net/anchenyanyue/article/details/7666370 1 using System; 2 using System.Text; 3 using System.IO; 4 5 namespace XXXX.Common 6 { 7 /// <summary> 8 /// 文本文件从磁盘读取.写入 9 /// </summary> 10 public class FileHelper 11 { 12 1

待解:使用命令行实参的方法

1 package com.java7; 2 // A simple automated telephone directory. 3 class Phone { 4 public static void main(String[] args) { 5 String numbers[][] = { 6 { "Tom", "555-3322" }, 7 { "Mary", "555-8976" }, 8 { "Jon&

Dictionaries and tuples

Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-value pair. As you should expect from a dictionary, the items are in no particular order. Conversely, you can use a list of tuples to initialize a new di

[Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能

很久没有更新博客了,再不写点东西都烂了. 这次更新一个小内容,是两个插件的组合使用,实现头像上传功能. 业务需求: 头像上传功能,要对上传的文件进行剪切,且保证头像到服务器时必须是正方形的. 优化<input type="file">的显示样式,基础的样式实在太难看了. 上传的头像需要进行质量压缩跟大小裁剪,以减缓浏览器的压力. 成果预览: 使用到的技术插件 Jcrop:用于前端"裁剪"图片 bootstrap-fileinput:用于前端优化上传控件样

linux(10)

##telnet##1.限制某IP主机远程使用telnet[[email protected] ~]# cd /etc/postfix[[email protected] postfix]# vim access 在文件内写入: IP  REJECT (例:172.25.27.10  REJECT)[[email protected] postfix]# postmap access ##加密access文件[[email protected] postfix]# postconf -e "sm

ansible执行shell模块和command模块报错| FAILED | rc=127 &gt;&gt; /bin/sh: lsof: command not found和| rc=2 &gt;&gt; [Errno 2] No such file or directory

命令: ansible -i hosts_20 st  -m shell -a 'service zabbix_agentd star'  -K --become ansible -i hosts_20 st  -m shell -a 'lsof -i:10050'  -K --become 在shell模块报错:| FAILED | rc=127 >>/bin/sh: lsof: command not found 在command模块报错:| rc=2 >>[Errno 2]