返回上一章 Premier Bob的算法模板库(I)


Geometry_Calculate_Basic .hpp (计算几何基础运算)

#include<cmath>
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;namespace Geometry_Calculate_Basic
{const double PI=acos(-1.0);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-(Point A,Point B){return Point(A.x-B.x,A.y-B.y);}Vector operator*(Vector A,double p){return Vector(A.x*p,A.y*p);}Vector operator/(Vector A,double p){return Vector(A.x/p,A.y/p);}bool operator<(const Point& a, const Point& b){return a.x<b.x || (a.x==b.x && a.y<b.y);}const double eps=1e-10;int dcmp(double x){if(fabs(x)<eps)return 0;else return x<0 ? -1 : 1;}bool operator==(const Point& a,const Point& b){return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;}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 Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}typedef double ArcAngle;Vector Rotate(Vector A,ArcAngle rad){return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}Vector Normal(Vector A){double L=Length(A);return Vector(-A.y/L,A.x/L);}Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){Vector u=P-Q;double t=Cross(w,u)/Cross(v,w);return P+v*t;}double DistanceToLine(Point P,Point A,Point B){Vector v1=B-A,v2=P-A;return fabs(Cross(v1,v2))/Length(v1);}double DistanceToSegment(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);}Point GetLineProjection(Point P,Point A,Point B){Vector v=B-A;return A+v*(Dot(v,P-A)/Dot(v,v));}bool SegmentProperIntersection(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 PolygonArea(Point* p,int n){double area=0;for(int i=1;i<n-1;i++)area+=Cross(p[i]-p[0],p[i+1]-p[0]);return area/2;}
}

但愿这个不是使用样例:

#include"Geometry_Calculate_Basic.hpp"
int main()
{int T;cin>>T;for(int i=1;i<=T;i++){Geometry_Calculate_Basic::Point A,B,C;cin>>A.x>>A.y>>B.x>>B.y>>C.x>>C.y;Geometry_Calculate_Basic::ArcAngle c_a=Geometry_Calculate_Basic::Angle(A-B,C-B);Geometry_Calculate_Basic::ArcAngle a_b=Geometry_Calculate_Basic::Angle(B-C,A-C);Geometry_Calculate_Basic::ArcAngle b_c=Geometry_Calculate_Basic::Angle(C-A,A-B);Geometry_Calculate_Basic::Vector a1,a2,b1,b2,c1,c2;a1=Geometry_Calculate_Basic::Rotate(C-A,b_c/3*2);a2=Geometry_Calculate_Basic::Rotate(C-A,b_c/3);b1=Geometry_Calculate_Basic::Rotate(A-B,c_a/3*2);b2=Geometry_Calculate_Basic::Rotate(A-B,c_a/3);c1=Geometry_Calculate_Basic::Rotate(B-C,a_b/3*2);c2=Geometry_Calculate_Basic::Rotate(B-C,a_b/3);Geometry_Calculate_Basic::Point D,E,F;D=Geometry_Calculate_Basic::GetLineIntersection(B,b1,C,c2);E=Geometry_Calculate_Basic::GetLineIntersection(A,a1,B,b2);F=Geometry_Calculate_Basic::GetLineIntersection(C,c1,A,a2);cout<<D.x<<" "<<D.y<<" "<<E.x<<" "<<E.y<<" "<<F.x<<" "<<F.y<<endl;}return 0;
}

所以说应该写成这样:

#include"Geometry_Calculate_Basic.hpp"
using namespace Geometry_Calculate_Basic;
//using namespace!
int main()
{int T;cin>>T;for(int i=1;i<=T;i++){Point A,B,C;cin>>A.x>>A.y>>B.x>>B.y>>C.x>>C.y;ArcAngle c_a=Angle(A-B,C-B);ArcAngle a_b=Angle(B-C,A-C);ArcAngle b_c=Angle(C-A,A-B);Vector a1,a2,b1,b2,c1,c2;a1=Rotate(C-A,b_c/3*2);a2=Rotate(C-A,b_c/3);b1=Rotate(A-B,c_a/3*2);b2=Rotate(A-B,c_a/3);c1=Rotate(B-C,a_b/3*2);c2=Rotate(B-C,a_b/3);Point D,E,F;D=GetLineIntersection(B,b1,C,c2);E=GetLineIntersection(A,a1,B,b2);F=GetLineIntersection(C,c1,A,a2);cout<<D.x<<" "<<D.y<<" "<<E.x<<" "<<E.y<<" "<<F.x<<" "<<F.y<<endl;}return 0;
}

NlogN_LIS .hpp (nlogn时间复杂度求解LIS长度)

