1007: [HNOI2008]水平可见直线
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7378 Solved: 2829
[ Submit][ Status][ Discuss]
Description
在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为
可见的,否则Li为被覆盖的.
例如,对于直线:
L1:y=x; L2:y=-x; L3:y=0
则L1和L2是可见的,L3是被覆盖的.
给出n条直线,表示成y=Ax+B的形式(|A|,|B|<=500000),且n条直线两两不重合.求出所有可见的直线.
Input
第一行为N(0 < N < 50000),接下来的N行输入Ai,Bi
Output
从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必须有个空格
Sample Input
3
-1 0
1 0
0 0
Sample Output
1 2
单调栈,所有直线按斜率从小到大排序,如果当前直线与栈顶直线的交点在栈顶两直线交点左边,
则栈顶直线一定不可见,弹出栈顶
http://www.lydsy.com/JudgeOnline/problem.php?id=1007
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct Res
{
double k;
double b;
int id;
bool operator < (const Res &c) const
{
if(k<c.k || k==c.k && b<c.b)
return 1;
return 0;
}
}Res;
Res s[50005];
int st[50005], ans[50005];
int main(void)
{
int n, i, top;
double nowx, dx;
while(scanf("%d", &n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%lf%lf", &s[i].k, &s[i].b);
s[i].id = i;
}
sort(s+1, s+n+1);
top = 0;
for(i=1;i<=n;i++)
{
if(i<=n-1 && s[i].k==s[i+1].k)
continue;
while(top>=2)
{
nowx = (s[st[top]].b-s[i].b)/(s[i].k-s[st[top]].k);
dx = (s[st[top-1]].b-s[st[top]].b)/(s[st[top]].k-s[st[top-1]].k);
if(nowx<=dx)
top--;
else
break;
}
st[++top] = i;
}
memset(ans, 0, sizeof(ans));
for(i=1;i<=top;i++)
ans[s[st[i]].id] = 1;
for(i=1;i<=n;i++)
{
if(ans[i]==1)
printf("%d ", i);
}
printf("\n");
}
return 0;
}