ACM-ICPC Live Archive

  三维几何,题意是要求求出两条空间线段的距离。题目难度在于要求用有理数的形式输出,这就要求写一个有理数类了。

  开始的时候写出来的有理数类就各种疯狂乱套,TLE的结果是显然的。后来发现,在计算距离前都是不用用到有理数类的,所以就将开始的部分有理数改成直接用long long。其实好像可以用int来做的,不过我的方法比较残暴,中间运算过程居然爆int了。所以就只好用long long了。

代码如下,附带debug以及各种强的数据:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a;}
template <class T> T sqr(T x) { return x * x;}
typedef long long LL;
struct Rat {
LL a, b;
Rat() {}
Rat(LL x) { a = x, b = ;}
Rat(LL _a, LL _b) {
LL GCD = gcd(_b, _a);
a = _a / GCD, b = _b / GCD;
}
double val() { return (double) a / b;}
bool operator < (Rat x) const { return a * x.b < b * x.a;}
bool operator > (Rat x) const { return a * x.b > b * x.a;}
bool operator == (Rat x) const { return a * x.b == b * x.a;}
bool operator < (LL x) const { return Rat(a, b) < Rat(x);}
bool operator > (LL x) const { return Rat(a, b) > Rat(x);}
bool operator == (LL x) const { return Rat(a, b) == Rat(x);}
Rat operator + (Rat x) {
LL tb = b * x.b, ta = a * x.b + b * x.a;
LL GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
Rat operator - (Rat x) {
LL tb = b * x.b, ta = a * x.b - b * x.a;
LL GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
Rat operator * (Rat x) {
if (a * x.a == ) return Rat(, );
// if (b * x.b == 0) { puts("..."); while (1) ;}
LL tb = b * x.b, ta = a * x.a;
LL GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
Rat operator / (Rat x) {
if (a * x.b == ) return Rat(, );
// if (b * x.a == 0) { puts("!!!"); while (1) ;}
LL GCD, tb = b * x.a, ta = a * x.b;
GCD = gcd(abs(tb), abs(ta));
return Rat(ta / GCD, tb / GCD);
}
void fix() {
a = abs(a), b = abs(b);
LL GCD = gcd(b, a);
a /= GCD, b /= GCD;
}
} ; struct Point {
LL x[];
Point operator + (Point a) {
Point ret;
for (int i = ; i < ; i++) ret.x[i] = x[i] + a.x[i];
return ret;
}
Point operator - (Point a) {
Point ret;
for (int i = ; i < ; i++) ret.x[i] = x[i] - a.x[i];
return ret;
}
Point operator * (LL p) {
Point ret;
for (int i = ; i < ; i++) ret.x[i] = x[i] * p;
return ret;
}
bool operator == (Point a) const {
for (int i = ; i < ; i++) if (!(x[i] == a.x[i])) return false;
return true;
}
void print() {
for (int i = ; i < ; i++) cout << x[i] << ' ';
cout << endl;
}
} ;
typedef Point Vec; struct Line {
Point s, t;
Line() {}
Line (Point s, Point t) : s(s), t(t) {}
Vec vec() { return t - s;}
} ;
typedef Line Seg; LL dotDet(Vec a, Vec b) {
LL ret = ;
for (int i = ; i < ; i++) ret = ret + a.x[i] * b.x[i];
return ret;
} Vec crossDet(Vec a, Vec b) {
Vec ret;
for (int i = ; i < ; i++) {
ret.x[i] = a.x[(i + ) % ] * b.x[(i + ) % ] - a.x[(i + ) % ] * b.x[(i + ) % ];
}
return ret;
} inline LL vecLen(Vec x) { return dotDet(x, x);}
inline bool parallel(Line a, Line b) { return vecLen(crossDet(a.vec(), b.vec())) == ;}
inline bool onSeg(Point x, Point a, Point b) { return parallel(Line(a, x), Line(b, x)) && dotDet(a - x, b - x) < ;}
inline bool onSeg(Point x, Seg s) { return onSeg(x, s.s, s.t);} Rat pt2Seg(Point p, Point a, Point b) {
if (a == b) return Rat(vecLen(p - a));
Vec v1 = b - a, v2 = p - a, v3 = p - b;
if (dotDet(v1, v2) < ) return Rat(vecLen(v2));
else if (dotDet(v1, v3) > ) return Rat(vecLen(v3));
else return Rat(vecLen(crossDet(v1, v2)), vecLen(v1));
}
inline Rat pt2Seg(Point p, Seg s) { return pt2Seg(p, s.s, s.t);}
inline Rat pt2Plane(Point p, Point p0, Vec n) { return Rat(sqr(dotDet(p - p0, n)), vecLen(n));}
inline bool segIntersect(Line a, Line b) {
Vec v1 = crossDet(a.s - b.s, a.t - b.s);
Vec v2 = crossDet(a.s - b.t, a.t - b.t);
Vec v3 = crossDet(b.s - a.s, b.t - a.s);
Vec v4 = crossDet(b.s - a.t, b.t - a.t);
// v1.print();
// v2.print();
// cout << dotDet(v1, v2).val() << "= =" << endl;
return dotDet(v1, v2) < && dotDet(v3, v4) < ;
}// cout << "same plane" << endl; pair<Rat, Rat> getIntersect(Line a, Line b) {
Point p = a.s, q = b.s;
Vec v = a.vec(), u = b.vec();
LL uv = dotDet(u, v), vv = dotDet(v, v), uu = dotDet(u, u);
LL pv = dotDet(p, v), qv = dotDet(q, v), pu = dotDet(p, u), qu = dotDet(q, u);
if (uv == ) return make_pair(Rat(qv - pv, vv), Rat(pu - qu, uu));
// if (vv == 0 || uv == 0 || uv / vv - uu / uv == 0) { puts("shit!"); while (1) ;}
Rat y = (Rat(pv - qv, vv) - Rat(pu - qu, uv)) / (Rat(uv, vv) - Rat(uu, uv));
Rat x = (y * uv - pv + qv) / vv;
// cout << x.a << ' ' << x.b << ' ' << y.a << ' ' << y.b << endl;
return make_pair(x, y);
} void work(Point *pt) {
Line a = Line(pt[], pt[]);
Line b = Line(pt[], pt[]);
if (parallel(a, b)) {
if (onSeg(pt[], b) || onSeg(pt[], b)) { puts("0 1"); return ;}
if (onSeg(pt[], a) || onSeg(pt[], a)) { puts("0 1"); return ;}
// cout << "parallel" << endl;
Rat tmp = min(min(pt2Seg(pt[], b), pt2Seg(pt[], b)), min(pt2Seg(pt[], a), pt2Seg(pt[], a)));
tmp.fix();
printf("%lld %lld\n", tmp.a, tmp.b);
return ;
}
Vec nor = crossDet(a.vec(), b.vec());
Rat ans = pt2Plane(pt[], pt[], nor);
// cout << "~~~" << endl;
if (ans == ) {
// cout << "same plane" << endl;
if (segIntersect(a, b)) { puts("0 1"); return ;}
Rat tmp = min(min(pt2Seg(pt[], b), pt2Seg(pt[], b)), min(pt2Seg(pt[], a), pt2Seg(pt[], a)));
tmp.fix();
printf("%lld %lld\n", tmp.a, tmp.b);
return ;
} else {
// cout << "diff plane" << endl;
pair<Rat, Rat> tmp = getIntersect(a, b);
// cout << tmp.first.val() << "= =" << tmp.second.val() << endl;
// cout << (tmp.first > 0) << endl;
if (tmp.first > && tmp.first < && tmp.second > && tmp.second < ) {
// cout << "cross" << endl;
ans.fix();
printf("%lld %lld\n", ans.a, ans.b);
} else {
// cout << "not cross" << endl;
Rat t = min(min(pt2Seg(pt[], b), pt2Seg(pt[], b)), min(pt2Seg(pt[], a), pt2Seg(pt[], a)));
t.fix();
printf("%lld %lld\n", t.a, t.b);
}
}
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
Point pt[];
int T;
cin >> T;
while (T--) {
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
scanf("%lld", &pt[i].x[j]);
}
}
work(pt);
}
return ;
} /*
13 -20 -20 -20 20 20 19
0 0 0 1 1 1 -20 -20 -20 20 19 20
-20 -20 20 20 20 -20 0 0 0 20 20 20
0 0 10 0 20 10 0 0 0 1 1 1
2 3 4 1 2 2 0 0 0 0 0 0
0 1 1 1 2 3 0 0 0 10 10 10
11 12 13 10 11 11 0 0 0 1 1 1
1 1 1 2 2 2 1 0 0 0 1 0
1 1 0 2 2 0 1 0 0 0 1 0
0 0 0 1 1 0 0 0 0 0 0 20
20 0 10 0 20 10 0 0 0 20 20 20
1 1 2 1 1 2 0 0 0 20 20 20
0 20 20 0 20 20 0 0 0 0 0 20
20 20 0 20 20 20
*/