#include<algorithm>
using namespace std;namespace NlogN_LIS
{const int INF=2147483647;const int maxn=1048576;int g[maxn],d[maxn];int LIS(int* Array,int n){for(int i=1;i<=n;i++)g[i]=INF;int ans=0;for(int i=1;i<=n;i++){int k=lower_bound(g+1,g+n+1,Array[i])-g;d[i]=k;g[k]=Array[i];ans=max(ans,d[i]);}return ans;}
}

UnionFindSet .hpp (并查集模板)

//made by GGN -from NECY
#pragma once#include<cstdio>
#include<cstdlib>namespace UnionFindSet
{const int UFS_Size=1048576;//you can use this to change the size of UFSclass UFS{int Father[UFS_Size];bool NeedInit;int UseSize;public:UFS(){NeedInit=1;}void Init(int NodeCount){NeedInit=0;for(int i=1;i<=NodeCount;i++)Father[i]=i;UseSize=NodeCount;}int Find(int NodeNum){if(NeedInit){printf("Error \"UnionFindSet\":Find  you have to init before use!\n");system("pause>nul");return -1;}if(NodeNum<=0 || NodeNum>UseSize){printf("Error \"UnionFindSet\":Find NodeNum=%d (<=0 or >UseSize=%d) is not allowed!\n",NodeNum,UseSize);system("pause>nul");return -1;}if(NodeNum!=Father[NodeNum])Father[NodeNum]=Find(Father[NodeNum]);return Father[NodeNum];}int Union(int NodeX,int NodeY){if(NeedInit){printf("Error \"UnionFindSet\":Union  you have to init before use!\n");system("pause>nul");return -1;}if(NodeX<=0 || NodeX>UseSize){printf("Error \"UnionFindSet\":Union NodeX=%d (<=0 or >UseSize=%d) is not allowed!\n",NodeX,UseSize);system("pause>nul");return -1;}if(NodeY<=0 || NodeY>UseSize){printf("Error \"UnionFindSet\":Union NodeY=%d (<=0 or >UseSize=%d) is not allowed!\n",NodeY,UseSize);system("pause>nul");return -1;}Father[Find(NodeY)]=Find(NodeX);}int Judge(int NodeX,int NodeY){if(NeedInit){printf("Error \"UnionFindSet\":Judge  you have to init before use!\n");system("pause>nul");return -1;}if(NodeX<=0 || NodeX>UseSize){printf("Error \"UnionFindSet\":Judge NodeX=%d (<=0 or >UseSize=%d) is not allowed!\n",NodeX,UseSize);system("pause>nul");return -1;}if(NodeY<=0 || NodeY>UseSize){printf("Error \"UnionFindSet\":Judge NodeY=%d (<=0 or >UseSize=%d) is not allowed!\n",NodeY,UseSize);system("pause>nul");return -1;}return Find(NodeX)==Find(NodeY);}int Ancestor(int Node){if(NeedInit){printf("Error \"UnionFindSet\":Ancestor  you have to init before use!\n");system("pause>nul");return -1;}if(Node<=0 || Node>UseSize){printf("Error \"UnionFindSet\":Ancestor Node=%d (<=0 or >UseSize=%d) is not allowed!\n",Node,UseSize);system("pause>nul");return -1;}return Father[Node];}};
}

TernarySearch .hpp (三分法求单峰函数最小值)

//made by GGN -from NEYC
#pragma once#include<cstdio>
#include<cstdlib>
#include<cmath>
namespace TernarySearch
{double TSmin(double L,double R,double (*function)(double x),double eps=1e-7){//make a ternary search for min value and return xif(R==L)return L;if(R<L){printf("Error \"Ternary Search\":TSmin R=%d (<L=%d) is not allowed!\n",R,L);system("pause>nul");return L;}if(function==NULL){printf("Error \"Ternary Search\":TSmin f=[NULL] is not allowed!\n");system("pause>nul");return L;}while(fabs(L-R)>eps){double m1=L+(R-L)/3;double m2=R-(R-L)/3;if(function(m1)<function(m2))R=m2;elseL=m1;}return L;}double TSmax(double L,double R,double (*function)(double x),double eps=1e-7){//make a ternary search for max value and return xif(R==L)return L;if(R<L){printf("Error \"Ternary Search\":TSmin R=%d (<L=%d) is not allowed!\n",R,L);system("pause>nul");return L;}if(function==NULL){printf("Error \"Ternary Search\":TSmin f=[NULL] is not allowed!\n");system("pause>nul");return L;}while(fabs(L-R)>eps){double m1=L+(R-L)/3;double m2=R-(R-L)/3;if(function(m1)>function(m2))R=m2;elseL=m1;}return L;}
}

Treap的基本板子

详见 Treap树堆的基本模板(无讲解)


后记

算法模板库仍在扩充中…

Premier Bob的算法模板库(II)相关推荐

