Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
B. Which floor?
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.
题目链接:http://codeforces.com/contest/861/problem/B
分析:直接看代码吧,代码给出了详细注释!
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=;
int n,m,cur=,idx=;;
int should[N+];
inline void ok(int per)
{
int now=,cnt=;//now是当前的楼层,cnt是当前楼层的公寓数目
int temp=;
for(int dd=;dd<=;dd++)//枚举第dd间房子在哪里
{
cnt++;//当前楼层的公寓数目递增。
if(cnt>per)//如果公寓数目大于每层的楼层数
{
cnt=;//进入下一层
now++;//楼层个数递增
}
if(should[dd]!=&&should[dd]!=now)
return;
if(dd == n)
temp=now;
//如果dd公寓不应该在第now层,就结束
}
//是一个合法的分配
if(cur==)//如果之前没有找到过合法的。
{
idx=temp;//第n个房子,它就在第now层
cur=;//找到的解数目为1
}
else//数目大于0了
{
if(cur==)//如果只有一个解的话
{
if(idx==temp)//如果第n间房子的层数和当前一样,退出
return;
}
cur++;//否则,答案递增。
}
}
int main(void)
{
cin>>n>>m;
for(int i=;i<=m;i++)
{
int x,y;
cin>>x>>y;
should[x]=y;//x号公寓房子啊第y层
}
for(int i=;i<=;i++)//枚举每层有i间公寓
ok(i);
if(cur>||cur==)//如果解的个数太多,或没解。
cout<<-;
else
cout<<idx;
return ;
}
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】的更多相关文章
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】
C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)
A. k-rounding 题目意思:给两个数n和m,现在让你输出一个数ans,ans是n倍数且末尾要有m个0; 题目思路:我们知道一个数末尾0的个数和其质因数中2的数量和5的数量的最小值有关系,所以 ...
- 【模拟】 Codeforces Round #434 (Div. 1, based on Technocup 2018 Elimination Round 1) C. Tests Renumeration
题意:有一堆数据,某些是样例数据(假设X个),某些是大数据(假设Y个),但这些数据文件的命名非常混乱.要你给它们一个一个地重命名,保证任意时刻没有重名文件的前提之下,使得样例数据命名为1~X,大数据命 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries
地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...
- Codeforces Round #440 (Div. 1, based on Technocup 2018 Elimination Round 2) C - Points, Lines and Ready-made Titles
C - Points, Lines and Ready-made Titles 把行列看成是图上的点, 一个点(x, y)就相当于x行 向 y列建立一条边, 我们能得出如果一个联通块是一棵树方案数是2 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) C. Maximum splitting
地址: 题目: C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input ...
- ACM-ICPC (10/15) Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers You are given two lists of non-zero digits. Let's call an integer pret ...
随机推荐
- 2. whoami,常用包,调优selinux,七种启动模式,系统开机服务
1 whoami 查看当前登录用户 useradd zhang 增加用户 passwd zhang su - zhang 切换用户 e ...
- Kotlin——最详细的数据类、密封类详解
在前面几个章节章节中,详细的讲解了Koltin中的接口类(Interface).枚举类(Enmu),还不甚了解的可以查看我的上一篇文章Kotlin--接口类.枚举类详解.当然,在Koltin中,除了接 ...
- rmdir 命令详解
rmdir 作用: 用来删除空目录, 当目录不再被使用时, 或者磁盘空间已达到使用限定值, 就需要删除失去价值的目录. 利用rmdir 命令可以从一个目录中删除一个或多个空的子目录. 该命令从一个 ...
- [解决方案]WebAPI+SwaggerUI部署服务器后,访问一直报错的问题
项目的背景:制作一批接口用来给前台app或者网站提供服务,因为WebApi是最近几年来比较流行和新颖的开发接口的方式,而且又属于轻型应用,所以选用它 部署的过程:建立了WebAPI项目并使用Swagg ...
- QT中几个函数的使用方法
一.把字符串转换成整形demo1:QString str = "FF";bool ok;int hex = str.toInt(&ok, 16); // hex == 25 ...
- Linux(CentOS6.5)下修改Nginx初始化配置
本文地址http://comexchan.cnblogs.com/,作者Comex Chan,尊重知识产权,转载请注明出处,谢谢! 首先备份相关文件: cp /comexHome/nginx/conf ...
- 搭建redis cluster
1 下载 redis安装包 tar zxvf redis-3.0.2.tar.gz cd redis-3.0.2/ make make install 2安装ruby sudo apt-get in ...
- c#常用快捷键
VS快捷键大全(总结了一些记忆的口诀) 原文转载至 https://www.cnblogs.com/liyunhua/p/4537054.html#top 谢谢大牛的分享! 相信.Net ...
- libcurl的使用
http://blog.csdn.net/ixiaochouyu/article/details/47998267
- vue-router源码学习(一)
因为v3.01版本中的 /src代码使用TypeScript进行书写,我这里仅仅用作模块学习, 具体学习的还是 /dist/vue-router.js 代码. (一)基本使用方式 JS代码 // ...