Building
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 168 Accepted Submission(s): 24
Special Judge
Problem 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.
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).
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
Source
今天真的彻底醉了,逗比一下午,四个多小时,全跪在这个题上,真想打人233233233233233233233。,。,。,。,。,。,。,。
很明显是单调栈,一开始要维护的东西没想清加上代码写的挫,怎么调怎么和暴力对拍都改不对。,,,。,。,。
就是维护相邻的建筑物的斜率应该是递增的,很简单。,。,,,。我是把所有的坐标都跑了,时间略慢
不多说,上代码:
/**
* @author neko01
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int N=10000005;
int h[N];
int sta[N];
double f1[N];
double f2[N];
int minx,maxx;
bool judge(int x,int y,int z)
{
return fabs(((h[x]-h[y])*1.0/(x-y)))>=fabs(((h[y]-h[z])*1.0/(y-z)));
}
void init()
{
int top=0;
for(int i=minx;i<=maxx;i++)
{
if(h[i]>0)
{
while(top>0&&h[i]>=h[sta[top-1]]) top--;
while(top>1&&judge(sta[top-2],sta[top-1],i)) top--;
sta[top++]=i;
}
else
{
while(top>1&&judge(sta[top-2],sta[top-1],i)) top--;
f1[i]=(i-sta[top-1])*1.0/h[sta[top-1]];
}
}
top=0;
for(int i=maxx;i>=minx;i--)
{
if(h[i]>0)
{
while(top>0&&h[i]>=h[sta[top-1]]) top--;
while(top>1&&judge(sta[top-2],sta[top-1],i)) top--;
sta[top++]=i;
}
else
{
while(top>1&&judge(sta[top-2],sta[top-1],i)) top--;
f2[i]=(sta[top-1]-i)*1.0/h[sta[top-1]];
}
}
}
double gao(int x)
{
return (atan(f1[x])+atan(f2[x]))*180.0/pi;
}
int main()
{
int t,cnt=0;
scanf("%d",&t);
while(t--)
{
int n,q;
scanf("%d",&n);
clr(h);
minx=N,maxx=0;
for(int i=0;i<n;i++)
{
int x,hh;
scanf("%d%d",&x,&hh);
if(x>maxx) maxx=x;
if(x<minx) minx=x;
h[x]=hh;
}
init();
scanf("%d",&q);
printf("Case #%d:\n",++cnt);
while(q--)
{
int x;
scanf("%d",&x);
printf("%.6lf\n",gao(x));
}
}
return 0;
}