티스토리 뷰
출처 : https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
vector<int> climbingLeaderboard(vector<int> scores, vector<int> alice) {
int s_size = scores.size(); int a_size = alice.size();
vector <int> ans(a_size), tmp(s_size); int n = 1; tmp[0] = n;
for (int i = 1; i < s_size; i++) {
if (scores[i] == scores[i - 1]) tmp[i] = n;
else tmp[i] = ++n;
}
int it = s_size - 1;
for (int j = 0; j < a_size; j++) {
for (int i = it; i >=0; i--) {
if (scores[i] < alice[j]) {
if (i == 0) {
ans[j] = 1; it = 1; break;
}
continue;
}
else if (scores[i]>alice[j]) {
ans[j] = tmp[i] + 1; it = i;break;
}
else {
ans[j] = tmp[i]; it = i; break;
}
}
}
return ans;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int scores_count;
cin >> scores_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string scores_temp_temp;
getline(cin, scores_temp_temp);
vector<string> scores_temp = split_string(scores_temp_temp);
vector<int> scores(scores_count);
for (int i = 0; i < scores_count; i++) {
int scores_item = stoi(scores_temp[i]);
scores[i] = scores_item;
}
int alice_count;
cin >> alice_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string alice_temp_temp;
getline(cin, alice_temp_temp);
vector<string> alice_temp = split_string(alice_temp_temp);
vector<int> alice(alice_count);
for (int i = 0; i < alice_count; i++) {
int alice_item = stoi(alice_temp[i]);
alice[i] = alice_item;
}
vector<int> result = climbingLeaderboard(scores, alice);
for (int i = 0; i < result.size(); i++) {
fout << result[i];
if (i != result.size() - 1) {
fout << "\n";
}
}
fout << "\n";
fout.close();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [](const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
'알고리즘 > 기타' 카테고리의 다른 글
[HackerRank] Forming a Magic Square (0) | 2018.09.30 |
---|---|
[HackerRank] Extra Long Factorials (0) | 2018.09.28 |
[HackerRank] Time Conversion (0) | 2018.09.27 |
[HackerRank] Birthday Cake Candles (0) | 2018.09.27 |
[HackerRank] Mini-Max Sum (0) | 2018.09.27 |
- Total
- Today
- Yesterday
- 17143
- 17144
- DP
- 입출력
- 연구소 3
- 미세먼지 안녕!
- 트렌드
- 게리맨더링 2
- 17837
- scanf
- SWEA
- 17140
- 2018 카카오 블라인드 채용
- 새로운 게임 2
- STL
- 시간 복잡도
- string
- hackerrank
- 백준
- 17142
- 삼성
- 이차원 배열과 연산
- 2018 KAKAO BLIND RECRUITMENT
- boj
- 팁
- SW Expert Academy
- DFS
- 17779
- 알고리즘
- 역량 테스트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |