题目链接:I Mr. Main and Windmills
题解
题目有一句关键点,给的任意三个点不共线,所以我们可以将其它点与查询点连线交路线线段于各个点(没有交上的点排除在外),将交于路线线段的点按起始终点位置排序,找第k个即可,没有则输出-1。
(题目来源于生活,想起坐高铁时看到电线杆的移动情况,本题自然而然的就好理解了)
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x
const double PI=acos(-1.0);
const double eps=1e-8;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e3+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) { }
};
typedef Point Vector;
Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y+B.y); }
Vector operator - (Point A,Point B) { return Vector(A.x-B.x,A.y-B.y); }
Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); }
Vector operator / (Vector A,double p) { return Vector(A.x/p,A.y/p); }
bool operator < (const Point& a,const Point& b) { return a.x<b.x || (a.x==b.x && a.y<b.y); }
int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; }
bool operator == (const Point& a,const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y)==0; }
double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; }
double Dot(Vector A,Vector B) { return A.x*B.x+A.y+B.y; }
double Length(Vector A) { return sqrt(Dot(A,A)); }
Point GetLine(Point p,Vector v,Point Q,Vector w)
{
Vector u = p-Q;
double t = Cross(w,u)/Cross(v,w);
return p+v*t;
}
double dis(Point P,Point A,Point B)
{
if(A==B) return Length(P-A);
Vector v1=B-A,v2=P-A,v3=P-B;
if(dcmp(Dot(v1, v2))<0) return Length(v2);
else if(dcmp(Dot(v1, v3))>0) return Length(v3);
else return fabs(Cross(v1, v2)) / Length(v1);
}
bool Onsegment(Point p,Point a1,Point a2) {
if(dcmp(p.x-a1.x)==0 && dcmp(p.y-a1.y)==0) return true;
if(dcmp(p.x-a2.x)==0 && dcmp(p.y-a2.y)==0) return true;
return dcmp(Dot(a1-p, a2-p))<0;
}
Point s,t;
bool cmp(Point a,Point b) { return Length(a-s)<=Length(b-s); }
Point a[maxn];
vector<Point> p;
int main()
{
int n,m,h,k;
scanf("%d%d",&n,&m);
scanf("%lf%lf%lf%lf",&s.x,&s.y,&t.x,&t.y);
for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
while (m--) {
scanf("%d%d",&h,&k);
p.clear();
for(int i=1;i<=n;i++) {
if(i==h) continue;
Vector v=Vector(a[i].x-a[h].x,a[i].y-a[h].y);
Vector w=Vector(s.x-t.x,s.y-t.y);
if(Cross(v, w)==0) continue;
Point tp=GetLine(a[h], v, s, w);
if(Onsegment(tp, s, t)) p.push_back(tp);
}
sort(p.begin(),p.end(),cmp);
if(p.size()<k) printf("-1\n");
else printf("%.10lf %.10lf\n",p[k-1].x,p[k-1].y);
}
return 0;
}