Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which
B. Which floor?
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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?
Input
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.
Output
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.
Examples
Input
10 3
6 2
2 1
7 3
Output
4
Input
8 4
3 1
6 2
5 2
2 1
Output
-1
Note
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.
题目链接:http://codeforces.com/contest/861/problem/B
分析:直接看代码吧,代码给出了详细注释!
下面给出AC代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 const int N=200;
4 int n,m,cur=0,idx=0;;
5 int should[N+10];
6 inline void ok(int per)
7 {
8 int now=1,cnt=0;//now是当前的楼层,cnt是当前楼层的公寓数目
9 int temp=1;
10 for(int dd=1;dd<=100;dd++)//枚举第dd间房子在哪里
11 {
12 cnt++;//当前楼层的公寓数目递增。
13 if(cnt>per)//如果公寓数目大于每层的楼层数
14 {
15 cnt=1;//进入下一层
16 now++;//楼层个数递增
17 }
18 if(should[dd]!=0&&should[dd]!=now)
19 return;
20 if(dd == n)
21 temp=now;
22 //如果dd公寓不应该在第now层,就结束
23 }
24 //是一个合法的分配
25 if(cur==0)//如果之前没有找到过合法的。
26 {
27 idx=temp;//第n个房子,它就在第now层
28 cur=1;//找到的解数目为1
29 }
30 else//数目大于0了
31 {
32 if(cur==1)//如果只有一个解的话
33 {
34 if(idx==temp)//如果第n间房子的层数和当前一样,退出
35 return;
36 }
37 cur++;//否则,答案递增。
38 }
39 }
40 int main(void)
41 {
42 cin>>n>>m;
43 for(int i=1;i<=m;i++)
44 {
45 int x,y;
46 cin>>x>>y;
47 should[x]=y;//x号公寓房子啊第y层
48 }
49 for(int i=1;i<=102;i++)//枚举每层有i间公寓
50 ok(i);
51 if(cur>1||cur==0)//如果解的个数太多,或没解。
52 cout<<-1;
53 else
54 cout<<idx;
55 return 0;
56 }
- [喵咪开源软件推荐(4)]Liunx跑分神器-unixbench
- 正则化贪心森林(RGF)的入门简介,含案例研究
- 数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 路由(二)之分库分表路由
- [喵咪开源软件推荐(3)]全球IP库-GeoLite2-City
- [喵咪MQ(3)]RabbitMQ集群安装配置
- 动态实现指定图片半透明及鼠标事件
- JVM杂谈之JIT
- 熔断器 Hystrix 源码解析 —— 执行结果缓存
- [喵咪MQ(2)]RabbitMQ单机模式使用
- [喵咪MQ(1)]RabbitMQ简单介绍准备工作
- Dubbo 源码解析 —— Directory
- [喵咪大数据]Hbase搭建和基本使用
- CSS:模拟Windows窗口及DIV居中
- [喵咪大数据]Hive2搭建和基本操作
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- ceph分布式存储-集群客户端连接
- rbd image找到对应osd位置
- CephFS 文件布局性能测试对比
- MySQL 8.0 新特性:极速“备份恢复工具”,Clone 插件
- CephFS 介绍及使用经验分享
- CephFS源码分析
- linux cache介绍及控制
- Lustre文件系统安装
- Lustre参数调整
- samba源码安装及调试
- 设计模式实战-状态模式,让代码更清爽简洁
- 改善深层神经网络——吴恩达深度学习课程笔记(二)
- Spring Boot 监听 Redis Key 失效事件实现定时任务,超时订单自动关闭绝佳实现
- TensorFlow的核心概念:张量和计算图
- 四连问:API 接口应该如何设计?如何保证安全?如何签名?如何防重?