——written by Lyon

LA 4973 Ardenia (3D Geometry + Simulation)的更多相关文章

  1. uvalive 4973 Ardenia

    题意:给出空间两条线段,求距离. 注意输出格式! #include<cstdio> #include<cmath> #include<algorithm> usin ...

  2. LA 4973异面线段

    题目大意:给两条线段求他们间的最小距离的平方(以分数形式输出). 贴个模版吧!太抽象了. #include<cstdio> #include<cmath> #include&l ...

  3. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  4. Pushing state-of-the-art in 3D content understanding

    Pushing state-of-the-art in 3D content understanding 2019-10-31 06:34:08 This blog is copied from: h ...

  5. LA 4064 Magnetic Train Tracks

    题意:给定平面上$n(3\leq n \leq 1200)$个无三点共线的点,问这些点组成了多少个锐角三角形. 分析:显然任意三点可构成三角形,而锐角三角形不如直角或钝角三角形容易计数,因为后者有且仅 ...

  6. LA 4998 Simple Encryption

    题意:输入正整数$K_1(K_1 \leq 50000)$, 找一个$12$位正整数$K_2$(不能含有前导零)使得${K_1}^{K_2}\equiv K_2(mod10^{12})$. 例如,$K ...

  7. LA 3704 Cellular Automaton

    题意概述: 等价地,本题可以转化为下面的问题: 考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$.求$ ...

  8. 计算机视觉code与软件

    Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...

  9. [OSG]OSG的相关扩展

    参考:osg官网 http://www.osgchina.org/index.php?view=article&id=176 http://trac.openscenegraph.org/pr ...

