1. 程式人生 > 實用技巧 >AtCoder Beginner Contest 178 E - Dist Max

AtCoder Beginner Contest 178 E - Dist Max

題目連結:https://atcoder.jp/contests/abc178/tasks/abc178_e

題意:給定n組座標 求

思路: 去絕對值化簡 假設xi>xj

有xi-xj+yi-yj 則(xi+yi)-(xj+yj)

或者有 xi-xj-yi+yj 則 (xi-yi)-(xj-yj) 所以把用兩個陣列記錄x+y x-y 排序在找最大的差即可

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define pb push_back
 5 const int maxn =2e5+10
; 6 const int mod=1e9+7; 7 8 9 10 11 int main() 12 { 13 ios::sync_with_stdio(false); 14 cin.tie(0); 15 int n; 16 cin>>n; 17 vector<int>a,b; 18 for(int i=1;i<=n;i++) 19 { 20 int x,y; 21 cin>>x>>y; 22 int t=x+y; 23 int
t2=x-y; 24 a.pb(t); 25 b.pb(t2); 26 } 27 sort(a.begin(),a.end()); 28 sort(b.begin(),b.end()); 29 cout<<max(a.back()-a.front(),b.back()-b.front())<<'\n'; 30 31 32 33 34 35 36 37 }
View Code