  1. Python 算法模板库,Pythonista 找工作利器

    来源:Github-dashidhy https://github.com/dashidhy/algorithm-pattern-python [导语]程序员找工作,刷算法题是必不可少的一步,这里给广 ...

  2. 【跟学C++】C++STL标准模板库——算法详细整理(上)(Study18)

    文章目录 1.STL简介 2.STL算法分类及常用函数 2.1.非变序算法 2.1.1 计数算法(2个) 2.1.2 搜索算法(7个) 2.1.3 比较算法(2个) 3.总结  =========== ...

  3. 【跟学C++】C++STL标准模板库——算法详细整理(下)(Study18)

    文章目录 1.简介 2.STL算法分类及常用函数 2.2.变序算法(二) 2.2.1 替换算法(2个) 2.2.2 排序算法(6个) 2.2.3 分区算法(4个) 2.2.4 可用于排序容器的算法(3 ...

  4. 【跟学C++】C++STL标准模板库——算法详细整理(中)(Study18)

    文章目录 1.简介 2.STL算法分类及常用函数 2.1.变序算法(一) 2.2.1 初始化算法(2个) 2.2.2 修改算法(2个) 2.2.3 复制算法(6个) 2.2.4 删除算法(6个) 3. ...

  5. C++ 笔记(19)— 标准模板库(STL容器、STL迭代器、STL算法、STL容器特点、STL字符串类)

    C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...

  6. 蓝桥杯算法竞赛系列第0章——蓝桥必考点及标准模板库STL(上)(万字博文,建议抱走)

    欢迎来到:遇见蓝桥遇见你,不负代码不负卿! 目录 ​ 一.蓝桥必考点剖析 二.什么是STL 三.vector的常见用法详解 1.vector的定义 2.vector容器内元素的访问 (1).通过下标访 ...

  7. CSP-J CSP-S NOIP 算法竞赛中的STL(标准模板库)

    P1996 约瑟夫问题 2037:[例5.4]约瑟夫问题 P1996 约瑟夫问题 2037:[例5.4]约瑟夫问题_dllglvzhenfeng的博客-CSDN博客 C++ STL详解超全总结(快速入 ...

  8. 【蓝桥杯算法模板题--蓝桥题库Java】

    PDF下载地址:点击即可 文章目录 ==算法模板== 1 排序(ArrayList,sort) 题目描述 输入描述 输出描述 输入输出样例 示例 1 运行限制 2 小明的彩灯(差分) 输入输出样例 示 ...

  9. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

最新文章

  1. gcc/g++链接时.o文件及库的顺序问题
  2. Qt中的模态对话框和非模态对话框
  3. 在Idea中测试各JVM语言的交互性
  4. 【VMCloud云平台】拥抱Docker(六)关于DockerFile(1)
  5. 关于socket组播和ssdp(二)
  6. day12 生成器和各种推导式
  7. 技术总监谈好的程序员如何写代码[转]
  8. FLOPs衡量模型复杂度
  9. XCode下的iOS单元测试
  10. 使用 jszip 实现.zip文件解压后上传
  11. 网易见外不能用?永久免费的软件送你 自动生成字幕,免费快速上字幕, 自动加字幕!自动加字幕!快读完成Pr的字幕制作srt字幕文件生成,AI智能语音生成字幕 视频字幕自动生成 语音转换字幕 极速上字幕
  12. 第1节 中华人民共和国网络安全法
  13. 画基因结构图 gggenes 用法
  14. android build.versioncodes.kitkat,Android 4.1至4.4 KitKat-为API启用TLS 1.2
  15. 谷歌发现育碧uPlay安全漏洞
  16. 家里用服务器放在哪个位置,家用路由器放在什么位置比较合理?
  17. Oracle 常用的V$ 视图脚本
  18. 成都的IT研发产业和芯片产业等情况:2006年初的数据。
  19. 项目经理必须具备的十大管理技能
  20. 1、Moravec角点检测算法

热门文章

  1. 乌鸦搜索算法和粒子集群算法_乌鸦和乌鸦
  2. C语言模拟实现:atoi函数
  3. php ses 发送邮件,使用PHP SDK从Amazon SES发送HTML邮件
  4. 在线版音乐播放器APP(一)
  5. RADARE2+FRIDA=R2FRIDA Best Dynamic Debugging Tool
  6. Java开发-空指针(NullPointException)
  7. 数据分析案例-数据可视化
  8. 进程与程序的区别与联系
  9. 这年头居然还有用360卫士清理垃圾的?那玩意就是最大的...Python自动清理不香吗?
  10. Linux: fPIC与 pie 区别