728x90
인접한 카드를 합쳐서 가장 많이 흭득할 수 있는 골드를 출력하는 문제입니다.
골드는 합치는 카드들의 레벨의 합만큼 받습니다.
가장 레벨이 큰 카드를 기준으로 오른쪽과 왼쪽을 확인해서 더 큰 값을 합치고
그 값을 누적해서 결과를 얻어냈습니다.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
int main(int argc, char* argv[])
{
int n;
int a, b;
int right, left;
int higherLevel = 0;
int index = 0;
int score = 0;
vector<int> v;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a;
v.push_back(a);
if (higherLevel < a) {
higherLevel = a;
index = i;
}
}
while (1) {
right = index + 1;
left = index - 1;
if (right >= n && left < 0) {
break;
}
else if (right >= n) {
score += v[index] + v[left];
v.erase(v.begin() + left);
--index;
--n;
}
else if (left < 0) {
score += v[index] + v[right];
v.erase(v.begin() + right);
--n;
}
else {
if (v[left] > v[right]) {
score += v[index] + v[left];
v.erase(v.begin() + left);
--index;
--n;
}
else {
score += v[index] + v[right];
v.erase(v.begin() + right);
--n;
}
}
}
cout << score << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 8980번 택배 (0) | 2019.11.24 |
---|---|
[C++] 백준 2212번 센서 (0) | 2019.11.24 |
[C++] 백준 1449번 수리공 항승 (0) | 2019.11.23 |
[C++] 백준 1969번 DNA (0) | 2019.11.23 |
[C++] 백준 4796번 캠핑 (0) | 2019.11.23 |