CF626D D. Jerry‘s Protest

题目链接

题意:

安德鲁和杰瑞分别从n个球中抽一个球(抽完之后放回),总共进行三轮,球上数字高的获胜,前两轮安德鲁胜最后一轮杰瑞胜。求在这种情况下杰瑞抽到的三个球的总和严格高于安德鲁抽到的三个球的总和的概率是多少?

题解:

对给定的a数组进行排序,可以用cnt[i]表示抽到的两个球数字之间差i的次数,每次都是从n个数中抽取两个数\binom{n}{2}也就是说总的方案是tot = n * (n - 1)  /  2。d[i + 1]数组表示前两轮差和为i + j的概率,可以表示为(cnt[i] / tot) * (cnt[j] / tot)(注意两次超出5000的差没有意义,最后一次的差一定<5000),可以通过N^{2}的预处理得到。使用前缀和之后代表前两轮差≤i + j的概率。最后一轮一定是杰瑞获胜,答案就是依次累加小于最后一轮差的概率/tot\sum_{i}^{k}d[i] / tot

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define ve vector
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using pi = pair<int, int>;
using i64 = long long;

inline int read() {
    int x;
    cin >> x;
    return x;
}

void solve() {
    int n = read();
    const int N = 5e3 + 7;
    ve<int> a(n);
    generate(all(a), read);
    sort(all(a));
    ve<int> cnt(N);
    ve<double> d(N);
    int tot = n * (n - 1) / 2;
    rep(i, 0, n) {
        rep(j, 0, i) {
            cnt[a[i] - a[j]]++;
        }
    }
    rep(i, 1, 5001) {
        rep(j, 1, 5001 - i) {
            d[i + j] += (1.0 * cnt[i] / tot) * (1.0 * cnt[j] / tot);
        }
    }
    double res = 0;
    rep(i, 1, 5001) {
        d[i] += d[i - 1];
    }
    rep(i, 0, n) {
        rep(j, i + 1, n) {
            int dif = a[j] - a[i];
            res += d[dif - 1] / tot;
        }
    }
    cout << res << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    while (t--) {
        solve();
    }

    return 0;
}

python3版本:
 

standard_input, packages, output_together = 1, 1, 0
dfs, hashing, read_from_file = 0, 0, 0
de = 1
 
if 1:
 
    if standard_input:
        import io, os, sys
        input = lambda: sys.stdin.readline().strip()
 
        import math
        inf = math.inf
 
        def I():
            return input()
        
        def II():
            return int(input())
 
        def MII():
            return map(int, input().split())
 
        def LI():
            return list(input().split())
 
        def LII():
            return list(map(int, input().split()))
 
        def LFI():
            return list(map(float, input().split()))
 
        def GMI():
            return map(lambda x: int(x) - 1, input().split())
 
        def LGMI():
            return list(map(lambda x: int(x) - 1, input().split()))
 
    if packages:
        from io import BytesIO, IOBase
 
        import random
        import os
 
        import bisect
        import typing
        from collections import Counter, defaultdict, deque
        from copy import deepcopy
        from functools import cmp_to_key, lru_cache, reduce
        from heapq import merge, heapify, heappop, heappush, heappushpop, nlargest, nsmallest
        from itertools import accumulate, combinations, permutations, count, product
        from operator import add, iand, ior, itemgetter, mul, xor
        from string import ascii_lowercase, ascii_uppercase, ascii_letters
        from typing import *
        BUFSIZE = 4096
 
    if output_together:
        class FastIO(IOBase):
            newlines = 0
 
            def __init__(self, file):
                self._fd = file.fileno()
                self.buffer = BytesIO()
                self.writable = "x" in file.mode or "r" not in file.mode
                self.write = self.buffer.write if self.writable else None
 
            def read(self):
                while True:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    if not b:
                        break
                    ptr = self.buffer.tell()
                    self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
                self.newlines = 0
                return self.buffer.read()
 
            def readline(self):
                while self.newlines == 0:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    self.newlines = b.count(b"\n") + (not b)
                    ptr = self.buffer.tell()
                    self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
                self.newlines -= 1
                return self.buffer.readline()
 
            def flush(self):
                if self.writable:
                    os.write(self._fd, self.buffer.getvalue())
                    self.buffer.truncate(0), self.buffer.seek(0)
 
        class IOWrapper(IOBase):
            def __init__(self, file):
                self.buffer = FastIO(file)
                self.flush = self.buffer.flush
                self.writable = self.buffer.writable
                self.write = lambda s: self.buffer.write(s.encode("ascii"))
                self.read = lambda: self.buffer.read().decode("ascii")
                self.readline = lambda: self.buffer.readline().decode("ascii")
 
        sys.stdout = IOWrapper(sys.stdout)
 
    if dfs:
        from types import GeneratorType
 
        def bootstrap(f, stack=[]):
            def wrappedfunc(*args, **kwargs):
                if stack:
                    return f(*args, **kwargs)
                else:
                    to = f(*args, **kwargs)
                    while True:
                        if type(to) is GeneratorType:
                            stack.append(to)
                            to = next(to)
                        else:
                            stack.pop()
                            if not stack:
                                break
                            to = stack[-1].send(to)
                    return to
            return wrappedfunc
 
    if hashing:
        RANDOM = random.getrandbits(20)
        class Wrapper(int):
            def __init__(self, x):
                int.__init__(x)
 
            def __hash__(self):
                return super(Wrapper, self).__hash__() ^ RANDOM
 
    if read_from_file:
        file = open("input.txt", "r").readline().strip()[1:-1]
        fin = open(file, 'r')
        input = lambda: fin.readline().strip()
        output_file = open("output.txt", "w")
        def fprint(*args, **kwargs):
            print(*args, **kwargs, file=output_file)
 
    if de:
        def debug(*args, **kwargs):
            print('\033[92m', end='')
            print(*args, **kwargs)
            print('\033[0m', end='')
def solve():
    n = II()
    a = LII()
    cnt = [0] * 5000
    a.sort()
    for i in range(n):
        for j in range(i):
            cnt[a[i] - a[j]] += 1
    cnt2 = [0] * 5000
    for i in range(5000):
        for j in range(5000):
            if i + j < 5000:
                cnt2[i + j] += cnt[i] * cnt[j]
    for i in range(1, 5000):
        cnt2[i] += cnt2[i-1]
    res = 0
    for i in range(1, 5000):
        res += cnt[i] * cnt2[i-1]

    print(res / (n * (n - 1) / 2) ** 3)

def main():
    t = 1
    while t:
        solve()
        t -= 1
main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值