今天讲了计算几何,发几道水水的tyvj上的题解...

计算几何好难啊!@Mrs.General....怎么办....

这几道题都是在省选之前做的,所以前面的Point运算啊,dcmp啊,什么什么的,基本上没用,每次都把上次的main()函数删了接着继续写....

原谅我曾经丑出翔的代码...

虽然现在也很丑...

TYVJ 1150 绳子围点

题解:

凸包 + 凸包面积 + 皮克定理

 #include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; const int MAXN = + ; char cj;
int n, m; struct Point{//{{{
ll x, y; friend bool operator < (const Point& A, const Point& B){
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
} p[MAXN], ch[MAXN * ];//}}} inline ll xmult(Point a, Point b, Point c){//{{{
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
} inline void AndrweScan(){
sort(p, p + n);
m = , ch[] = p[], ch[] = p[];
for (int i = ; i < n; i ++){
while (m >= && xmult(ch[m], ch[m - ], p[i]) > )
m --;
ch[++ m] = p[i];
} for (int i = n - ; i >= ; i --){
while (m >= && xmult(ch[m], ch[m - ], p[i]) > )
m --;
ch[++ m] = p[i];
}
}//}}} inline ll nextLL(){
bool flag = false;
do {
cj = getchar();
if (cj == '-')
flag = true;
} while (!( <= cj && cj <= )); ll ret = ;
do {
ret = ret * + cj - ;
cj = getchar();
} while ( <= cj && cj <= ); return flag ? -ret : ret;
} inline int nextInt(){
do
cj = getchar();
while (!( <= cj && cj <= )); int ret = ;
do {
ret = ret * + cj - ;
cj = getchar();
} while ( <= cj && cj <= ); return ret;
} inline ll Plot(){
ll ret = ;
for (int i = ; i < m; i ++)
ret += xmult(ch[], ch[i - ], ch[i]);
return ret;
} inline ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
} inline ll Cal(int i){
int j = (i == m - ) ? : i + ,
dx = ch[i].x - ch[j].x, dy = ch[i].y - ch[j].y;
return gcd(abs(dx), abs(dy));
} inline void Pick(){
ll dS = Plot(), b = , a;
for (int i = ; i < m; i++)
b += Cal(i);
a = (dS - b + ) / ;
printf("%lld\n", a + b);
} int main(){
n = nextInt();
for (int i = ; i < n; i++)
p[i].x = nextLL(), p[i].y = nextLL();
AndrweScan();
Pick();
}

TYVJ 1543 房间最短路

题解:

Floyd,dp[i][j]表示到第i面墙的第j个点的最短路,注意在更新最小值的时候判断两个点的连通

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define INF 100000000
#define eps 1e-9 const int MAXN = ; int n;
double dp[MAXN][MAXN]; struct Point{
double x, y; friend Point operator + (const Point A, const Point B){
return (Point){A.x+B.x, A.y+B.y};
} friend Point operator - (const Point A, const Point B){
return (Point){A.x-B.x, A.y-B.y};
} friend Point operator * (const Point A, const Point B){
return (Point){A.x*B.x, A.y*B.y};
} friend Point operator / (const Point A, const Point B){
return (Point){A.x/B.x, A.y/B.y};
}
}p[MAXN][MAXN]; inline Point make_Point(double X, double Y){
return (Point){X, Y};
} inline double len(Point A){
return sqrt(A.x*A.x+A.y*A.y);
} inline bool In_region(double eg, double l, double r){
if (l>r) swap(l, r);
return (l-eps<=eg && eg<=r+eps);
} inline bool Connected(int I1, int I2, int I3, int I4){
if (I1>I3) swap(I1, I3);
if ((I1==I3 && I2==I4) || I3-I1==) return true;
double k = (p[I1][I2].y-p[I3][I4].y)/(p[I1][I2].x-p[I3][I4].x), b = p[I1][I2].y-p[I1][I2].x*k, bn;
for (int i=I1+; i<I3; i++){
bn = p[i][].x*k+b;
if (!In_region(bn, p[i][].y, p[i][].y) && !In_region(bn, p[i][].y, p[i][].y)) return false;
}
return true;
} int main(){
scanf("%d", &n); p[][] = p[][] = p[][] = p[][] = make_Point(, );
p[n+][] = p[n+][] = p[n+][] = p[n+][] = make_Point(, ); double x, in, apl;
for (int i=; i<=n; i++){
scanf("%lf", &x);
for (int j=; j<; j++)
scanf("%lf", &in), p[i][j] = make_Point(x, in);
} //init
for (int i=; i<; i++)
dp[][i] = 0.0;
for (int i=; i<=n+; i++)
for (int j=; j<; j++)
dp[i][j] = Connected(, , i, j) ? len(p[i][j]-p[][]) : INF; for (int i=; i<=n+; i++)
for (int j=; j<; j++)
for (int k=; k<i; k++)
for (int l=; l<; l++)
if (Connected(k, l, i, j)) dp[i][j] = min(dp[i][j], dp[k][l]+len(p[i][j]-p[k][l]));
apl = INF;
for (int i=; i<; i++)
apl = min(apl, dp[n+][i]);
printf("%.2lf\n",apl);
return ;
}

