CF858B.Which floor?
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how many flats are on each floor, but he remembers that the flats are numbered from 1 from lower to upper floors. That is, the first several flats are on the first floor, the next several flats are on the second and so on. Polycarp don't remember the total number of flats in the building, so you can consider the building to be infinitely high (i.e. there are infinitely many floors). Note that the floors are numbered from 1.
Polycarp remembers on which floors several flats are located. It is guaranteed that this information is not self-contradictory. It means that there exists a building with equal number of flats on each floor so that the flats from Polycarp's memory have the floors Polycarp remembers.
Given this information, is it possible to restore the exact floor for flat n?
在波利卡普居住的大楼中,每层楼的公寓数量相同。不幸的是,波利卡普不记得每层楼有多少间公寓,但他记得公寓编号从 1 开始,由下至上依次编号。也就是说,最开始的若干间公寓位于第一层,接下来的若干间公寓位于第二层,依此类推。波利卡普不记得大楼中公寓的总数,因此你可以将大楼视为无限高(即楼层数量无限)。注意:楼层编号从 1 开始。
波利卡普记得若干间公寓所在的楼层。题目保证该信息自洽,即:存在一个每层公寓数相同的建筑,使得波利卡普所记忆的这些公寓恰好位于他所记住的那些楼层上。
给定这些信息,是否能够唯一确定公寓 n 所在的楼层?
输入格式
The first line contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 100), where n is the number of the flat you need to restore floor for, and m is the number of flats in Polycarp's memory.
m lines follow, describing the Polycarp's memory: each of these lines contains a pair of integers k__i, f__i (1 ≤ k__i ≤ 100, 1 ≤ f__i ≤ 100), which means that the flat k__i is on the f__i-th floor. All values k__i are distinct.
It is guaranteed that the given information is not self-contradictory.
第一行包含两个整数 n 和 m(1≤n≤100,0≤m≤100),其中 n 是你需要为其恢复楼层号的公寓编号,m 是 Polycarp 记忆中的公寓数量。
接下来 m 行描述 Polycarp 的记忆:每行包含一对整数 ki,fi(1≤ki≤100,1≤fi≤100),表示公寓 ki 位于第 fi 层。所有 ki 的值互不相同。
保证所给信息不存在自相矛盾之处。
输出格式
Print the number of the floor in which the n-th flat is located, if it is possible to determine it in a unique way. Print -1 if it is not possible to uniquely restore this floor.
输出第 n 套公寓所在的楼层号(如果能够唯一确定的话)。如果无法唯一确定该楼层,则输出 −1。
输入输出样例
输入#1
10 3 6 2 2 1 7 3
输出#1
4
输入#2
8 4 3 1 6 2 5 2 2 1
输出#2
-1
说明/提示
In the first example the 6-th flat is on the 2-nd floor, while the 7-th flat is on the 3-rd, so, the 6-th flat is the last on its floor and there are 3 flats on each floor. Thus, the 10-th flat is on the 4-th floor.
In the second example there can be 3 or 4 flats on each floor, so we can't restore the floor for the 8-th flat.
在第一个例子中,第 6 套公寓位于第 2 层,而第 7 套公寓位于第 3 层,因此第 6 套公寓是其所在楼层的最后一套,且每层有 3 套公寓。于是,第 10 套公寓位于第 4 层。
在第二个例子中,每层可能有 3 套或 4 套公寓,因此我们无法确定第 8 套公寓所在的楼层。
输入解题思路,AI测评打分。不知道怎么写?