【題解】ZeroJudge a362: 1. 搬雕像

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a362

#include <iostream>
#include <algorithm>
using namespace std;

int n, a[10005], h[10005], w[10005], cnt;

bool cmp(int x, int y){
    if (h[x] != h[y]) return h[x] < h[y];
    if (w[x] != w[y]) return w[x] < w[y];
    return x < y;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> h[i] >> w[i];
        a[i] = i;
    }
    sort(a, a+n, cmp);
    for (int i = 0; i < n; i++){
        cnt += abs(i-a[i]);
    }
    cout << cnt << "\n";
}
分享本文 Share with friends