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?
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 ki, fi (1 ≤ ki ≤ 100, 1 ≤ fi ≤ 100), which means that the flat ki is on the fi-th floor. All values ki are distinct.
It is guaranteed that the given information is not self-contradictory.
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.
10 3 6 2 2 1 7 3
4
8 4 3 1 6 2 5 2 2 1
-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.
题意:
有一幢大楼,大楼每层有相同数量的房间,假设大楼为无数层,房间号从下至上逐个增加,第一个房间的号码为1,已知m个房间所在层数,求房间号为 n 的房间所在层数。若不能确定房间所在层数,则输出”-1”。
已知条件:
1.每层楼有一个或多个房间
2.每层楼的房间数量相同
3.房间号为1的房间只能在1楼
4.房间号从小到大递增
题解:
将所有已知的条件放入,暴力枚举一下每层房间数(1-100),找出可行的每层房间数,再算出房间n所在层数,看是否第n个房间总是在同一层。
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxn = 100+5;
int n,k,room[maxn],l[maxn],r[maxn],pos,ans = 0;
int main()
{
for(int i=0;i<maxn;i++) l[i] = maxn,r[i]=0;//l,r数组标记第i层已知房间号的左右值
l[1] = r[1] = 1;
scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++)
{
int flat,floor;
scanf("%d%d",&flat,&floor);
l[floor] = min(l[floor],flat);
r[floor] = max(r[floor],flat);
}
for(int i=1;i<=100;i++) for(int j=l[i];j<=r[i];j++) room[j] = i;
//标记已知房间所属
for(int i=1;i<=100&&ans!=-1;i++)//每层房间数
{
bool flag1 = true;
for(int ce=1;(ce-1)*i<=100;ce++)//层数
{
for(int j=1;j<=i;j++)
{
pos = (ce-1) * i + j;//房间号
if(pos>100) break;
if(room[pos]==0) continue;//无归属房间
if(room[pos]!=ce)//该每层房间数不符合条件
{
flag1 = false;break;
}
}
if(!flag1) break;
}
if(!flag1) continue;
int ans_ = n/i;
if(n%i) ans_++;
if(ans!=ans_&&ans==0) ans = ans_;
else if(ans!=ans_&&ans) ans = -1;
}
printf("%d",ans);
return 0;
}