Submission #1533171


Source Code Expand

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <ctime>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll;
typedef vector<vector<long long> > vvll;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i,n) _rep2((i),0,(n))
#define _rep2(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

ll mainmain(int n, vector<pll> v){
	sort(ALL(v));
	if(n==1){
		return 0;
	}
	ll mn = INFL, mx = 0;
	int pmn = -1, pmx = -1;
	rep(i,n){
		if(mn > v[i].fi){
			mn = v[i].fi;
			pmn = i;
		}
		if(mx < v[i].se){
			mx = v[i].se;
			pmx = i;
		}
	}
	// cout<<mx<<" "<<pmx<<endl;
	// cout<<mn<<" "<<pmn<<endl;
	ll a = 0;
	ll b = INFL;
	if(pmn==pmx){
		v.erase(v.begin()+pmn);
	}
	else{
		a = v[pmx].fi;
		b = v[pmn].se;
		v.erase(v.begin()+max(pmn,pmx));
		v.erase(v.begin()+min(pmn,pmx));
	}
	sort(ALL(v));
	// cout<<v.size()<<endl;
	// ll aa = max(a,b), bb = min(a,b);
	ll aa = a, bb = b;
	a = aa;
	b = bb;
	// cout<<"ab "<<a<<" "<<b<<endl;
	rep(i,v.size()){
		maxs(a, v[i].fi);
		mins(b, v[i].se);
	}
	// cout<<"ab "<<a<<" "<<b<<endl;
	ll ans = (a-mn)*(mx-b);
	// cout<<"ans "<<ans<<endl;
	if(pmn == pmx || v.size()==0){
		return ans;
	}
	// cout<<a<<" "<<b<<endl;
	// aa = max(a,b), bb = min(a,b);

	a = max(aa,bb);
	b = min(aa,bb);
	// cout<<"ab "<<a<<" "<<b<<endl;
	vll wfi(v.size());
	ll tmx = 0;
	for(int i=wfi.size()-1; i>=0; i--){
		maxs(tmx, v[i].fi);
		wfi[i] = tmx;
	}
	rep(i,v.size()){
		ll l = min(b, v[i].fi);
		ll r = max(a, wfi[i]);
		// cout<<"r l "<<r<<" "<<l<<endl;
		mins(ans, (mx-mn)*(r-l));
		mins(b, v[i].se);
		maxs(a, v[i].se);
	}
	a = max(aa,bb);
	b = min(aa,bb);
	vll wse(v.size());
	rep(i,n) swap(v[i].fi, v[i].se);
	sort(ALL(v));
	rep(i,n) swap(v[i].fi, v[i].se);
	ll tmn = INFL;
	rep(i,wfi.size()){
		mins(tmn, v[i].se);
		wse[i] = tmn;
	}
	for(int i=wfi.size()-1; i>=0; i--){
		ll l = min(b, wse[i]);
		ll r = max(a, v[i].se);
		// cout<<"r l "<<r<<" "<<l<<endl;
		// cout<<"v[i] "<<v[i].fi<<" "<<v[i].se<<endl;
		mins(ans, (mx-mn)*(r-l));
		mins(b, v[i].fi);
		maxs(a, v[i].fi);
	}
	return ans;
}

ll solve(int n, vector<pll> v){
	ll ans = INFL;
	rep(i,1<<n){
		ll rmx = 0, rmn = INFL;
		ll bmx = 0, bmn = INFL;
		rep(j,n){
			ll a = v[j].fi, b = v[j].se;
			if(i&(1<<j)){
				swap(a,b);
			}
			maxs(rmx, a);
			mins(rmn, a);
			maxs(bmx, b);
			mins(bmn, b);
		}
		mins(ans, (rmx-rmn)*(bmx-bmn));
	}
	return ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    int n;
	cin>>n;
	// n = 10;
	vector<pll> v(n);
	rep(i,n){
		cin>>v[i].fi>>v[i].se;
		if(v[i].fi>v[i].se) swap(v[i].fi, v[i].se);
	}
	cout<<mainmain(n, v)<<endl;
	return 0;
	// srand(time(NULL));
	// while(1){
	// 	rep(i,n){
	// 		cin>>v[i].fi>>v[i].se;
	// 		// v[i] = pll(rand()%10, rand()%10);
	// 		if(v[i].fi > v[i].se) swap(v[i].fi, v[i].se);
	// 	}
	// 	if(mainmain(n,v)!=solve(n,v)){
	// 		cout<<mainmain(n,v)<<" "<<solve(n,v)<<endl;
	// 		rep(i,n) cout<<v[i].fi<<" "<<v[i].se<<endl;
	// 		return 0;
	// 	}
	//     // cout<<mainmain(n, v)<<endl;
	//     // cout<<solve(n, v)<<endl;
	// }
}

Submission Info

Submission Time
Task E - Ball Coloring
User j_gui0121
Language C++14 (GCC 5.4.1)
Score 700
Code Size 4938 Byte
Status AC
Exec Time 82 ms
Memory 9600 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 35
Set Name Test Cases
Sample example0, example1, example2
All div20, div21, div22, div23, div24, example0, example1, example2, maxrand0, maxrand1, maxrand2, maxrand20, maxrand21, maxrand210, maxrand211, maxrand22, maxrand23, maxrand24, maxrand25, maxrand26, maxrand27, maxrand28, maxrand29, maxrand3, maxrand4, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, sparse0, sparse1, sparse2, sparse3, sparse4
Case Name Status Exec Time Memory
div20 AC 80 ms 9600 KB
div21 AC 79 ms 9600 KB
div22 AC 80 ms 9600 KB
div23 AC 80 ms 9600 KB
div24 AC 80 ms 9600 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 80 ms 9600 KB
maxrand1 AC 80 ms 9600 KB
maxrand2 AC 80 ms 9600 KB
maxrand20 AC 79 ms 9600 KB
maxrand21 AC 81 ms 9600 KB
maxrand210 AC 73 ms 9600 KB
maxrand211 AC 79 ms 9600 KB
maxrand22 AC 81 ms 9600 KB
maxrand23 AC 82 ms 9600 KB
maxrand24 AC 82 ms 9600 KB
maxrand25 AC 79 ms 9600 KB
maxrand26 AC 79 ms 9600 KB
maxrand27 AC 81 ms 9600 KB
maxrand28 AC 82 ms 9600 KB
maxrand29 AC 82 ms 9600 KB
maxrand3 AC 80 ms 9600 KB
maxrand4 AC 80 ms 9600 KB
smallrand0 AC 1 ms 256 KB
smallrand1 AC 1 ms 256 KB
smallrand2 AC 1 ms 256 KB
smallrand3 AC 1 ms 256 KB
smallrand4 AC 1 ms 256 KB
sparse0 AC 72 ms 9600 KB
sparse1 AC 72 ms 9600 KB
sparse2 AC 72 ms 9600 KB
sparse3 AC 72 ms 9600 KB
sparse4 AC 72 ms 9600 KB