FOJ 2160 Mountain climbing

题目链接http://acm.fzu.edu.cn/problem.php?pid=2160


详细题解


①把每一棵树的x和h当做一个点p(x,h) 。构建一个结构体Tree,包括数据:该树的坐标p,该树到第n棵树的步骤ans,和该树可以到达的最远的一棵树next。


②将i点,i+1点,i+1点的next点分别记做 a,b,c点。运用叉积判断 射线cb和射线ca的叉积的正负情况,如果为正,则Tree[i].ans = Tree[i+1].ans+1,  Tree[i].next = i+1. 如果为负将i点,i+1点的next点, i+1点的next点的next点 分别记做 a,b,c点,重复以上的操作,直到为正或者next点为n。



#include<cstdio>
#include<cmath>
#include<cstring>
#define N 110000
const double eps = 1e-10;
int dcmp(double x)
{
	if (fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector; //Vector 为 Point的别名
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
double Cross(Vector A, Vector B){ return A.x*B.y - A.y*B.x; }
struct Tree
{
	Point p;
	int next_no;
	int ans;
};
Tree node[N];
void Tree_clear()
{
	for (int i = 1; i < N; i++)
	{
		node[i].ans = 0;
		node[i].next_no = i;
	}
}
int main()
{
	int T, t = 1;
	scanf("%d", &T);
	while (T--)
	{
		Tree_clear();
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%lf %lf", &node[i].p.x, &node[i].p.y);


		if (n == 1){ printf("Case#%d: 0\n", t++); continue; }
		node[n].ans = 0;
		node[n].next_no = n;
		node[n - 1].ans = 1;
		node[n - 1].next_no = n;

		for (int i = n - 2; i >= 1; i--)
		{
			int next = i + 1;
			int no = node[next].next_no;
			double temp = Cross(node[next].p - node[no].p, node[i].p - node[no].p);
			while (temp < 0)
			{
				next = no;
				no = node[next].next_no;
				temp = Cross(node[next].p - node[no].p, node[i].p - node[no].p);
			}


			node[i].ans = node[next].ans + 1;
			node[i].next_no = next;

		}

		printf("Case#%d:", t++);
		for (int i = 1; i <= n; i++)
			printf(" %d", node[i].ans);
		printf("\n");

	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值