bzoj 2178 圆的面积并 —— 辛普森积分

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2178

先看到这篇博客:https://www.cnblogs.com/heisenberg-/p/6740654.html

好像本应算弓形面积、三角形面积之类的,但不会...于是用辛普森积分硬做...

参考了这篇博客:https://blog.csdn.net/orpinex/article/details/7311363

然而如果写成精度友好型的 asr ( *15, /15, eps/2 ),或T或RE的,不精度友好反而好了...

为什么一开始传的范围是所有圆边界的 min, max 就会WA,传 -inf, inf 就A了...

总之写的时候还是尽量稳妥一点吧...

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double db;
int const xn=1005,inf=2005;
db const eps=1e-8;
int n;
bool tmp[xn];
struct N{int x,y,r;}c[xn];
struct S{db l,r;}seg[xn];
int rd()
{int ret=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();return f?ret:-ret;
}
int dmp(db x)
{if(fabs(x)<eps)return 0;else if(x>eps)return 1;else return -1;
}
db sqr(db x){return x*x;}
bool cmp(S a,S b){return dmp(a.l-b.l)<0||(dmp(a.l-b.l)==0&&dmp(a.r-b.r)<0);}
bool cmp2(N a,N b){return a.r<b.r;}
db maxx(db x,db y){if(dmp(x-y)<0)return y; return x;}
bool in(int a,int b){return sqr(c[a].x-c[b].x)+sqr(c[a].y-c[b].y)<=sqr(c[a].r-c[b].r);}
void init()
{sort(c+1,c+n+1,cmp2);for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++)if(in(i,j)){tmp[i]=1; break;}}int tot=0;for(int i=1;i<=n;i++)if(!tmp[i])c[++tot]=c[i];n=tot;
}
db f(db x)
{int cnt=0;for(int i=1;i<=n;i++){if(dmp(fabs(c[i].x-x)-c[i].r)>0)continue;db dis=sqrt(sqr(c[i].r)-sqr(x-c[i].x));seg[++cnt].l=c[i].y-dis; seg[cnt].r=c[i].y+dis;}sort(seg+1,seg+cnt+1,cmp);db ret=0,r=-inf;for(int i=1;i<=cnt;i++){if(dmp(seg[i].l-r)>0)ret+=seg[i].r-seg[i].l,r=seg[i].r;else if(dmp(seg[i].r-r)>0)ret+=seg[i].r-r,r=seg[i].r;}return ret;
}
db simp(db l,db r){return (r-l)/6*(f(l)+4*f((l+r)/2)+f(r));}
db asr(db l,db r,db eps,db lst)
{db mid=(l+r)/2;db ls=simp(l,mid),rs=simp(mid,r);if(fabs(ls+rs-lst)<=15*eps)return ls+rs+(ls+rs-lst)/15;return asr(l,mid,eps/2,ls)+asr(mid,r,eps/2,rs);
}
db asr(db l,db r,db lst)
{db mid=(l+r)/2;db ls=simp(l,mid),rs=simp(mid,r);if(fabs(ls+rs-lst)<=eps)return ls+rs;return asr(l,mid,ls)+asr(mid,r,rs);
}
int main()
{n=rd(); int L=inf,R=-inf;for(int i=1;i<=n;i++)c[i].x=rd(),c[i].y=rd(),c[i].r=rd(),L=min(L,c[i].x-c[i].r),R=max(R,c[i].x+c[i].r);init();//printf("%.3f\n",asr(L,R,eps,simp(L,R)));//printf("%.3f\n",asr(L,R,simp(L,R)));printf("%.3f\n",asr(-inf,inf,simp(-inf,inf)));//printf("%.3f\n",asr(-inf,inf,eps,simp(-inf,inf)));return 0;
}
View Code

 

然而这样其实会错HAHA,随便来个数据竟然就错了:

3
0 0 1
0 0 1
100 100 1

应该输出 6.283,但上面的代码以及许多题解输出都是 3.142 ...

于是换了一种写法,对每个连续段做积分,这样避免了空白区域对积分结果的影响;

而且发现求一次 f(x) 很慢,所以之前求过的尽量重复利用;

然后就T了,调了两小时...

TLE 的原因竟然是 sort 里面传了 cmp() 函数??!!!如果改成重载结构体小于号,就不T了呵呵-_-

