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卡了好久,一直做不出来,往后看看,这道水题居然排在了它的后面。