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.pswp.cn/news/395682.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395682.shtml
英文地址,請注明出處:http://en.pswp.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 …