007: [HNOI2008]水平可见直线
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 6614 Solved: 2519
[ 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
-1 0
1 0
0 0
Sample Output
1 2
题解:需要维护一个上凹的这么一个图形,从上往下直观的看到几个拐点,就应该是n-1条直线。
1.按斜率第一关键字,截距第二关键字排序。2.需要一个单调栈来维护,每当该斜率的直线需要入队列时。
需要判断 x1 : (i, top), x2 : (top, top-1)这两个交点的情况。当 x1 <= x2 的时候要不断pop;
代码:
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
#define rep(i,a,b) for(int i = a;i <= b;++ i)
#define per(i,a,b) for(int i = a;i >= b;-- i)
#define mem(a,b) memset((a),(b),sizeof((a)))
#define FIN freopen("in.txt","r",stdin)
#define FOUT freopen("out.txt","w",stdout)
#define IO ios_base::sync_with_stdio(0),cin.tie(0)
#define mid ((l+r)>>1)
#define ls (id<<1)
#define rs ((id<<1)|1)
#define N 50005
#define INF 0x3f3f3f3f
#define INFF ((1LL<<62)-1)
#define mod 998244353
typedef long long ll;
using namespace std;
int n,q[N],ans[N]; // q[n] = [l, r);
struct Node{
int a,b,id;
bool operator < (const Node &r) const {
if(a == r.a) return b > r.b;
return a < r.a;
}
}node[N];
double cal(int x, int y){
double ans = (node[y].b-node[x].b)*1.0/(node[x].a-node[y].a);
return ans;
}
int main()
{IO;
//FIN;
while(cin >> n){
rep(i, 1, n) { cin >> node[i].a >> node[i].b; node[i].id = i; }
sort(node+1, node+n+1);
int top = 0;
rep(i, 1, n){
if(top >= 2){
while(top >= 2){
double x = cal(i, q[top-1]), X = cal(q[top-1], q[top-2]);
if(x <= X) top--;
else
break;
}
}
q[top++] = i;
}
int len = 0;
rep(i, 0, top-1) ans[len++] = node[q[i]].id;
sort(ans, ans+len);
rep(i, 0, len-1) cout << ans[i] << " ";
}
return 0;
}
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <set>
#include <map>
#define rep(i,a,b) for(int i = a;i <= b;++ i)
#define per(i,a,b) for(int i = a;i >= b;-- i)
#define mem(a,b) memset((a),(b),sizeof((a)))
#define FIN freopen("in.txt","r",stdin)
#define FOUT freopen("out.txt","w",stdout)
#define IO ios_base::sync_with_stdio(0),cin.tie(0)
#define mid ((l+r)>>1)
#define ls (id<<1)
#define rs ((id<<1)|1)
#define N 50005
#define INF 0x3f3f3f3f
#define INFF ((1LL<<62)-1)
#define mod 998244353
typedef long long ll;
using namespace std;
int n,q[N],ans[N]; // q[n] = [l, r);
struct Node{
int a,b,id;
bool operator < (const Node &r) const {
if(a == r.a) return b > r.b;
return a < r.a;
}
}node[N];
double cal(int x, int y){
double ans = (node[y].b-node[x].b)*1.0/(node[x].a-node[y].a);
return ans;
}
int main()
{IO;
//FIN;
while(cin >> n){
rep(i, 1, n) { cin >> node[i].a >> node[i].b; node[i].id = i; }
sort(node+1, node+n+1);
int top = 0;
rep(i, 1, n){
if(top >= 2){
while(top >= 2){
double x = cal(i, q[top-1]), X = cal(q[top-1], q[top-2]);
if(x <= X) top--;
else
break;
}
}
q[top++] = i;
}
int len = 0;
rep(i, 0, top-1) ans[len++] = node[q[i]].id;
sort(ans, ans+len);
rep(i, 0, len-1) cout << ans[i] << " ";
}
return 0;
}