Codeforces 문제 해결: 467A - 조지와 숙박 시설

오늘 우리는 Codeforces 문제 467A - George and Accommodation을 해결하려고 합니다. 괜찮은! 시작하겠습니다.

목차


  • The Question
  • My Analysis with Sample Examples
  • Code and Complexity
  • Links and References

  • 질문

    문제 설명



    George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory.

    George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≤ qi). Your task is to count how many rooms has free place for both George and Alex.



    입력



    The first line contains a single integer n (1 ≤ n ≤ 100) — the number of rooms.

    The i-th of the next n lines contains two integers pi and qi (0 ≤ pi ≤ qi ≤ 100) — the number of people who already live in the i-th room and the room's capacity.



    산출



    Print a single integer — the number of rooms where George and Alex can move in.



    샘플 예제를 사용한 내 분석

    Now, as per the question we need to check if a room, say ri has enough space left for 2 people, i.e. George and Alex. So putting this in the form of a condition, we get (assuming we start counting the rooms from 1):

    qi - pi ≥ 2, ∀ i ∈ [1, n]
    

    Now let's try out our logic on the examples given in the problem:

    예-1



    입력



    3
    1 1
    2 2
    3 3



    여기 기숙사 방 하나하나가 꽉 차서 우리 두 친구가 설 자리가 없는게 확연히 보이네요 😢. 따라서 답은 0이 됩니다.

    산출



    0



    예-2



    입력



    3
    1 10
    0 10
    10 10



    여기서 우리는 수용 인원이 10이고 수용 인원이 1인 첫 번째 방과 수용 인원이 10이고 수용 인원이 0인 두 번째 방에 2명의 친구를 위한 충분한 공간이 있음을 알 수 있습니다 😊. 엄청난! 그래서 우리는 기숙사에 있는 2개의 방에 대한 답을 얻었습니다.

    산출



    2



    코드와 복잡성

    Alright, so now we know mathematically what we need to check for each room in the dormitory and we have verified our logic over the examples as well. Let's now translate this logic into code. For this I'll be using modern C++ (C++11 and beyond), so that it's easier to get the big picture logic which is implemented in the code.

    Another interesting reason to use modern C++ is that some of its features make the code more readable and similar looking to Python code (many people consider this a plus).

    /**
     * @file 467A-George_and_Accommodation.cpp
     * @author Rishit Chaudhary (@rishitc)
     * @version 1.0
     * @date 2021-06-19
     * 
     * @copyright Copyright (c) 2021
     * 
     */
    
    #include <iostream>
    
    int main()
    {
        int n;
        int p, q;
        int count{0};
        const int Space_for_George_and_Alex{2};
    
        std::cin >> n;
    
        while (n--)
        {
            std::cin >> p >> q;
    
            if (q - p >= Space_for_George_and_Alex)
            {
                ++count;
            }
        }
    
        std::cout << count << "\n";
    }
    
    

    복잡성




    복잡성 유형
    대답


    시간 복잡도
    에)

    공간 복잡성
    오(1)

    난이도(주관적 가치)

    쉬운


    링크 및 참조

  • Link to the problem: https://codeforces.com/problemset/problem/467/A
  • 내 제출 링크: https://codeforces.com/contest/467/submission/120009560
  • 내 GitHub 리포지토리의 솔루션 링크: https://github.com/rishitc/Codeforces-Codes/blob/main/467A-George_and_Accommodation.cpp
  • 좋은 웹페이지 즐겨찾기