【題解】ZeroJudge c230: 松鼠旅行

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c230
姊妹題:TIOJ 1419 . 飛天李晴(?) (Sunny)

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
#define pii pair<int,int>

bool cmp(pii a, pii b){
    if (a.first == b.first) return a.second > b.second;
    return a.first < b.first;
}

int main(){
    int n;
    vector <pii>v;
    while (cin >> n){
        int a, b, c, maxi = 0, ans = 0;
        v.clear();
        for (int i = 0; i < n; i++){
            cin >> a >> b >> c;
            v.push_back({a*a + b*b, c});
        }
        sort(v.begin(), v.end(), cmp);
        for (int i = 0; i < n; i++){
            ans = max(ans, maxi-v[i].second);
            maxi = max(maxi, v[i].second);
        }
        cout << ans << "\n";
    }
}

Python 程式碼 (credit: Amy Chou)
注意:在ZeroJudge會RE,在TIOJ可以AC

N = int(input())
v = []
for i in range(N):
    x, y, h = map(int, input().split())
    v.append((x*x + y*y, h))

v.sort(key=lambda x:(x[0], -x[1]))

ans = 0
mx = 0
for i in range(N):
    ans = max(ans, mx - v[i][1])
    mx = max(mx, v[i][1])

print(ans)
分享本文 Share with friends