题意
给出n条直线,求从上到下能看到直线的编号。
分析
对于每两条直线,都有一条直线被另一条直线遮住一部分。
设两条直线Y1= k1X + b1, Y2 = k2X + b2(K1 < K2)。
以及第三条直线Y3 = k3X + b3;
可以算出前两条直线的交点的横坐标为X = (b2-b1)/(k1 - k2)
由于k2 > k1,所以当x<X时,Y1遮住Y2,当x>X时,Y2遮住Y1。
而第三条直线分别和Y1,Y2的交点横坐标为X' = (b3 - b1) / (k1 - k3), X'' = (b3 - b1) / (k1 - k3)
同理,由于k3 > k1,所以当x<X'时,Y1遮住Y3,当x>X'时,Y3遮住Y1。
由于k3 > k2,所以当x<X''时,Y2遮住Y3,当x>X''时,Y3遮住Y2。
所以当X''<X'时,X''<X'<X,则x<X部分Y2被Y1遮住,x>''部分Y2被Y3遮住。于是Y2被完全遮住。
于是就将直线按k从小到大排序,对于每一条直线判断其能否遮住前一条。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<stack>
using namespace std;
struct node
{
int k, b, tru, no;
node()
{
tru = 0;
}
friend bool operator <(node a, node b)
{
if (a.k == b.k)
return a.b < b.b;
return a.k < b.k;
}
friend bool operator ==(node a, node b)
{
return a.k == b.k;
}
void Write()
{
cout << k << " " << b << endl;
}
}p[500010], no[500010];
int n;
stack<int> c;
void Read()
{
cin >> n;
for (int i = 1;i <= n; i++)
{
scanf("%d%d", &p[i].k, &p[i].b);
p[i].no = i;
no[i] = p[i];
}
}
int check(int a, int x, int y)
{
double x1 = (p[a].b - p[x].b) * 1. / (p[x].k - p[a].k);
double x2 = (p[a].b - p[y].b) * 1. / (p[y].k - p[a].k);
if (x1 <= x2) return 1;
return 0;
}
void work()
{
int fir, sec;
for (int i = 1;i <= n; i++)
{
while (!c.empty() && p[i] == p[c.top()]) c.pop();
if (c.size() < 2)
{
c.push(i);
continue;
}
while (!c.empty())
{
fir = c.top();
c.pop();
if (c.empty())
{
c.push(fir);
break;
}
if (p[fir] == p[i]) continue;
sec = c.top();
if (!check(i, fir, sec))
{
c.push(fir);
break;
}
}
c.push(i);
}
while (!c.empty())
{
no[p[c.top()].no].tru = 1;
c.pop();
}
for (int i = 1;i <= n; i++)
if (no[i].tru)
{
printf("%d ", i);
}
}
int main(void)
{
freopen("lines.in","r",stdin);
freopen("lines.out","w",stdout);
Read();
sort(p + 1, p + 1 + n);
work();
fclose(stdin);
fclose(stdout);
return 0;
}