• CodeForces


    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?


    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.


    Example
    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.

    题意:给m个房间的具体位置,求出第n个房间在第几层楼。

    思路:每层楼最多有100个房间,遍历一遍,找到一个符合的数,如果找到两个不想等的数,就无法确定一层楼有几个房间。

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    using namespace std;
    int a[1000];
    int b[1000];
    int main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
           cin>>a[i]>>b[i];
        }
        int ans=0,x;
        for(int i=1;i<=100;i++)
        {
            int flag=0;
            for(int j=1;j<=m;j++)
            {
                 x=(a[j]+i-1)/i;
                 //cout<<x<<endl;
                if(x!=b[j])
                    flag=1;
            }
            if(!flag)
            {
               int w=(n+i-1)/i;
               if(!ans)
                ans=w;
               else if(ans!=w)
                ans=-1;
            }
        }
        if(!ans)ans=-1;
        cout<<ans<<endl;
    
    }




  • 相关阅读:
    ReentrantLock和synchronized区别和联系?
    从上向下打印二叉树
    字符串的全排列
    二叉树中和为某一值得路径 java实现
    三大框架面试题
    SQL语句总结
    [terry笔记]Oracle10g/11g安装-redhat5.5
    [terry笔记]Oracle会话追踪(二):TKPROF
    [terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046
    [terry笔记]IMPDP报错ORA-39083 Object type TYPE failed to create ORA-02304
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053315.html
Copyright © 2020-2023  润新知