[BZOJ1913][Apio2010]signaling 信号覆盖

[Apio2010]signaling 信号覆盖

Description
这里写图片描述
Input
输入第一行包含一个正整数 n, 表示房子的总数。接下来有 n 行,分别表示 每一个房子的位置。对于 i = 1, 2, .., n, 第i 个房子的坐标用一对整数 xi和yi来表 示,中间用空格隔开。
Output
输出文件包含一个实数,表示平均有多少个房子被信号所覆盖,需保证输出 结果与精确值的绝对误差不超过0.01。
Sample Input
4
0 2
4 4
0 0
2 0
Sample Output
3.500
HINT
3.5, 3.50, 3.500, … 中的任何一个输出均为正确。此外,3.49, 3.51,
3.499999,…等也都是可被接受的输出。
【数据范围】
100%的数据保证,对于 i = 1, 2, .., n, 第 i 个房子的坐标(xi, yi)为整数且
–1,000,000 ≤ xi, yi ≤ 1,000,000. 任何三个房子不在同一条直线上,任何四个房子不
在同一个圆上;
40%的数据,n ≤ 100;
70%的数据,n ≤ 500;
100%的数据,3 ≤ n ≤ 1,500。

Solution
转化计数方式,我们考虑任意四个点,如果这四个点组成凸四边形,对答案贡献为2,如果是凹四边形则贡献为1.
统计凹四边形可以枚举中间点转化成1913的问题

Code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define MS(_) memset(_, 0, sizeof(_))
#define MP make_pair
#define PB push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
template<typename T> inline void read(T &x){
    x = 0; T f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch))  {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

typedef long long ll;
const int N = 5555;
struct Vec{
    double x, y, angle;
    Vec() {}
    Vec(double _x, double _y):x(_x), y(_y) {angle = atan2(y, x);}
    friend double atan2(Vec a){return atan2(a.y, a.x);}
};
inline Vec operator + (const Vec &a, const Vec &b) {return Vec(a.x+b.x, a.y+b.y);}
inline Vec operator - (const Vec &a, const Vec &b) {return Vec(a.x-b.x, a.y-b.y);}
template<typename T> inline Vec operator * (const Vec &a, T b) {return Vec(a.x*b, a.y*b);}
template<typename T> inline Vec operator * (T a, const Vec &b) {return Vec(a*b.x, a*b.y);}
inline Vec operator / (const Vec &a, double b) {return Vec(a.x/b, a.y/b);}
inline double dot(const Vec &a, const Vec &b) {return a.x*b.x + a.y*b.y;}
inline double cross(const Vec &a, const Vec &b) {return a.x*b.y - a.y*b.x;}
typedef Vec Poi;
inline bool angle_cmp(Vec a, Vec b) {return a.angle<b.angle;}

ll ans1, ans2, n;
Vec p[N], a[N];

inline ll solve(int k){
    ll tot = 1ll*(n-1)*(n-2)*(n-3)/6; 
    int len = 0;
    for (int i = 1; i <= n; i++)
        if (i != k) p[++len] = a[i]; else p[0] = a[i];
    rep(i, 1, len) p[i] = p[i]-p[0];
    sort(p+1, p+1+len, angle_cmp);
    for (int i = 1, r = 1, t = 0; i <= len; i++){
        if (r%len+1 == i) r++, t++;
        while ((r%len+1 != i) && (cross(p[i], p[r%len+1]) >= 0)) r++, t++;
        tot -= 1ll*t*(t-1)/2;
        t--;
    }
    return tot;
}

int main(){ 
    read(n);
    rep(i, 1, n) read(a[i].x), read(a[i].y);

    rep(i, 1, n) ans1 += solve(i);
    ans2 = 1ll*n*(n-1)*(n-2)*(n-3)/24 - ans1;
    double ans = 2*ans2+ans1;
    ans /= 1ll*n*(n-1)*(n-2)/6; ans += 3;
    printf("%.6lf\n", ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值