1. 程式人生 > 實用技巧 >hihocoder #1442 : Smallest Rectangle

hihocoder #1442 : Smallest Rectangle

題意 :

給N個點,

從裡面隨機取四個點,如果能構成矩形,求最小的矩形面積

題解:

一旦對角兩個點確定 四個點都確定

複雜度N方

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,a,n) for(int i=n;i>=a;--i)
#define pb push_back
#define fi first
#define se second
#define io std::ios::sync_with_stdio(false)
using namespace
std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } ll qpow(ll a,ll n) { ll r=1%P; for (a%=P; n; a=a*a%P,n>>=1)if(n&1)r=r*a%P; return r; } const int maxn=1e4+10; int main() { io;
int n; cin>>n; map<pii,int> mp; std::vector<pii> v; for(int i=1;i<=n;i++) { pii x; cin>>x.fi>>x.se; mp[x]++; v.push_back(x); } ll ans=1e18; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { int x1=v[i].fi,y1=v[i].se;
int x2=v[j].fi,y2=v[j].se; if(x1==x2||y1==y2) continue; if(mp[pii(x1,y2)]&&mp[pii(x2,y1)]) ans=min(ans,1ll*abs(x1-x2)*abs(y1-y2)); } if(ans==1e18) return puts("-1"),0; cout<<ans<<endl; }