HDU - 5033 Building(单调栈)

本文介绍了一种利用单调栈优化算法来解决高楼视角模拟的问题,通过计算特定位置观察天空的最大角度,探讨了高效算法的设计与实现。
Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position x  i with its height h  i. All skyscrapers located in different place. The skyscrapers had no width, to make it simple. As the skyscrapers were so high, Matt could hardly see the sky.Given the position Matt was at, he wanted to know how large the angle range was where he could see the sky. Assume that Matt's height is 0. It's guaranteed that for each query, there is at least one building on both Matt's left and right, and no building locate at his position.
 

Input

The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow. 

Each test case begins with a number N(1<=N<=10^5), the number of buildings. 

In the following N lines, each line contains two numbers, x  i(1<=x  i<=10^7) and h  i(1<=h  i<=10^7). 

After that, there's a number Q(1<=Q<=10^5) for the number of queries. 

In the following Q lines, each line contains one number q  i, which is the position Matt was at.
 

Output

For each test case, first output one line "Case #x:", where x is the case number (starting from 1). 

Then for each query, you should output the angle range Matt could see the sky in degrees. The relative error of the answer should be no more than 10^(-4).
 

Sample Input

    
3 3 1 2 2 1 5 1 1 4 3 1 3 2 2 5 1 1 4 3 1 4 2 3 5 1 1 4
 

Sample Output

    
Case #1: 101.3099324740 Case #2: 90.0000000000 Case #3: 78.6900675260
 


题目大意:
在一个平地上右n个摩天轮,题目告诉你每个摩天轮的位置,和高度,现在马特站在q个位置上,问马特站在每个位置上,他能看到最大仰望天空的角度为多少?
1<=N<=10^5 1<=Q<=10^5

解析:
这题的数据量很大,如果直接暴力的话肯定超时。
所以要用单调栈进行优化,将人和楼组合在一起,按照位置从小到大排序后,维护一个从左到右凸的高度下降的单调栈,然后每次查询到人的位置的时候,维护单调栈,使得图形是凸的,那么栈顶和人所构成的仰角就是正向答案。但是这只是正向的答案,所以反向扫描所有的位置,维护出一个从右到左凸的单调栈,求出反向的答案,和之前的结果相加就是最终答案。

总结:在做这题时把pi写成int类型了,白白浪费了一个下午的时间要好好吸取教训。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <algorithm>
using namespace std;
const int N = 100005 * 2;
const double PI = acos(-1.0);
struct Node {
	double x,y;
	int flag;
}p[N],st[N];
double ans[N];
int n,q;
bool cmp(Node a,Node b) {
	return a.x < b.x;
}
double getAngle(Node a,Node b) {
	return atan(fabs(b.x - a.x) / a.y);
}
double rate(Node a,Node b) {
	return (a.y - b.y) / (a.x - b.x);
}
bool judge_front(Node a,Node b,Node c) {
	double k1 = rate(a,b);
	double k2 = rate(a,c);
	if(k1 < 0 && k2 < 0 && fabs(k1) >= fabs(k2)) {
		return true;
	}else {
		return false;
	}
}
void solve_front() {
	int top = 0;
	for(int i = 0; i < n+q; i++) {
		if(p[i].flag) {
			while(top >= 2 && judge_front(st[top-2],st[top-1],p[i]) ) {
				top--;
			}
			ans[p[i].flag] += getAngle(st[top-1],p[i]);
		}else {
			while(top && st[top-1].y <= p[i].y) {
				top--;
			}
			while(top >= 2 && judge_front(st[top-2],st[top-1],p[i])) {
				top--;
			}
			st[top] = p[i];
			top++;
		}
	}
}
bool judge_back(Node a,Node b,Node c) {
	double k1 = rate(a,b);
	double k2 = rate(a,c);
	if(k1 > 0 && k2 > 0 && fabs(k1) >= fabs(k2)) {
		return true;
	}else {
		return false;
	}
}
void solve_back() {
	int top = 0;
	for(int i = n+q-1; i >= 0; i--) {
		if(p[i].flag) {
			while(top >= 2 && judge_back(st[top-1],st[top-2],p[i]) ) {
				top--;
			}
			ans[p[i].flag] += getAngle(st[top-1],p[i]);
		}else {
			while(top && st[top-1].y <= p[i].y) {
				top--;
			}
			while(top >= 2 && judge_back(st[top-2],st[top-1],p[i])) {
				top--;
			}
			st[top++] = p[i];
		}
	}
}
int main() {
	int t , cas = 1;
	scanf("%d",&t);
	while(t--) {
		scanf("%d",&n);
		for(int i = 0; i < n; i++) {
			scanf("%lf%lf",&p[i].x,&p[i].y);
			p[i].flag = 0;
		}
		scanf("%d",&q);
		for(int i = n; i < n+q; i++) {
			scanf("%lf",&p[i].x);
			p[i].y = 0;
			p[i].flag = i - n + 1;
		}
		sort(p,p+n+q,cmp);
		memset(ans,0,sizeof(ans));
		solve_front();
		solve_back();
		printf("Case #%d:\n",cas++);
		for(int i = 1; i <= q; i++) {
			printf("%.10lf\n",ans[i] * 180 / PI);
		}
	}
	return 0;
}


