第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)I-Mr. Main and Windmills 计算几何

I-Mr. Main and Windmills
在这里插入图片描述

SOLUTION

所选点与其余点的连线与已知线段st若有的交点,则被记录,依照靠近起始点st.s排序,若无k个交点输出-1,若有输出_p[k]

CODE

/*SiberianSquirrel*/
/*CuteKiloFish*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <bitset>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <time.h>
#include <iomanip>
//#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define re register
typedef long long ll;
typedef pair<ll, ll> PII;
typedef unsigned long long ull;

const double eps = 1e-7;
const double pi = acos(-1.0);
const int N = 1e6 + 10, M = 1e6 + 5, INF = 0x3f3f3f3f;
const int MOD = 1e9+7;

int sgn(double x) {
    if(fabs(x) < eps) return 0;
    if(x > 0) return 1;
    return -1;
}

struct Point {
    double x, y;
    Point(){}
    Point(double _x, double _y) {
        x = _x;
        y = _y;
    }
    void out() {
        printf("%.10f %.10f\n", x, y);
    }
    double operator*(const Point &a) const {
        return x * a.x + y * a.y;
    }
    double operator^(const Point &a) const {
        return x * a.y - y * a.x;
    }
    Point operator-(const Point &a) const {
        return Point(x - a.x, y - a.y);
    }
    Point operator+(const Point &a) const {
        return Point(x + a.x, y + a.y);
    }
    double dis(const Point &a) const {
        return sqrt((x - a.x) * (x - a.x) + (y - a.y) * (y - a.y));
    }
}p[1010], s, t, _p[1000010];

struct Line {
    Point s, e;
    Line(){}
    Line(Point _s, Point _e) {
        s = _s;
        e = _e;
    }
    Line(double a, double b, double c) {
        if(sgn(a) == 0) {
            s = Point(0, -c / b);
            e = Point(1, -c / b);
        } else if(sgn(b) == 0) {
            s = Point(-c / a, 0);
            e = Point(-c / a, 1);
        } else {
            s = Point(0, -c / b);
            e = Point(1, (-c - a) / b);
        }
    }
    void out() {
        s.out();
        e.out();
    }
    Point crosspoint(Line v) {
        double a1 = (v.e - v.s) ^ (s - v.s);
        double a2 = (v.e - v.s) ^ (e - v.s);
        return Point((s.x * a2 - e.x * a1) / (a2 - a1), (s.y * a2 - e.y * a1) / (a2 - a1));
    }
}st, lt;

int n, m, h, k;

bool cmp(Point a, Point b) {
    return st.s.dis(a) < st.s.dis(b);
}

bool check(Point x) {
    return (sgn(max(st.e.x, st.s.x) - x.x) >= 0 && sgn(min(st.e.x, st.s.x) - x.x) <= 0 && sgn(max(st.e.y, st.s.y) - x.y) >= 0 && sgn(min(st.e.y, st.s.y) - x.y) <= 0);
}

void solve() {
    scanf("%d%d", &n, &m);
    scanf("%lf%lf%lf%lf", &st.s.x, &st.s.y, &st.e.x, &st.e.y);

    for(int i = 1; i <= n; ++ i) scanf("%lf%lf", &p[i].x, &p[i].y);

    while (m --) {
        scanf("%d%d", &h, &k);
        for(int i = 1; i <= n; ++ i) {
            if(i != h) {
                lt.s = p[h];
                lt.e = p[i];
                Point temp = st.crosspoint(lt);
                if(check(temp)) _p[++pos] = st.crosspoint(lt); 
            }
        }
        sort(_p + 1, _p + 1 + pos, cmp);
        if(pos < k) printf("-1\n");
        else _p[k].out();
    }

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    //freopen("out.txt", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值