随机推荐

  1. 杨柳絮-Info:对抗杨柳絮的7种方法和2种防治手段

    ylbtech-杨柳絮-Info:对抗杨柳絮的7种方法和2种防治手段 园林养护人员在对抗杨柳絮上 主要有以下两种方法↓↓ 1.化学方法 化学方法是通过激素等调节剂来抑制植物发芽分化,达到减少杨柳开花的 ...

  2. Linux下的MySQL主从同步

    网上一些关于Linux下的MySQL主从同步教程非常之多,有些很简单的配置却弄的非常复杂,有些根本无法配通,下面是我通过简单的配置完成的主从同步过程,大家可以参考,此文章更适用于新手. 一.测试环境: ...

  3. malloc: *** error for object 0x10a291df8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

    malloc_error_break错误: .You'll find out what the object is when you break in the debugger. Just look ...

  4. laravel 文件

    用laravel处理上传的文件 1.获取上传的文件 $file=$request->file('file');2.获取上传文件的文件名(带后缀,如abc.png) $filename=$file ...

  5. python流程控制和循环

    变量的命名:可以由数字字母下换线组成 ,不能以数字开头,可以使用中文但是不推荐使用中文,不推荐前面使用_ __,不能使用系统的关键字,变量名严格区分大小写 逻辑运算优先级 or<and<n ...

  6. hadoop生态系统默认port集合

    版权声明:本文为博主John Lau原创文章.未经博主同意不得转载 https://blog.csdn.net/GreatElite/article/details/24651569      1 H ...

  7. Directx11教程41 纹理映射(11)

    原文:Directx11教程41 纹理映射(11)     1.第一副图我们采用各性异性的滤波方式,并设置最大各性异性值为8.     samplerDesc.Filter =  D3D11_FILT ...

  8. vuxdemo1

    //main.js import Vue from 'vue' import router from './router' import store from './store' import axi ...

  9. JavaScript--for in循环访问属性用"."和[ ]的区别

    // for in 循环遍历对象的时候// 内部要访问属性的时候不能点语法访问,因为for in 的key是字符串格式// 可通过方括号实现访问 for(var key in manObj) { co ...

  10. LeedCode OJ --- Binary Tree Inorder Traversal

    点击打开题目链接 今天只是写了递归的版本,因为还没想好怎么用迭代来实现,可以写的过程中,有一点是有疑问的,虽然我的代码可以AC. 问题是:主调函数是可以使用子函数中返回的在子函数中定义的vector. ...