TYVJ 1462 凸多边形

 #include <cstdio>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define eps 1e-12
using namespace std; const int MAXN = +; int n; struct Point{
long double x, y; friend Point operator - (const Point& A, const Point& B){
return (Point){A.x-B.x, A.y-B.y};
} friend long double operator * (const Point& A, const Point& B){//cross
return A.x*B.y-A.y*B.x;
} friend bool operator < (const Point& A, const Point& B){
return (A.x!=B.x) ? A.x<B.x : A.y<B.y;
} inline void print(){
cout<<setiosflags(ios::fixed)<<setprecision()<<x<<" "<<setiosflags(ios::fixed)<<setprecision()<<y<<"\n";
}
}p[MAXN], ch[MAXN]; inline int dcmp(long double eg){
if (eg>eps) return ; //>0
if (eg<-eps) return -;//<0
else return ;
} inline void GrahamScan(){
int tot, t;
sort(p, p+n); ch[] = p[], ch[] = p[], tot = ;
for (int i=; i<n; i++){
while (tot> && dcmp((ch[tot]-ch[tot-])*(p[i]-ch[tot-]))!=-) tot--;
ch[++tot] = p[i];
} t = tot, ch[++tot] = p[n-];
for (int i=n-; i>=; i--){
while (tot>t && dcmp((ch[tot]-ch[tot-])*(p[i]-ch[tot-]))!=-) tot--;
ch[++tot] = p[i];
} for (int i=; i<tot; i++)
ch[i].print();
} int main(){
scanf("%d", &n);
for (int i=; i<n; i++)
cin>>p[i].x>>p[i].y; GrahamScan();
return ;
}

TYVJ 1523 神秘大三角

题解:

这道题考的是读入...面积随便整一下就好了

 #include <cstdio>
#include <cmath>
#include <algorithm>
#define eps 1e-9
using namespace std; const int MAXL = ; struct Point{
double x,y; inline void in(){
char s[MAXL];
scanf("%s",s);
int i;
x = ,y = ;
for (i=;;i++){
if (s[i]==',') break;
x *= ,x += s[i]-;
}
for (i+=;;i++){
if (s[i]==')') break;
y *= ,y += s[i]-;
} } inline void out(){
printf("%.2lf %.2lf\n",x,y);
} inline bool is_zero(){
return (y== && x==);
} inline double len(){
return sqrt(x*x-y*y);
} friend Point operator + (const Point A,const Point B){
return (Point){A.x+B.x,A.y+B.y};
} friend Point operator - (const Point A,const Point B){
return (Point){A.x-B.x,A.y-B.y};
} friend Point operator * (const Point A,const double B){
return (Point){A.x*B,A.y*B};
} friend Point operator / (const Point A,const double B){
return (Point){A.x/B,A.y/B};
} friend bool operator == (const Point A,const Point B){
return (A.x==B.x && A.y==B.y);
}
}A,B,C,D; inline double area(Point p1,Point p2,Point p3){
return abs(0.5*(p1.x*p2.y+p2.x*p3.y+p3.x*p1.y-p1.y*p2.x-p2.y*p3.x-p3.y*p1.x));
} inline bool on(Point Aa,Point Ba,Point q){
double MX = max(Aa.x,Ba.x);
double mx = min(Aa.x,Ba.x);
double MY = max(Aa.y,Ba.y);
double my = min(Aa.y,Ba.y);
return ((mx<q.x && q.x<MX) && (my<q.y && q.y<MY)) ? true : false;
} inline bool zero(double eg){
return (eg>-eps && eg<eps);
} inline int appleeeeeeee(){
A.in();B.in();C.in();D.in();
if (A==D || B==D || C==D) return ;//顶点上
double s1,s2,s3,s;
s = area(A,B,C);
s1 = area(A,B,D);
s2 = area(A,C,D);
s3 = area(B,C,D); if (zero(s1) && on(A,B,D)) return ;
if (zero(s2) && on(A,C,D)) return ;
if (zero(s3) && on(B,C,D)) return ;//边上 if ((s1+s2+s3-s)<eps && (s1+s2+s3-s)>-eps) return ;
else return ;
} int main(){
printf("%d",appleeeeeeee());
return ;
}

