Eastern Exhibition
The question
Solve The very first thing we should know is we are using Manhattan distance to represent the distance of to points, which means we can handle Xs and Ys separately.
1 2 3 for (int i=0 ; i<n; i++) cin>>x[i]>>y[i];sort (x.begin (), x.end ());sort (y.begin (), y.end ());
If the number of points is odd, there will be only 1 point to make sum of absolute difference of Xs minimum, so does Ys.
Otherwise, any X between two mid Xs has the same sum, so does Ys.
1 2 3 4 5 6 7 8 9 10 11 12 ll cx, cy; if (x.size ()&1 ) cx=1 ;else { int mid=x.size ()>>1 ; cx=x[mid]-x[mid-1 ]+1 ; } if (y.size ()&1 ) cy=1 ;else { int mid=y.size ()>>1 ; cy=y[mid]-y[mid-1 ]+1 ; } cout<<cx*cy<<endl;
Code 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 29 30 31 32 33 34 35 36 37 38 39 40 #include <bits/stdc++.h> using namespace std;const int maxn = 2e5 + 5 ;const int inf = INT_MAX;const int mod = 1e9 + 7 ;typedef long long ll;vector<int > x, y; int main () { ios::sync_with_stdio (false ); int t; cin>>t; while (t--){ int n; cin>>n; x.resize (n); y.resize (n); for (int i=0 ; i<n; i++) cin>>x[i]>>y[i]; sort (x.begin (), x.end ()); sort (y.begin (), y.end ()); ll cx, cy; if (x.size ()&1 ) cx=1 ; else { int mid=x.size ()>>1 ; cx=x[mid]-x[mid-1 ]+1 ; } if (y.size ()&1 ) cy=1 ; else { int mid=y.size ()>>1 ; cy=y[mid]-y[mid-1 ]+1 ; } cout<<cx*cy<<endl; } return 0 ; }