zoukankan      html  css  js  c++  java
  • B. Which floor?

    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.

     题意:给你两个数,一个表示房号,另一个表示有n 组数据,

    接下来N组数据分为x,y,分别表示房号和所在的楼层,你要根据他给出的数据确定是否能确定第一个所给的房号的楼层,

    注意1楼永远在一楼。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #define N 102
     6 using namespace std;
     7 int n,m;
     8 struct Node{
     9   int x,y;
    10 }k[N];
    11 int ans,cnt;
    12 int mi=101,mx=0;
    13 int main(){
    14   cin>>n>>m;
    15   bool flag=true;
    16   for(int i=0;i<m;i++){
    17     cin>>k[i].x>>k[i].y;
    18   }
    19   if(n==1&&m==0){
    20     cout<<1<<endl;
    21     return 0;
    22   }
    23   for(int i=1;i<=100;i++){
    24     bool prime=true;
    25     for(int j=0;j<m;j++){
    26       int t=k[j].x%i==0?k[j].x/i:k[j].x/i+1;
    27       if(t!=k[j].y){
    28         prime=false;
    29         break;
    30       }
    31     }
    32     if(prime){
    33       cnt++;
    34       int z=n%i==0?n/i:n/i+1;
    35       if(z==ans){
    36         cnt--;
    37       }
    38       while(flag){
    39         ans=z;
    40         flag=false;
    41       }
    42     }
    43   }
    44   if(cnt==1){
    45     cout<<ans<<endl;
    46   }else{
    47     cout<<-1<<endl;
    48   }
    49   return 0;
    50 }
  • 相关阅读:
    WikiPedia技术架构学习笔记
    MySQL 架构设计篇 (十二) 可扩展设计的基本原则
    php前端控制器二
    php前端控制器三
    构建可扩展的WEB站点读书笔记
    发布脚本开发框架代码
    改良dbgrideh的文字过滤
    cxgrid在当前View插入记录
    生成不重复单据编号
    cxgrid按条件计算合计值
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7648109.html
Copyright © 2011-2022 走看看