1일1알

백준 10655번 마라톤 1 C++ 본문

알고리즘

백준 10655번 마라톤 1 C++

영춘권의달인 2023. 4. 8. 12:28

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

 

10655번: 마라톤 1

젖소 박승원은 2번째 혹은 3번째 체크포인트를 건너뛸 수 있는데, 여기서 두 번째 체크포인트를 건너뛸 경우 경로는 (0,0) -> (11,-1) -> (10,0) 이 되며 거리는 14이다. 박승원은 이것보다 더 짧게 달릴

www.acmicpc.net

 

#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 <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int n;
int64 totalDist = 0;
vector<pair<int, int>> v;
vector<int64> dists;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n;
    v = vector<pair<int, int>>(n);
    dists = vector<int64>(n);
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        v[i] = { x,y };
    }
    for (int i = 1; i < n; i++) {
        int64 dist = abs(v[i - 1].first - v[i].first) + abs(v[i - 1].second - v[i].second);
        dists[i] = dist;
        totalDist += dist;
    }
    int64 ans = totalDist;
    for (int i = 1; i < n - 1; i++) {
        int64 dist1 = dists[i] + dists[i + 1];
        int64 dist2 = abs(v[i - 1].first - v[i + 1].first) + abs(v[i - 1].second - v[i + 1].second);
        int sub = dist1 - dist2;
        ans = min(ans, totalDist - sub);
    }
    cout << ans;
}