洛谷P2510 [HAOI2008]下落的圆盘(计算几何)

本文介绍了一种计算多个圆相交时被覆盖总周长的方法,利用余弦定理和极角转化,解决了圆弧多次覆盖的计算难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

传送门

题解

对于每个圆,我们单独计算它被覆盖的周长是多少

只有相交的情况需要考虑,我们需要知道相交的那段圆弧的角度,发现其中一个交点和两个圆的圆心可以构成一个三角形且三边都已经知道了,那么我们可以根据余弦定理计算出这段圆弧的余弦进而用\(acos\)计算出角度

然而现在有个尴尬的问题是一段圆弧可能会被多次覆盖。那么我们考虑把相交的圆弧的左右端点用极角来表示,并把这个看成一条线段,那么最后只要求出线段覆盖就行了

顺便注意转化为极角的时候如果极角是负的要加上\(2\pi\),如果这时候\(l>r\),就拆成\([l,2\pi]+[2\pi,r]\)的形式

//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
    R int res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
double readdb()
{
    R double x=0,y=0.1,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(x=ch-'0';(ch=getc())>='0'&&ch<='9';x=x*10+ch-'0');
    for(ch=='.'&&(ch=getc());ch>='0'&&ch<='9';x+=(ch-'0')*y,y*=0.1,ch=getc());
    return x*f;
}
const int N=2005;const double Pi=acos(-1.0);
struct point{double r,x,y;}p[N];
struct node{
    double l,r;
    node(){}
    node(R double ll,R double rr):l(ll),r(rr){}
    inline bool operator <(const node &b)const{return l<b.l;}
}st[N];
int n,top;double res;
inline double dis(R int i,R int j){return sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));}
inline int in(R int i,R int j){return p[j].r>=p[i].r+dis(i,j);}
void calc(int pos){
    fp(i,pos+1,n)if(in(pos,i))return;
    top=0;
    fp(i,pos+1,n){
        R double d=dis(pos,i);if(in(i,pos)||p[i].r+p[pos].r<=d)continue;
        R double t=acos((d*d+p[pos].r*p[pos].r-p[i].r*p[i].r)/(2*p[pos].r*d));
        R double b=atan2(p[i].y-p[pos].y,p[i].x-p[pos].x);
        st[++top]=node(b-t,b+t);
        st[top].l<0?st[top].l+=2*Pi:0;
        st[top].r<0?st[top].r+=2*Pi:0;
        st[top].l>st[top].r?(st[top+1]=node(0,st[top].r),st[top++].r=2*Pi):0;
    }
    sort(st+1,st+1+top);
    R double now=0,tmp=0;
    fp(i,1,top)now<st[i].l?(tmp+=st[i].l-now,now=st[i].r):cmax(now,st[i].r);
    res+=p[pos].r*(tmp+2*Pi-now);
}
int main(){
//  freopen("testdata.in","r",stdin);
    n=read();
    fp(i,1,n)p[i].r=readdb(),p[i].x=readdb(),p[i].y=readdb();
    fp(i,1,n)calc(i);
    printf("%.3lf\n",res);
    return 0;
}

转载于:https://www.cnblogs.com/bztMinamoto/p/10513288.html

这道题目还可以使用树状数组或线段树来实现,时间复杂度也为 $\mathcal{O}(n\log n)$。这里给出使用树状数组的实现代码。 解题思路: 1. 读入数据; 2. 将原数列离散化,得到一个新的数列 b; 3. 从右往左依次将 b 数列中的元素插入到树状数组中,并计算逆序对数; 4. 输出逆序对数。 代码实现: ```c++ #include <cstdio> #include <cstdlib> #include <algorithm> const int MAXN = 500005; struct Node { int val, id; bool operator<(const Node& other) const { return val < other.val; } } nodes[MAXN]; int n, a[MAXN], b[MAXN], c[MAXN]; long long ans; inline int lowbit(int x) { return x & (-x); } void update(int x, int val) { for (int i = x; i <= n; i += lowbit(i)) { c[i] += val; } } int query(int x) { int res = 0; for (int i = x; i > 0; i -= lowbit(i)) { res += c[i]; } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); nodes[i] = {a[i], i}; } std::sort(nodes + 1, nodes + n + 1); int cnt = 0; for (int i = 1; i <= n; ++i) { if (i == 1 || nodes[i].val != nodes[i - 1].val) { ++cnt; } b[nodes[i].id] = cnt; } for (int i = n; i >= 1; --i) { ans += query(b[i] - 1); update(b[i], 1); } printf("%lld\n", ans); return 0; } ``` 注意事项: - 在对原数列进行离散化时,需要记录每个元素在原数列中的位置,便于后面计算逆序对数; - 设树状数组的大小为 $n$,则树状数组中的下标从 $1$ 到 $n$,而不是从 $0$ 到 $n-1$; - 在计算逆序对数时,需要查询离散化后的数列中比当前元素小的元素个数,即查询 $b_i-1$ 位置上的值; - 在插入元素时,需要将离散化后的数列的元素从右往左依次插入树状数组中,而不是从左往右。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值