hdu 5033 ( Building ) 单调栈
2017-10-10 15:50
423 查看
题意: 给出建筑物位置与高度,以及人的位置,询问人看到天空的最大角度
思路:
对于每个位置,人的视野受到左侧与右侧楼与这个点相连的直线 , 左侧斜率最小的 以及右侧斜率最大的 两条直线限制,如果暴力枚举显然会T
可以处理为一个栈的形式:
以左侧楼的限制为例:右侧同理
对于每个位置 如果这个位置是楼,就扔进先除去所有栈中比它高度低的, 再以当前点为基准,所描栈中的点,确保栈中点的到当前点的斜率是递减的(左侧栈是个 凸形),这样就会保证了当前点是卡住人的视野最大的。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#include<cmath>
using namespace std;
const int N = 3e5 + 50;
const double pi = acos(-1.0);
typedef long long ll;
struct p{
ll x, h, id;
}a
, ansl
, ansr
;
bool cmp(p a, p b){
return a.x < b.x;
}
stack<int> s;
int main(){
int T;
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++){
memset(a, 0, sizeof a);
memset(ansl, 0, sizeof ansl);
memset(ansr, 0, sizeof ansr);
int n, m;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lld %lld", &a[i].x, &a[i].h);
scanf("%d", &m);
for(int i = n + 1; i <= n + m; i++) {
scanf("%lld", &a[i].x);
a[i].h = -1, a[i].id = i - n;
}
int tot = n + m;
sort(a + 1, a + tot + 1, cmp);
while(!s.empty())
s.pop();
for(int i = 1; i <= tot; i++){
if(a[i].h != -1){
while(!s.empty() && a[i].h >= a[s.top()].h) s.pop();
while(s.size() > 1){
ll x1 = a[i].x - a[s.top()].x, h1 = a[s.top()].h - a[i].h;
int t = s.top();
s.pop();
ll x2 = a[t].x
4000
- a[s.top()].x, h2 = a[s.top()].h - a[t].h;
if(h1 * x2 > h2 * x1) {
s.push(t);
break;
}
}
s.push(i);
}else {
while(s.size() > 1){
ll x1 = a[i].x - a[s.top()].x, h1 = a[s.top()].h;
int t = s.top();
s.pop();
ll x2 = a[i].x - a[s.top()].x, h2 = a[s.top()].h;
if(h1 * x2 > x1 * h2) {
s.push(t);
break;
}
}
ansl[a[i].id].h = s.top();
ansl[a[i].id].x = a[i].x;
}
}
while(!s.empty())
s.pop();
for(int i = tot; i >= 1; i--){
if(a[i].h != -1){
while(!s.empty() && a[s.top()].h <= a[i].h) s.pop();
while(s.size() > 1){
ll x1 = a[s.top()].x - a[i].x, h1 = a[s.top()].h - a[i].h;
int t = s.top();
s.pop();
ll x2 = a[s.top()].x - a[t].x, h2 = a[s.top()].h - a[t].h;
if(h1 * x2 > h2 * x1) {
s.push(t);
break;
}
}
s.push(i);
}else {
while(s.size() > 1){
ll x1 = a[s.top()].x - a[i].x, h1 = a[s.top()].h;
int t = s.top();
s.pop();
ll x2 = a[s.top()].x - a[i].x, h2 = a[s.top()].h;
if(h1 * x2 > x1 * h2) {
s.push(t);
break;
}
}
ansr[a[i].id].h = s.top();
ansr[a[i].id].x = a[i].x;
}
}
printf("Case #%d:\n", kase);
for(int i = 1; i <= m; i++){
double j1 = 180 * 1.0 / pi * atan2(1.0 * a[ansl[i].h].h, (ansl[i].x - a[ansl[i].h].x));
double j2 = 180 * 1.0 / pi * atan2(1.0 * a[ansr[i].h].h, (a[ansr[i].h].x - ansr[i].x));
printf("%0.10lf\n", 180 - j1 - j2);
}
}
return 0;
}
思路:
对于每个位置,人的视野受到左侧与右侧楼与这个点相连的直线 , 左侧斜率最小的 以及右侧斜率最大的 两条直线限制,如果暴力枚举显然会T
可以处理为一个栈的形式:
以左侧楼的限制为例:右侧同理
对于每个位置 如果这个位置是楼,就扔进先除去所有栈中比它高度低的, 再以当前点为基准,所描栈中的点,确保栈中点的到当前点的斜率是递减的(左侧栈是个 凸形),这样就会保证了当前点是卡住人的视野最大的。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#include<cmath>
using namespace std;
const int N = 3e5 + 50;
const double pi = acos(-1.0);
typedef long long ll;
struct p{
ll x, h, id;
}a
, ansl
, ansr
;
bool cmp(p a, p b){
return a.x < b.x;
}
stack<int> s;
int main(){
int T;
scanf("%d", &T);
for(int kase = 1; kase <= T; kase++){
memset(a, 0, sizeof a);
memset(ansl, 0, sizeof ansl);
memset(ansr, 0, sizeof ansr);
int n, m;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%lld %lld", &a[i].x, &a[i].h);
scanf("%d", &m);
for(int i = n + 1; i <= n + m; i++) {
scanf("%lld", &a[i].x);
a[i].h = -1, a[i].id = i - n;
}
int tot = n + m;
sort(a + 1, a + tot + 1, cmp);
while(!s.empty())
s.pop();
for(int i = 1; i <= tot; i++){
if(a[i].h != -1){
while(!s.empty() && a[i].h >= a[s.top()].h) s.pop();
while(s.size() > 1){
ll x1 = a[i].x - a[s.top()].x, h1 = a[s.top()].h - a[i].h;
int t = s.top();
s.pop();
ll x2 = a[t].x
4000
- a[s.top()].x, h2 = a[s.top()].h - a[t].h;
if(h1 * x2 > h2 * x1) {
s.push(t);
break;
}
}
s.push(i);
}else {
while(s.size() > 1){
ll x1 = a[i].x - a[s.top()].x, h1 = a[s.top()].h;
int t = s.top();
s.pop();
ll x2 = a[i].x - a[s.top()].x, h2 = a[s.top()].h;
if(h1 * x2 > x1 * h2) {
s.push(t);
break;
}
}
ansl[a[i].id].h = s.top();
ansl[a[i].id].x = a[i].x;
}
}
while(!s.empty())
s.pop();
for(int i = tot; i >= 1; i--){
if(a[i].h != -1){
while(!s.empty() && a[s.top()].h <= a[i].h) s.pop();
while(s.size() > 1){
ll x1 = a[s.top()].x - a[i].x, h1 = a[s.top()].h - a[i].h;
int t = s.top();
s.pop();
ll x2 = a[s.top()].x - a[t].x, h2 = a[s.top()].h - a[t].h;
if(h1 * x2 > h2 * x1) {
s.push(t);
break;
}
}
s.push(i);
}else {
while(s.size() > 1){
ll x1 = a[s.top()].x - a[i].x, h1 = a[s.top()].h;
int t = s.top();
s.pop();
ll x2 = a[s.top()].x - a[i].x, h2 = a[s.top()].h;
if(h1 * x2 > x1 * h2) {
s.push(t);
break;
}
}
ansr[a[i].id].h = s.top();
ansr[a[i].id].x = a[i].x;
}
}
printf("Case #%d:\n", kase);
for(int i = 1; i <= m; i++){
double j1 = 180 * 1.0 / pi * atan2(1.0 * a[ansl[i].h].h, (ansl[i].x - a[ansl[i].h].x));
double j2 = 180 * 1.0 / pi * atan2(1.0 * a[ansr[i].h].h, (a[ansr[i].h].x - ansr[i].x));
printf("%0.10lf\n", 180 - j1 - j2);
}
}
return 0;
}
相关文章推荐
- HDU 5033 Building(2014北京网络赛 单调栈+几何)
- 【HDU】5033 Building 单调栈
- HDU 5033 Building 单调栈
- HDU 5033 Building(2014北京网络赛 单调栈+几何)
- hdu 5033 Building 几何+单调栈
- HDU 5033-B - Building-维护凸包-单调栈
- HDU 5033 Building 单调队列
- HDU 5033 Building 解题报告(维护凸包)
- hdu 5033 Building(单调性+二分)
- HDU - 5033 Building 单调栈维护斜率 好题
- hdu 5033 Building 2014 ACM/ICPC Asia Regional Beijing Online
- HDU 5033 Building
- HDU 5033 Building 暴力+单调栈优化
- HDU 5033 Building(单调栈维护凸包)
- HDU 5033 单调栈
- HDU - 5033 Building
- HDU 5033 - Building
- hdu 5033 Building 2014 ACM/ICPC Asia Regional Beijing Online
- HDU 5033---Building(单调栈)
- HDU 5033 Building (维护单调栈)