CodeForces 858B Which floor?
来源:互联网 发布:efd.lab软件下载 编辑:程序博客网 时间:2024/05/18 19:43
题目:
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 36 22 17 3
4
8 43 16 25 22 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.
代码:
#include<stdio.h>#include<algorithm>using namespace std;int main(){ int m,n,i,x,y,mi=0,ma=105,k=0,ans,flag=1; scanf("%d%d",&m,&n); if(n==0) { if(m==1)printf("1\n"); //特判m=1,n=0 else printf("-1\n"); //特判m>1,n=0 } else { for(i=0;i<n;i++) //x为已知房间数 y为楼层 { //y-1楼最大值为x-1 y楼最小值为x int w; scanf("%d%d",&x,&y); //接下来找到最大值里的最小值,最小值里的最大值 if(y==1) //防止除0 if(x>mi)mi=x; else { w=(x-1)/(y-1); //上一层最大值 if(w<ma)ma=w; //找到最大值里的最小值存到ma w=0; //除不尽结果要+1 if(x%y!=0)w++; w+=x/y; //找到最小值里的最大值存到mi if(w>mi)mi=w; } } if(m%mi!=0)k++; //确定可能楼层是否唯一 k+=m/mi; ans=k; k=0; if(m%ma!=0)k++; k+=m/ma; if(ans!=k)flag=0; if(flag==1) printf("%d\n",ans); else printf("-1\n"); } return 0;}
- CodeForces 858B Which floor?
- B. Which floor?
- Codeforces Round #434 Which floor?
- Codeforces Round #434(div2)B-模拟&思维&读题-Which floor?
- Codeforces858B which floor?
- codeforces 858b
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- floor
- CodeForces 626B CodeForces 626B【暴力】
- CodeForces 841B (B) 博弈
- which
- which
- which
- codeforces 134B
- codeforces#98 b
- 游戏控制代码
- 手把手教你如何玩转虚拟定位
- JavaFile之获取获取目录下符合条件的文件集合
- MVP登录注册
- matlab 保存figure中的图像
- CodeForces 858B Which floor?
- 创建桌面notifications
- Lock、synchronized和ReadWriteLock的区别和联系
- 时间戳格式化
- Linux网络编程9 -- 简单总结Select改善多进程并发服务器
- RabbitMq--00
- javaweb三大框架和MVC设计模式
- java 字符串截取的几种方式
- Android快速实现扫一扫(Zxing),闪光灯,生成二维码图片,解析二维码(条码)