HDU-3480 是一个典型的动态规划问题,其题目标题通常为 *Division*,主要涉及二维费用背包问题或优化后的动态规划策略。题目大意是:给定一个整数数组,将其划分为若干个连续的子集,每个子集最多包含 $ m $ 个元素,并且每个子集的最大值与最小值之差不能超过给定的阈值 $ t $,目标是使所有子集的划分代价总和最小。每个子集的代价是该子集最大值与最小值的差值。 ### 动态规划思路 设 $ dp[i] $ 表示前 $ i $ 个元素的最小代价。状态转移方程如下: $$ dp[i] = \min_{j=0}^{i-1} \left( dp[j] + cost(j+1, i) \right) $$ 其中 $ cost(j+1, i) $ 表示从第 $ j+1 $ 到第 $ i $ 个元素构成一个子集的代价,即 $ \max(a[j+1..i]) - \min(a[j+1..i]) $。 为了高效计算 $ cost(j+1, i) $,可以使用滑动窗口或单调队列等数据结构来维护区间最大值与最小值,从而将时间复杂度优化到可接受的范围。 ### 示例代码 以下是一个简化版本的动态规划实现,使用暴力方式计算区间代价,适用于理解问题结构: ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 10010; int a[MAXN]; int dp[MAXN]; int main() { int T, n, m; cin >> T; for (int Case = 1; Case <= T; ++Case) { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; dp[0] = 0; for (int i = 1; i <= n; ++i) { dp[i] = INF; int mn = a[i], mx = a[i]; for (int j = i; j >= max(1, i - m + 1); --j) { mn = min(mn, a[j]); mx = max(mx, a[j]); if (mx - mn <= T) { dp[i] = min(dp[i], dp[j - 1] + mx - mn); } } } cout << "Case " << Case << ": " << dp[n] << endl; } return 0; } ``` ### 优化策略 - **单调队列**:可以使用两个单调队列分别维护当前窗口的最大值与最小值,从而将区间代价计算的时间复杂度从 $ O(n^2) $ 降低到 $ O(n) $。 - **斜率优化**:若问题满足特定的决策单调性,可以考虑使用斜率优化技巧进一步加速状态转移过程。 ### 时间复杂度分析 原始暴力解法的时间复杂度为 $ O(n^2) $,在 $ n \leq 10^4 $ 的情况下可能勉强通过。通过单调队列优化后,可以稳定运行于 $ O(n) $ 或 $ O(n \log n) $。 ### 应用场景 HDU-3480 的问题模型可以应用于资源调度、任务划分等场景,尤其适用于需要控制子集内部差异的问题,如图像分块压缩、数据分段处理等[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值