http://acm.hdu.edu.cn/showproblem.php?pid=6590
这道题,当时没有看。后来看了一下。看了题很有感觉,但就是不知道怎么不做。
题意:就是找一个问是否存在一个3维向量,使得他给出的每一个x1,x2,y都存在如下式子
成立。
做法:看了题解之后,一下子就懂了,上面那个式子表示为一条直线,而y的值只有-1和1,因此可以理解为存在一条
把y等于1和-1的点集分成两部分。所以就成了,判断两个凸包相交的问题了。
凸包的相交也很暴力,首先判断有没有点在凸包的边上,然后暴力判断有没有线段相交。都是模板。
#include "bits/stdc++.h"
using namespace std;
const double eps = 1e-8;
#define reg register
#define lowbit(x) x&-x
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fi first
#define se second
#define makp make_pair
int dcmp(double x) {
if (fabs(x) < eps) return 0;
return (x > 0) ? 1 : -1;
}
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-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator*(Vector A, double B) { return Vector(A.x * B, A.y * B); }
Vector operator/(Vector A, double B) { return Vector(A.x / B, A.y / B); }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Cross(Vector A, Vector B) { return A.x * B.y - B.x * A.y; }
bool operator==(const Vector A, const Vector B) {
return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;
}
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const ull hash2 = 50331653;
const int N = 100000 + 10;
const int M = 1000 + 10;
const int inf = 0x3f3f3f3f;
const ll mod = 1000000000 + 7;
double xx, yy;
vector<point> v1, v2, pst1, pst2;
struct line {
point a, b;
line(point a, point b) : a(a), b(b) {};
};
bool cmp1(point a, point b) {
if (a.y == b.y) return a.x < b.x;
return a.y < b.y;
}
double xmult(point a, point b, point c) {
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}
double dist(point a, point b) {
return Length(Vector{a.x - b.x, a.y - b.y});
}
bool cmp(point a, point b) {
if (atan2(a.y - yy, a.x - xx) != atan2(b.y - yy, b.x - xx))
return (atan2(a.y - yy, a.x - xx)) < (atan2(b.y - yy, b.x - xx));
return a.x < b.x;
}
bool OnSegment(point p, point &a1, point a2) {
return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}
bool Intersection(point a1, point a2, point b1, point b2) {
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
double DistToSeg(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);
return fabs(Cross(v1, v2)) / Length(v1);
}
void Graham(vector<point> p, vector<point> &pst) {
sort(p.begin(), p.end(), cmp1);
pst.push_back(p[0]);
xx = p[0].x, yy = p[0].y;
sort(p.begin() + 1, p.end(), cmp);
pst.push_back(p[1]);
int top = 1;
for (int i = 2; i < p.size(); i++) {
while (i >= 1 && xmult(pst[top - 1], pst[top], p[i]) < 0) pst.pop_back();
pst.push_back(p[i]);
}
}
int ispoint(point p, vector<point> pp) {
int wn = 0, cc = pp.size();
for (int i = 0; i < cc; i++) {
point p1 = pp[i];
point p2 = pp[(i + 1) % cc];
if (p1 == p || p2 == p || OnSegment(p, p1, p2)) return -1;
int k = dcmp(Cross(p2 - p1, p - p1));
int d1 = dcmp(p1.y - p.y);
int d2 = dcmp(p2.y - p.y);
if (k > 0 && d1 <= 0 && d2 > 0) wn++;
if (k < 0 && d2 <= 0 && d1 > 0) wn--;
}
if (wn != 0) return 1;
return 0;
}
bool judge(const vector<point> p1, const vector<point> p2) {
int c1 = p1.size(), c2 = p2.size();
for (int i = 0; i < c1; i++) {
if (ispoint(p1[i], p2) != 0) return false;
}
for (int i = 0; i < c2; i++) {
if (ispoint(p2[i], p1) != 0) return false;
}
for (int i = 0; i < c1; i++) {
for (int j = 0; j < c2; j++) {
if (Intersection(p1[i], p1[(i + 1) % c1], p2[j], p2[(j + 1) % c2]))
return false;
}
}
return true;
}
int main() {
int T, n;
scanf("%d", &T);
while (T--) {
v1.clear();
v2.clear();
pst1.clear();
pst2.clear();
scanf("%d", &n);
double x1, x2, y1;
for (int i = 1; i <= n; i++) {
scanf("%lf%lf%lf", &x1, &x2, &y1);
if (dcmp(y1 - 1) == 0) {
v1.push_back({x1, x2});
} else {
v2.push_back({x1, x2});
}
}
if (v1.size() > 1)Graham(v1, pst1);
if (v2.size() > 1)Graham(v2, pst2);
if (judge(pst1, pst2)) printf("Successful!\n");
else printf("Infinite loop!\n");
}
return 0;
}