TYVJ 1544 角平分线

题解:

推个公式就好了

 #include <cstdio>
#include <cmath> struct Point{
double x,y; inline void in(){
scanf("%lf%lf",&x,&y);
} inline void out(){
printf("%.2lf %.2lf",x,y);
} friend Point operator + (const Point A,const Point B){
return (Point){A.x+B.x,A.y+B.y};
} friend Point operator - (const Point A,const Point B){
return (Point){A.x-B.x,A.y-B.y};
} friend Point operator * (const Point A,const double B){
return (Point){A.x*B,A.y*B};
} friend Point operator / (const Point A,const double B){
return (Point){A.x/B,A.y/B};
}
}a,b,c,d; inline double len(Point eg){
return sqrt(eg.x*eg.x+eg.y*eg.y);
} int main(){
a.in();b.in();c.in();
b = b-a,c = c-a;
double AB = len(b),AC = len(c);
double cj = (AB*AC)/(AB+AC);
d = b/AB + c/AC;
d = d*cj;
d = d + a;
d.out();
return ;
}

TYVJ计算几何的更多相关文章

  1. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  2. [BZOJ3224]Tyvj 1728 普通平衡树

    [BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...

  3. ACM/ICPC 之 计算几何入门-叉积-to left test(POJ2318-POJ2398)

    POJ2318 本题需要运用to left test不断判断点处于哪个分区,并统计分区的点个数(保证点不在边界和界外),用来做叉积入门题很合适 //计算几何-叉积入门题 //Time:157Ms Me ...

  4. HDU 2202 计算几何

    最大三角形 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  6. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9629  Solved: 4091[Submit][Sta ...

  7. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

  8. TYVJ P1080 N皇后

    描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 列号  1  2  3  4  5  6 -- ...

  9. ACM 计算几何中的精度问题(转)

    http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...

随机推荐

  1. Spark ML 文本的分类

    最近一直在研究Spark的分类算法,因为我们是做日志文本分类,在官网和各大网站一直没找到相应的Demo,经过1个多月的研究,终于有点成效. val sparkConf = new SparkConf( ...

  2. mybatis配置优化

    1.加入日志log4j 1)加入jar包:log4j-1.2.17.jar 2)加入log4j配置文件: 可以使properties或者xml形式 log4j.rootLogger = DEBUG,C ...

  3. impala简单使用

    impala-shell connect ha1:21000 更新元信息 invalidate metadata;

  4. HTML中行内元素与块级元素的区别

    块级元素:独占一行,可设宽高,内外边距:块级元素有form,p,h1到h6,ol ,ul ,dl和dd和dt ,hr,li,pre,caption ,div ,table ,tr ,td ,th等. ...

  5. UIAutomator 编译

    环境搭建 1.必备条件: 1.JDK    2.SDK(API高于15)    3.Eclipse(安装ADT插件)    4.ANT(用于编译生成jar) 2.简要步骤: 1.安装JDK并添加环境变 ...

  6. SQL注入原理

    随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于这个行业的入门门槛不高,程序员的水平及经验也参差不齐,相当大一 部分程序员在编写代码的时候,没有对用户输入数据的合法性 ...

  7. mysql主从复制的一篇文章(转载)

      管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本的主从,本博文属于总结性的,有一部分是摘自网络,大部分是根据自己管理的心得和经验所写,整理了一下,分享给各位同行,希 ...

  8. HttpUtility.UrlEncode

  9. UART UVM验证平台平台搭建总结

    tb_top是整个UVM验证平台的最顶层:tb_top中例化dut,提供时钟和复位信号,定义接口以及设置driver和monitor的virual interface,在intial中调用run_te ...

  10. sql基础知识(新手必备)

    一.简单查询 1.查询所有数据,查询部分列数据,列别名 SELECT * FROM 表名 SELECT 列1 AS 'BIAOTI1','BIAOTI2'=列2  FROM 表名 2.查询不重复的数据 ...