Codeforces 문제 해결: 467A - 조지와 숙박 시설
목차
질문
문제 설명
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)
난이도(주관적 가치)
쉬운
링크 및 참조
Reference
이 문제에 관하여(Codeforces 문제 해결: 467A - 조지와 숙박 시설), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/rishitc/solving-codeforces-problem-467a-george-and-accommodation-39jb텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)