C++/C++ 알고리즘

백준 11971번 속도위반 C++ 구현해보기

kimc 2021. 10. 31. 11:44

```

백준 11971번 속도위반 C++ 구현해보기

```

 

이번 글을 통해 배워갈 내용

  1.  백준 11971번 풀이

https://www.acmicpc.net/problem/11971

 

11971번: 속도 위반

예를 들어 도로가 3구역 [(40km, 75km/h), (50km, 35km/h), (10km, 45km/h)] 이고. 연정이가 달린 3구역 [(40km, 76km/h), (20km, 30km/h), (40km, 40km/h)] 이라면, 처음 0 ~ 40km 영역에서 1km/h 만큼 위반했으나 60km ~ 90km 사이에

www.acmicpc.net

 

 

 

백준 11971번 속도위반은 

난이도 실버 등급의 문제로서

 

100Km의 도로가 주어지고

n 개의 도로 구간에 각 길이와 속도제한

m개의 도로 구간에 달린 속도와 길이가 주어질 때

 

위반한 최대 속도 차를 구하면 되는 문제입니다.

 

 

예를 들어

2개의 도로구간 

30km 길이에 30km/h 제한

70km 길이에 40km/h 제한

이 주어지고

 

도로를

29km 길이에 30km/h으로 달리고

71km 길이에 40km/h으로 달린다면

 

29km 까지는 제한 이내이고 (30 - 30)

30km에서 제한을 40-30 = 10을 초과 후

나머지 구간은 40 -40 제한 미만으로 달렸기 때문에

 

10이 정답입니다.

 

 


30분 정도 위에 링크를 방문하셔서 풀어보시고

안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.


저는 

100 km라고 가정한 배열 안에다가 모든 구간의 속도 제한을 넣어주고

입력받은 운전속도 구간을 그대로 비교해서 풀었습니다.

 

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <regex>

int main()
{
	// 입력 최적화
	std::cin.tie(NULL);
	std::ios::sync_with_stdio(false);

	std::string inputStr;
	std::getline(std::cin, inputStr); 
	std::stringstream ss(inputStr);

	size_t speedLimitSectionCnt;
	ss >> speedLimitSectionCnt;

	size_t bessieSectionCnt;
	ss >> bessieSectionCnt;

	ss.seekg(0);

	int32_t speedLimitSectionArr[100] = {0};
	int32_t previousSectionEnd = 0;

	for (size_t i = 0; i < speedLimitSectionCnt; i++)
	{
		std::getline(std::cin, inputStr);
		ss.str(inputStr);

		int32_t sectionLength;
		ss >> sectionLength;
		int32_t sectionLimit;
		ss >> sectionLimit;
		ss.seekg(0);

		for (size_t i = 0; i < sectionLength; i++)
		{
			speedLimitSectionArr[(i + previousSectionEnd)] = sectionLimit;
		}

		previousSectionEnd += sectionLength;

	}

	int32_t maxOffLimit = 0;
	previousSectionEnd = 0;
	for (size_t i = 0; i < bessieSectionCnt; i++)
	{
		std::getline(std::cin, inputStr);
		ss.str(inputStr);

		int32_t sectionLength;
		ss >> sectionLength;
		int32_t sectionLimit;
		ss >> sectionLimit;
		ss.seekg(0);

		for (size_t i = 0; i < sectionLength; i++)
		{
			int32_t curOffLimit = sectionLimit - speedLimitSectionArr[(i + previousSectionEnd)];
			maxOffLimit = std::max(curOffLimit, maxOffLimit);
		}
		previousSectionEnd += sectionLength;

	}

	std::cout << maxOffLimit;
}

 

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 

참조 및 인용

C++ Primer

Introduction to Algorithms


https://codemasterkimc.tistory.com/35

 

C++ 이론을 배울수 있는 곳 정리

개요  C++을 배우는 책, 강의, 블로그, 링크 등을 공유합니다. (링크 및 간략한 설명을 하였으나 만약 원작자가 링크를 거는것을 원치 않을 경우 연락주시기 바랍니다.) 서적 https://www.amazon.com/Prime

codemasterkimc.tistory.com

 

https://codemasterkimc.tistory.com/50

 

300년차 개발자의 좋은 코드 5계명 (Clean Code)

이번 글을 통해 배워갈 내용  좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋

codemasterkimc.tistory.com

 


 

728x90