题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1845

给定100个三角形,求三角形面积并。

戴神模板太可怕。直接调用函数秒掉。思路有点繁琐,不大清楚。贴一个代码。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/7/3 22:46:38
File Name :2.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
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;
}
};
Point operator + (Point a,Point b){
return Point(a.x+b.x,a.y+b.y);
}
Point operator - (Point a, Point b){
return Point(a.x-b.x,a.y-b.y);
}
Point operator * (Point a,double p){
return Point(a.x*p,a.y*p);
}
Point operator / (Point a,double p){
return Point(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);
}
bool operator == (const Point &a,const Point &b){
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Point a, Point b){
return a.x*b.x+a.y*b.y;
}
double Length(Point a){
return sqrt(Dot(a,a));
}
double Angle(Point a,Point b){
return acos(Dot(a,b)/Length(a)/Length(b));
}
double angle(Point a){
return atan2(a.y,a.x);
}
double Cross(Point a,Point b){
return a.x*b.y-a.y*b.x;
}
Point GetLineIntersection(Point p,Point v,Point q,Point w){
Point u=p-q;
double t=Cross(w,u)/Cross(v,w);
return p+v*t;
}
struct polygon{
int n;
Point p[100];
double getarea(){
double sum=0;
for(int i=0;i<n;i++){
sum+=Cross(p[i],p[(i+1)%n]);
}
return fabs(sum)/2;
}
bool getdir(){
double sum=0;
for(int i=0;i<n;i++)
sum+=Cross(p[i],p[(i+1)%n]);
if(dcmp(sum)>0)return 1;
return 0;
}
};
struct polygons{
vector<polygon> p;
polygons(){
p.clear();
}
void push(polygon q){
if(dcmp(q.getarea()))p.push_back(q);
}
vector<pair<double,int> > e;
void ins(Point s,Point t,Point X,int i){
double r=fabs(t.x-s.x)>eps?(X.x-s.x)/(t.x-s.x):(X.y-s.y)/(t.y-s.y);
r=min(r,1.0);r=max(r,0.0);
e.push_back(make_pair(r,i));
}
double polyareaunion(){
double ans=0;
int c0,c1,c2;
for(int i=0;i<p.size();i++)
if(p[i].getdir()==0)
reverse(p[i].p,p[i].p+p[i].n);
for(int i=0;i<p.size();i++){
for(int k=0;k<p[i].n;k++){
Point &s=p[i].p[k],&t=p[i].p[(k+1)%p[i].n];
if(!dcmp(Cross(s,t)))continue;
e.clear();
e.push_back(make_pair(0.0,1));
e.push_back(make_pair(1.0,-1));
for(int j=0;j<p.size();j++)
if(i!=j){
for(int w=0;w<p[j].n;w++){
Point a=p[j].p[w];
Point b=p[j].p[(w+1)%p[j].n];
Point c=p[j].p[(w-1+p[j].n)%p[j].n];
c0=dcmp(Cross(t-s,c-s));
c1=dcmp(Cross(t-s,a-s));
c2=dcmp(Cross(t-s,b-s));
if(c1*c2<0)ins(s,t,GetLineIntersection(s,t-s,a,b-a),-c2);
else if(!c1&&c0*c2<0)ins(s,t,a,-c2);
else if(!c1&&!c2){
int c3=dcmp(Cross(t-s,p[j].p[(w+2)%p[j].n]-s));
int dp=dcmp(Dot(t-s,b-a));
if(dp&&c0)ins(s,t,a,dp>0?c0*((j>i)^(c0<0)):-(c0<0));
if(dp&&c3)ins(s,t,b,dp>0?-c3*((j>i)^(c3<0)):c3<0);
}
}
}
sort(e.begin(),e.end());
int ct=0;
double tot=0,last;
for(int j=0;j<e.size();j++){
if(ct==1)tot+=e[j].first-last;
ct+=e[j].second;
last=e[j].first;
}
ans+=Cross(s,t)*tot;
}
}
return fabs(ans)/2;
}
};
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout); int n;
while(~scanf("%d",&n)){
polygons ps;
double ans=0;
for(int i=0;i<n;i++){
polygon p1;
p1.n=3;
for(int j=0;j<p1.n;j++){
scanf("%lf%lf",&p1.p[j].x,&p1.p[j].y);
}
ps.push(p1);
}
printf("%.2f\n",ps.polyareaunion());
}
return 0;
}

