```
백준3252번 JANICA C++로 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 3252번 풀이
https://www.acmicpc.net/problem/3252
3252번: JANICA
The first line of the output file should contain the number of the skier winning the gold medal, second line the number of skier winning the silver medal, and the third line the number of skier winning the bronze medal.
www.acmicpc.net
백준 3252번 JANICA는
난이도 중급 등급의 문제로서
문제와는 별개로 JANICA의 의미는
Janica Kostelic (야니차 코스텔리치)이다
크로아티아의 유명한 스키선수이고 금메달 4개 은메달 2개를 획득하셨다고 한다.
야니차 코스텔리치 - 위키백과, 우리 모두의 백과사전
야니차 코스텔리치(Janica Kostelić, 1982년 1월 5일 자그레브에서 태어남)는 크로아티아의 알파인 스키 선수이다. 2002년 동계 올림픽에서 금메달 3개, 2001년과 2003년 월드컵 종합 우승으로 역대 가장
ko.wikipedia.org
그럼 다시 문제로 돌아와서
스키장에서 선수들이 참가했다고 가정했을때
테스트케이스 N 과 M은
1라운드에 참가한 선수와 그에 1대1 맵핑되는 기록의 수 N
2라운드에 참가한 선수와 그에 1대1 맵핑되는 기록의 수 M
이다
1라운드에서
첫번째 선수의 기록을 받고
그뒤로 1등하고 있는(가장 값이 작은) 선수의 값을 비교한 값을 받으며
2라운드에서는
1라운드 최고 기록선수가 가장 마지막에 출발하고
1라운드에서 2라운드 넘어온 선수중에 가장 점수가 낮은 선수가 제일 먼저 출발한다.
첫번째로 출발한 선수의 기록을 받고
그뒤로 1등하고 있는(가장 값이 작은) 선수의 값을 비교한 값을 받으며
최종적으로는
금은동 메달을 받는 선수의 번호를 출력하면 되는 문제이다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
클래스를 사용하였으며 멤버 변수와 구조체를 다음과 같이 사용하였다
int firstRoundSkiersNumber;
int secondRoundSkiersNumber;
struct Record
{
int no;
double time;
};
std::vector<Record> FirstRound;
std::vector<Record> SecondRound;
먼저 유저로부터 테스트 케이스 firstRoundSkiersNumber, secondRoundSkiersNumber
그리고 알파인 스키 결과 값을 입력받는다.
void getInputFromUser()
{
/* 입력 속도를 줄여서 시간제한을 통과하기 위해서 stringstream 사용*/
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
ss >> firstRoundSkiersNumber;
ss >> secondRoundSkiersNumber;
for (int i = 0; i < firstRoundSkiersNumber; i++)
{
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
double tempTime;
ss >> tempTime;
Record record = {i, tempTime };
FirstRound.push_back(record);
}
for (int i = 0; i < secondRoundSkiersNumber; i++)
{
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
double tempTime;
ss >> tempTime;
Record record = { i, tempTime };
SecondRound.push_back(record);
}
}
그뒤 첫번째 라운드 데이터를 가공해서
두번째 라운드의 번호 순서를 구한다.
Record 라는 커스텀 구조체 타입을 원소로 하는 벡터를 사용하였으며
vector를 stl에 있는 sort를 이용해서 쉽게 sorting 하였다.
// 기록 가공
double leadingTime = FirstRound[0].time;
for (int i = 1; i < firstRoundSkiersNumber; i++)
{
FirstRound[i].time = leadingTime + FirstRound[i].time;
leadingTime = std::min(leadingTime, FirstRound[i].time);
}
// 두번째 라운드에 첫번째 라운드 번호 매칭
std::sort(FirstRound.begin(), FirstRound.end(), [](Record const& lhs, Record const& rhs) {
bool retBool = true;
if (lhs.time > rhs.time)
{
retBool = false;
}
return retBool;
});
for (int i = 0; i < secondRoundSkiersNumber; i++)
{
const int indexOfFirstRound = secondRoundSkiersNumber - i - 1;
SecondRound[i].no = FirstRound[indexOfFirstRound].no;
}
두번째 라운드를 가지고
결과를 구하고 그 결과 값을 가지고
금은동을 출력하면 된다.
// 값입력
leadingTime = SecondRound[0].time;
for (int i = 1; i < secondRoundSkiersNumber; i++)
{
SecondRound[i].time = SecondRound[i].time + leadingTime;
leadingTime = std::min(leadingTime, SecondRound[i].time);
}
std::sort(SecondRound.begin(), SecondRound.end(), [](Record const& lhs, Record const& rhs) {
bool retBool = true;
if (lhs.time > rhs.time)
{
retBool = false;
}
return retBool;
});
for (int i = 0; i < 3; i++)
{
std::cout << SecondRound[i].no + 1 << "\n";
}
전체코드는 아래와 같다
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include<numeric>
// https://www.acmicpc.net/problem/3252
class CJANICA
{
private:
int firstRoundSkiersNumber;
int secondRoundSkiersNumber;
struct Record
{
int no;
double time;
};
std::vector<Record> FirstRound;
std::vector<Record> SecondRound;
public:
CJANICA()
{
}
~CJANICA()
{
}
void getInputFromUser()
{
/* 입력 속도를 줄여서 시간제한을 통과하기 위해서 stringstream 사용*/
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
ss >> firstRoundSkiersNumber;
ss >> secondRoundSkiersNumber;
for (int i = 0; i < firstRoundSkiersNumber; i++)
{
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
double tempTime;
ss >> tempTime;
Record record = {i, tempTime };
FirstRound.push_back(record);
}
for (int i = 0; i < secondRoundSkiersNumber; i++)
{
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
double tempTime;
ss >> tempTime;
Record record = { i, tempTime };
SecondRound.push_back(record);
}
}
bool roundSorter()
{
}
void printWinners()
{
// 기록 가공
double leadingTime = FirstRound[0].time;
for (int i = 1; i < firstRoundSkiersNumber; i++)
{
FirstRound[i].time = leadingTime + FirstRound[i].time;
leadingTime = std::min(leadingTime, FirstRound[i].time);
}
// 두번째 라운드에 첫번째 라운드 번호 매칭
std::sort(FirstRound.begin(), FirstRound.end(), [](Record const& lhs, Record const& rhs) {
bool retBool = true;
if (lhs.time > rhs.time)
{
retBool = false;
}
return retBool;
});
for (int i = 0; i < secondRoundSkiersNumber; i++)
{
const int indexOfFirstRound = secondRoundSkiersNumber - i - 1;
SecondRound[i].no = FirstRound[indexOfFirstRound].no;
}
// 값입력
leadingTime = SecondRound[0].time;
for (int i = 1; i < secondRoundSkiersNumber; i++)
{
SecondRound[i].time = SecondRound[i].time + leadingTime;
leadingTime = std::min(leadingTime, SecondRound[i].time);
}
std::sort(SecondRound.begin(), SecondRound.end(), [](Record const& lhs, Record const& rhs) {
bool retBool = true;
if (lhs.time > rhs.time)
{
retBool = false;
}
return retBool;
});
for (int i = 0; i < 3; i++)
{
std::cout << SecondRound[i].no + 1 << "\n";
}
}
void printTest()
{
std::cout << "first Round" << "\n";
for (auto& element : FirstRound) {
std::cout << element.no +1 << " " << element.time << "\n";
}
std::cout << "second Round" << "\n";
for (auto& element : SecondRound) {
std::cout << element.no +1 << " " << element.time << "\n";
}
}
};
int main()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
CJANICA* cJANICA = new CJANICA();
cJANICA->getInputFromUser();
cJANICA->printWinners();
//cJANICA->printTest();
}
입력
5 4
27.29
-1.02
+1.83
-0.43
+0.03
56.98
+1.83
-0.43
+0.03
결과
5
4
1
인증

읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩하시길 바랍니다 ~ :)
참조 및 인용
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
'C++ > C++ 알고리즘' 카테고리의 다른 글
| 백준14645번 와이버스 부릉부릉 C++로 구현해보기 (0) | 2021.08.30 |
|---|---|
| 백준 21964번 선린인터넷고등학교 교가(마지막5글자 구하기) C++로 구현해보기 (0) | 2021.08.30 |
| 백준17496번 StarFruit C++로 구현해보기 (0) | 2021.08.29 |
| 백준1987번 알파벳 C++로 구현해보기 (2) | 2021.08.29 |
| 백준1764번 듣보잡 C++로 구현해보기 (0) | 2021.08.29 |