团体程序设计天梯赛 L3-009 长城 (30分)

题目链接:

L3-009 长城 (30分)

思路:

既然所组建的瞭望台可以看见所有地方,我们不妨化简一下题意:
组建的瞭望台可以看见所有的点—>对于每个点我们找到它南方的瞭望台
做法就是对于每一个点我们遍历它南方的点,找到必须成为瞭望台的那个点,将那个点设置为瞭望台;
假设当前点为A,它的南方有相邻的两点B、C,此时B必须成为瞭望台的条件是 A B ⃗ × A C ⃗ < 0 \vec{AB} \times \vec{AC} < 0 AB ×AC <0,这里的乘法是叉乘,几何意义为线段AB需要在线段AC上方,即ABC形成一个凸点;
(在计算后面的点时,之前所计算出的凹点可以忽略去,所以将它们出栈)

代码:

#include<bits/stdc++.h>

using namespace std;

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

typedef long long ll;
const int maxn = 1e5 + 5;
int n, s[maxn], t;
ll x[maxn], y[maxn];

inline bool check(int a, int b, int c) {
	return (x[b] - x[a]) * (y[c] - y[a]) >= (x[c] - x[a]) * (y[b] - y[a]);
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	n = read();
	set<int> st;
	for(int i = 0; i < n; i++) {
		x[i] = read(), y[i] = read();
		while(t > 1 && check(i, s[t - 1], s[t - 2])) --t;
		if(s[t - 1]) st.insert(s[t - 1]);
		s[t++] = i;
	}
	cout << st.size();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值