BZOJ 1845三角形面积并的更多相关文章

  1. bzoj 1845: [Cqoi2005] 三角形面积并 扫描线

    1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 848  Solved: 206[Submit][Statu ...

  2. BZOJ 1845: [Cqoi2005] 三角形面积并 [计算几何 扫描线]

    1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 1151  Solved: 313[Submit][Stat ...

  3. CQOI2005 三角形面积并 和 POJ1177 Picture

    1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 1664  Solved: 443[Submit][Stat ...

  4. ytu 1058: 三角形面积(带参的宏 练习)

    1058: 三角形面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 190  Solved: 128[Submit][Status][Web Boar ...

  5. UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)

    Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...

  6. OpenJudge计算概论-计算三角形面积【海伦公式】

    /*============================================== 计算三角形面积 总时间限制: 1000ms 内存限制: 65536kB 描述 平面上有一个三角形,它的 ...

  7. nyoj 67 三角形面积【三角形面积公式】

    三角形面积 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 给你三个点,表示一个三角形的三个顶点,现你的任务是求出该三角形的面积   输入 每行是一组测试数据,有6个 ...

  8. NYOJ 67 三角形面积(线代,数学)

    三角形面积 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 给你三个点,表示一个三角形的三个顶点,现你的任务是求出该三角形的面积   输入 每行是一组测试数据,有6个 ...

  9. TZOJ 2519 Regetni(N个点求三角形面积为整数总数)

    描述 Background Hello Earthling. We're from the planet Regetni and need your help to make lots of mone ...

随机推荐

  1. highcharts图表配置参数汇总

    一.chart的部分相关属性说明    renderTo: 'container',      //图表的页面显示容器(也就是要显示到的div) chart.events.addSeries:添加数列 ...

  2. Tomcat报错Exception: java.lang.OutOfMemoryError

    进入Tomcat中的/bin/catalina.sh 在catalina.sh中echo"Using CATALINA_BASE"之前的一行添加如下代码: JAVA_OPTS=&q ...

  3. Lunar New Year and Red Envelopes CodeForces - 1106E (dp)

    大意: 总共$n$的时间, $k$个红包, 红包$i$只能在时间$[s_i,t_i]$范围内拿, 并且拿完后时间跳到$d_i+1$,Bob采用贪心策略,每个时间点若有红包能取则取钱数$w_i$最大的, ...

  4. 『Scrapy』爬取斗鱼主播头像

    分析目标 爬取的是斗鱼主播头像,示范使用的URL似乎是个移动接口(下文有提到),理由是网页主页属于动态页面,爬取难度陡升,当然爬取斗鱼主播头像这么恶趣味的事也不是我的兴趣...... 目标URL如下, ...

  5. Python并行(parallel)之谈

    简介 可以先看看并发Concurrent与并行Parallel的区别 在谈并行前,头脑中总会浮出多线程.多进程.线程/进程同步.线程/进程通信等词语. 那为什么需要同步.通信,它们之间的作用是怎样的呢 ...

  6. Coconuts, Revisited(递推+枚举+模拟)

    Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...

  7. $LANG、$NLS_LANG 记录一下

    环境:linux $LANG 为linux termal终端环境下的 语言环境 $NLS_LANG  为oracle数据库中 会话中的语言环境. 个人理解,望大家补充

  8. 使用Bulk Binding批量绑定的模式高效处理ORACLE大量数据

           用批量绑定(bulk binding)的方式.当循环执行一个绑定变量的sql语句时候,在PL/SQL 和SQL引擎(engines)中,会发生大量的上下文切换(context switc ...

  9. 4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  10. echatrs可视化图在隐藏后显示不出来或是宽度出现问题

    最近在做一个可视化的项目,用了百度的ECharts.js作为可视化的视图框架,echarts的实例很多,基本能满足项目的需求,而且文档也相对完整.清晰,是个很不错的前端可视化框架. 我们的项目是使用b ...