[TIOJ] 1280. 領土 (Territory)

題目連結:http://tioj.infor.org/problems/1280
應該很明顯就是要求個凸包的面積,那就先求個凸包,求法就是先求出上凸包,再求下凸包,看上下凸包的方法也不難,就看看兩個相鄰的邊的夾角是不是超過180度就好。接著要求面積就直接算個有向面積加起來,而因為剛剛求凸包的順序關西,我們求有向面積的時候甚至不需要極角排序,直接求就好了。
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
typedef pair<lld,lld> PLL;
#define FF first
#define SS second
const int N = 10000 + 5;

inline PLL operator-(const PLL &a, const PLL &b){
	return {a.FF-b.FF, a.SS-b.SS};
}
inline lld cross(const PLL &a, const PLL &b){
	return a.FF*b.SS - b.FF*a.SS;
}
inline lld cross(const PLL &o, const PLL &a, const PLL &b){
	return cross(a-o, b-o);
}

PLL arr[N];
vector<PLL> hull1, hull2;

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	int n; cin>>n;
	for(int i=0;i<n;i++) cin>>arr[i].FF>>arr[i].SS;
	sort(arr, arr+n);
	for(int i=0;i<n;i++){
		while(hull1.size() > 1 and \
			cross(hull1.back(), hull1[hull1.size()-2], arr[i]) < 0)
			hull1.pop_back();
		hull1.push_back(arr[i]);
	}
	for(int i=n-1;i>=0;i--){
		while(hull2.size() > 1 and \
			cross(hull2.back(), hull2[hull2.size()-2], arr[i]) < 0)
			hull2.pop_back();
		hull2.push_back(arr[i]);
	}
	hull1.insert(hull1.end(), hull2.begin(), hull2.end());
	lld ans = 0;
	for(int i=1;i<(int)hull1.size();i++){
		ans += cross(hull1[i], hull1[i-1]);
	}
	cout<<(ans+1)/2<<'\n';
	return 0;
}

留言

這個網誌中的熱門文章

[TIOJ] 1094. C.幼稚國王的獎賞

[TIOJ] 1902. 「殿仁.王,不認識,誰啊?」,然後他就死了……

[IOJ] 19. 啦啦啦