。。也不知道是第几次写这种OJ没人提交,题解搜不到的题了。。
这个题 一开始想法很简单,对于一个点p,找到临界点p1, p2,然后设要求的点
为k 则k = p1 + (p2 - p1) * t;
然后二分t的值,就行了。。
不过问题是,他么输出居然是分数。。
必须重新换个思路,还是要找到两个临界点p1, p2,然后把他看成一个三角形(p, p1, p2)剩余两部分被分割的面积都能算出来,为了让最后两部分面积相等 那么有
等式(L + t * area) = (R + (1-t) * area))成立(L, R 分别为两部分面积)
//
// Created by Matrix on 2015-12-05
// Copyright (c) 2015 Matrix. All rights reserved.
//
//
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <stack>
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x,begin())
#define ll long long
#define CLR(x) memset(x, 0, sizeof x)
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 1e2 + 10;
const int maxv = 1e3 + 10;
const double eps = 1e-9;
int n;
ll lcm(ll a1, ll a2) {
return a1 / __gcd(a1, a2) * a2;
}
struct fen {
ll u, d;
fen(ll _u = 0, ll _d = 1) : u(_u), d(_d) {
ll tmp = __gcd(u, d);
u /= tmp, d /= tmp;
}
void print() {
printf("(%lld/%lld)", u, d);
}
void To() {
if(u == 0) d = 1;
else {
ll tmp = __gcd(u, d);
u /= tmp;
d /= tmp;
}
if(d < 0) {
u *= -1;
d *= -1;
}
}
fen operator+ (ll x) {
ll _u = u + x * d;
ll _d = d;
ll __u = abs(_u);
ll tmp = __gcd(__u, _d);
return fen(_u / tmp, _d / tmp);
}
fen& operator= (const fen& r) {
u = r.u;
d = r.d;
return *this;
}
fen& operator= (const ll& r) {
u = r;
d = 1;
return *this;
}
fen operator- (ll x) {
ll _u = u - x * d;
ll _d = d;
ll __u = abs(_u);
ll tmp = __gcd(__u, _d);
return fen(_u / tmp, _d / tmp);
}
fen operator* (ll x) {
ll tmp = __gcd(x, d);
d /= tmp;
x /= tmp;
return fen(u * x, d);
}
fen operator/ (ll x) {
ll _u = abs(u);
ll tmp = __gcd(_u, x);
u /= tmp;
x /= tmp;
return fen(u, d * x);
}
fen operator+ (fen r) {
ll _d = lcm(d, r.d);
u = _d / d * u;
r.u = _d / r.d * r.u;
return fen(u + r.u, _d);
}
fen operator- (fen r) {
ll _d = lcm(d, r.d);
u = _d / d * u;
r.u = _d / r.d * r.u;
return fen(u - r.u, _d);
}
fen operator* (fen r) {
ll _u = abs(u);
ll __u = abs(r.u);
ll tmp1 = __gcd(_u, r.d);
ll tmp2 = __gcd(d, __u);
u /= tmp1;
r.d /= tmp1;
d /= tmp2;
r.u /= tmp2;
return fen(u * r.u, d * r.d);
}
fen operator/ (fen r) {
swap(r.u, r.d);
return (*this) * r;
}
bool operator== (const fen& r) const{
return u == 0 && r.u == 0 || (u == r.u && d == r.d);
}
bool operator< (const fen& r) const{
return u * r.d < d * r.u;
}
bool operator> (const fen& r) const{
return u * r.d > d * r.u;
}
bool operator<= (const fen& r) const{
return u * r.d <= d * r.u;
}
bool operator>= (const fen& r) const{
return u * r.d >= d * r.u;
}
};
struct Point {
fen x, y;
Point (fen _x, fen _y) : x(_x), y(_y) {}
Point(){}
Point& operator= (const Point& r) {
(*this).x = r.x;
(*this).y = r.y;
return *this;
}
Point operator+ (Point r) {
return Point(x + r.x, y + r.y);
}
Point operator- (Point r) {
return Point(x - r.x, y - r.y);
}
Point operator* (fen r) {
return Point (x * r, y * r);
}
Point operator/ (fen r) {
return Point (x / r, y / r);
}
fen operator* (Point r) {
return x * r.x + y * r.y;
}
fen operator^ (Point r) {
return x * r.y - y * r.x;
}
void print() {
x.print(), y.print();
}
}a[maxn], b[maxn];
typedef Point Vector;
int top;
fen sum;
bool OnSegment(Point p, Point A, Point B) {
fen flag = (p - A) ^ (p - B);
fen flag1 = (p - A) * (p - B);
return flag == fen(0, 1) && flag1 <= fen(0, 1);
}
void sol(Point p) {
int aim;
for(int i = 0; i < n; i++) {
if(OnSegment(p, a[i], a[i+1])) {
aim = (i + 1) % n;
// printf("%d\n", aim);
// a[i].print(); puts("NO.1");
// a[i+1].print();puts("NO.2");
// puts("");
break;
}
}
top = 0;
for(int i = 0; i < n; i++) {
b[top++] = a[aim];
aim = (aim + 1) % n;
}
b[top].x = b[0].x;
b[top].y = b[0].y;
Point p1, p2;
fen l;
for(int i = 0; i < top; i++) {
l = l + ((b[i] - p) ^ (b[i+1] - p));
if(l * 2 - sum >= fen(0, 1)) {
p1 = b[i];
p2 = b[i+1];
break;
top = i + 1;
}
}
fen r = sum - l;
fen area = ((p1 - p) ^ (p2 - p));
l = l - area;
fen t = ((r + area - l) / (area * 2));
Point res = p1 + ((p2 - p1) * t);
res.x.To();
res.y.To();
res.x.print();
printf(",");
res.y.print();
puts("");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
while(scanf("%d", &n) != EOF) {
sum.u = 0, sum.d = 1;
for(int i = 0; i < n; i++) {
ll u, v;
scanf("%lld%lld", &u, &v);
a[i].x = u;
a[i].y = v;
}
a[n].x = a[0].x;
a[n].y = a[0].y;
for(int i = 1; i + 1 < n; i++) {
sum = sum + ((a[i] - a[0]) ^ (a[i+1] - a[0]));
}
// cout << sum << endl;
int m;
scanf("%d", &m);
while(m--) {
Point p;
ll u, v;
scanf("%lld%lld", &u, &v);
p.x = u, p.y = v;
sol(p);
}
}
return 0;
}