所以还是要注意代码习惯阿。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double db;
int const xn=1005,inf=2005;
db const eps=1e-13;
int n,st,ed,xl[xn],xr[xn];
bool tmp[xn];
struct N{int x,y,r;bool operator < (const N &b) const{return r<b.r;}
}c[xn];
struct S{db l,r;bool operator < (const S &b) const{return l<b.l;}
}seg[xn];
int rd()
{int ret=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();return f?ret:-ret;
}
db sqr(db x){return x*x;}
//bool cmp(S a,S b){return a.l<b.l;}
//bool cmp2(N a,N b){return a.r<b.r;}
bool cmp3(N a,N b){return a.x-a.r<b.x-b.r;}
bool in(int a,int b){return sqr(c[a].x-c[b].x)+sqr(c[a].y-c[b].y)<=sqr(c[a].r-c[b].r);}
void init()
{sort(c+1,c+n+1);//cmp2for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++)if(in(i,j)){tmp[i]=1; break;}}int tot=0;for(int i=1;i<=n;i++)if(!tmp[i])c[++tot]=c[i];n=tot;sort(c+1,c+n+1,cmp3);//
}
db f(db x)
{int cnt=0;for(int i=st;i<=ed;i++){if(xl[i]>=x||xr[i]<=x)continue;db dis=sqrt(c[i].r-sqr(x-c[i].x));//(sqr)seg[++cnt].l=c[i].y-dis; seg[cnt].r=c[i].y+dis;}sort(seg+1,seg+cnt+1);//cmpdb ret=0,r=-inf;for(int i=1,j;i<=cnt;i=j){r=seg[i].r;for(j=i+1;j<=cnt&&seg[j].l<=r;j++)if(r<seg[j].r)r=seg[j].r;ret+=r-seg[i].l; }return ret;
}
db simp(db len,db fl,db fr,db fm){return len/6*(fl+4*fm+fr);}
db asr(db l,db r,db mid,db fl,db fr,db fm,db lst)
{db lmid=(l+mid)/2,flm=f(lmid),rmid=(mid+r)/2,frm=f(rmid);db ls=simp(mid-l,fl,fm,flm),rs=simp(r-mid,fm,fr,frm);if(fabs(ls+rs-lst)<=eps)return ls+rs;return asr(l,mid,lmid,fl,fm,flm,ls)+asr(mid,r,rmid,fm,fr,frm,rs);
}
int main()
{n=rd();for(int i=1;i<=n;i++)c[i].x=rd(),c[i].y=rd(),c[i].r=rd();init(); db ans=0;for(int i=1;i<=n;i++)xl[i]=c[i].x-c[i].r,xr[i]=c[i].x+c[i].r,c[i].r=c[i].r*c[i].r;for(int i=1,j;i<=n;i=j){int l=xl[i],r=xr[i];for(j=i+1;xl[j]<=r&&j<=n;j++)if(xr[j]>r)r=xr[j];st=i; ed=j-1; db mid=(l+r)/2;db fl=f(l),fm=f(mid),fr=f(r);ans+=asr(l,r,mid,fl,fr,fm,simp(r-l,fl,fr,fm));}printf("%.3f\n",ans);return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Zinn/p/10142646.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395682.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php获取访问者ip地址汇总,php获取访问者IP地址汇总_PHP

//方法1&#xff1a;$ip $_SERVER["REMOTE_ADDR"];echo $ip;//方法2&#xff1a;代码如下:$user_IP ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];$user_IP ($user_IP) ? $user_IP : $…

Charles抓包工具的使用

2019独角兽企业重金招聘Python工程师标准>>> 感谢唐巧分享的文章&#xff0c;受益匪浅 文章目录 1. 目录及更新说明2. Charles 限时优惠3. 简介4. 安装 Charles5. 将 Charles 设置成系统代理6. Charles 主界面介绍7. 过滤网络请求8. 截取 iPhone 上的网络封包 8.1. …

python每秒20个请求_使用Python每秒百万个请求

python每秒20个请求by Paweł Piotr Przeradowski通过PawełPiotr Przeradowski 使用Python每秒百万个请求 (A million requests per second with Python) Is it possible to hit a million requests per second with Python? Probably not until recently.使用Python每秒可以…

iOS开发——处理1000张图片的内存优化

一、项目需求 在实际项目中&#xff0c;用户在上传图片时&#xff0c;有时会一次性上传大量的图片。在上传图片前&#xff0c;我们要进行一系列操作&#xff0c;比如&#xff1a;旋转图片为正确方向&#xff0c;压缩图片等&#xff0c;这些操作需要将图片加载到内存中&#xff…

jquery ui php,php – 打开带有动态内容的jQuery UI对话框

我有一个关于jQuery UI对话框的问题,并显示数据库中的动态内容.所以我得到了一个web应用程序,我还需要创建一个管理模块来管理所有用户和其他信息.我创建了一个页面,显示列表中的所有用户,在每一行中我也创建了一个编辑按钮.我想这样做,当你按下用户的编辑按钮时,会打开一个对话…

linux shell的单行多行注释

1.单行注释&#xff0c;使用符号# echo "123456"echo "test"#echo "comment“ 2. 多行注释 &#xff08;1&#xff09;使用 :<<! &#xff01; filenametest.txt :<<! fileContentcat $filenamei0 for line in $fileContent dofileList[…

MapReduce Input Split 输入分/切片

MapReduce Input Split&#xff08;输入分/切片&#xff09;详解 public static long getMaxSplitSize(JobContext context) { return context.getConfiguration().getLong(SPLIT_MAXSIZE, Long.MAX_VALUE); } 如果没有设置这maxsize默认是Long.MAX_VALUE public static long …

win7无损扩大c盘空间_无损网络导航的空间模型

win7无损扩大c盘空间by Patryk Adaś通过PatrykAdaś 无损网络导航的空间模型 (A Spacial Model for Lossless Web Navigation) In my last post I described the concept of navigation trails as an evolution of the standard tabbed browsing model.在我的上一篇文章中&am…

php访问者信息,如何通过PHP检索访问者的ISP?

我试图纠正拉姆库马尔的答案,但每当我编辑他们的帖子,我将被暂时禁止,我的修改被忽略。(至于为什么,我不知道,这是我第一次也是唯一一次在这个网站上编辑。)由于网站更改和管理员执行基本的bot检查(检查标题),他的代码不再工作:$IP $_SERVER[REMOTE_ADDR];$User_Agent Mozill…

从《在小吃店遇见凯恩斯》初识经济

最近在看《在小吃店遇见凯恩斯》这本书&#xff0c;算是对经济和经济学的初步认识。 那些概念 1. 经济与经济学 经济&#xff1a;经世济民&#xff0c;经营国家、救赎百姓&#xff0c;发展国家经济进步、促成人人致富。 经济学&#xff1a;研究发展国家经济进步、促成人人致富的…

2pc 3pc_在1990年代如何宣传PC

2pc 3pcby Ilya Pestov通过伊利亚佩斯托夫(Ilya Pestov) 在1990年代如何宣传PC (How PCs were advertised in the 1990s) Today, hard drives are boring. You can buy a terabyte hard drive for $50. But back in the day, people would get excited when they saw ads anno…

WPF自定义空心文字

WPF自定义空心文字 原文:WPF自定义空心文字首先创建一个自定义控件&#xff0c;继承自FrameworkElement&#xff0c;“Generic.xaml”中可以不添加样式。 要自定义空心文字&#xff0c;要用到绘制格式化文本FormattedText类。FormattedText对象提供的文本格式设置功能比WPF提供…

php默认日志位置,Laravel 修改默认日志文件名称和位置的例子

修改默认日志位置我们平常的开发中可能一直把laravel的日志文件放在默认位置不会有什么影响&#xff0c;但如果我们的项目上线时是全量部署&#xff0c;每次部署都是git中最新的代码&#xff0c;那这个时候每次都会清空我们的日志&#xff0c;显示这不是我们所期望的&#xff0…

【转】UITableView详解(UITableViewCell

原文网址&#xff1a;http://www.kancloud.cn/digest/ios-1/107420 上一节中,我们定义的cell比较单一,只是单调的输入文本和插入图片,但是在实际开发中,有的cell上面有按钮,有的cell上面有滑动控件,有的cell上面有开关选项等等,具体参加下面2个图的对比: 我们可以通过…

Android 最简单的MVP案例;

随手撸个发出来&#xff1a; V&#xff1a;界面层 //界面层需要实现P.View方法&#xff0c;然后重写P.View中的方法&#xff1b;M层给的数据就在这些个方法的参数中&#xff1b; // 还要获取到P.Provide的实例&#xff0c;使用P.Provide去调用M层的方法&#xff1b; public cla…

c++编码风格指南_100%正确编码样式指南

c编码风格指南Tabs or spaces? Curly brace on the same line or a new line? 80 character width or 120?制表符或空格&#xff1f; 在同一行或新行上大括号&#xff1f; 80个字符的宽度还是120个字符&#xff1f; Coders love to argue about this kind of stuff. The ta…

Netty源码注释翻译-Channel类

定义为一个通往网络socket或者一个由I/O读写能力的组件。 通道提供&#xff1a; 1&#xff0c;通道的当前状态&#xff0c;打开&#xff1f;已连接&#xff1f; 2&#xff0c;跟通道关联的配置信息ChannelConfig&#xff0c;包括buffer大小等。 3&#xff0c;通道支持的I/O操作…

Today is weekend不是应该一定会输出吗

判断语句 If…else块&#xff0c;请看下面这个例子&#xff1a; <%! int day 3; %>                       //声明变量感叹号 <html> <head><title>IF...ELSE Example</title></head> <body> <% if (day …

时间模块和时间工具

一、time模块 三种格式 时间戳时间&#xff1a;浮点数 单位为秒 时间戳起始时间&#xff1a; 1970.1.1 0:0:0 英国伦敦时间 1970.1.1 8:0:0 我国(东8区) 结构化时间&#xff1a;元组(struct_time) 格式化时间&#xff1a;str数据类型的 1、常用方法 import timetime.sleep(secs…

redux扩展工具_用鸭子扩展您的Redux App

redux扩展工具How does your front-end application scale? How do you make sure that the code you’re writing is maintainable 6 months from now?您的前端应用程序如何扩展&#xff1f; 您如何确定您正在编写的代码从现在起6个月内可维护&#xff1f; Redux took the …