Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Team Fortress 2
- 자료구조
- 그래프
- 유니티
- ue5
- XR Interaction Toolkit
- 다익스트라
- 재귀
- BFS
- 문자열
- 그리디 알고리즘
- 알고리즘
- c++
- 유니온 파인드
- DFS
- 다이나믹 프로그래밍
- 수학
- 누적 합
- 브루트포스
- 백트래킹
- 백준
- 트리
- 시뮬레이션
- 투 포인터
- 정렬
- 스택
- VR
- 우선순위 큐
- Unreal Engine 5
- 구현
Archives
- Today
- Total
1일1알
백준 2374번 같은 수로 만들기 C++ 본문
가장 작은 수부터 인접한 수와 같게 만들어가면서 문제를 해결하였다.
답을 int로 하면 오버플로우가 나기 때문에 long long을 사용하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int n;
vector<int> v(1001);
ll ans = 0;
void Add(int index) {
int val = v[index];
int start = index;
int end = index;
int MIN = 1000000001;
while (true) {
start--;
if (start <= 0) {
start = 1;
break;
}
if (v[start] != val) {
MIN = min(MIN, v[start]);
start++;
break;
}
}
while (true) {
end++;
if (end > n) {
end = n;
break;
}
if (v[end] != val) {
MIN = min(MIN, v[end]);
end--;
break;
}
}
for (int i = start; i <= end; i++) {
v[i] = MIN;
}
ans += MIN - val;
}
void solve() {
bool repeat = true;
while (repeat) {
repeat = false;
int MIN = 1000000001;
int lastVal = v[1];
for (int i = 1; i <= n; i++) {
if (lastVal != v[i]) {
repeat = true;
lastVal = v[i];
}
if (v[i] < MIN) MIN = v[i];
}
if (repeat) {
for (int i = 1; i <= n; i++) {
if (v[i] == MIN) {
Add(i);
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
solve();
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 2082번 시계 C++ (0) | 2022.02.11 |
---|---|
백준 1730번 판화 C++ (0) | 2022.02.10 |
백준 14600번 샤워실 바닥 깔기 (Small) C++ (0) | 2022.02.08 |
백준 1275번 커피숍2 C++ (0) | 2022.02.07 |
백준 1802번 종이 접기 C++ (0) | 2022.02.06 |