• codeforces626D . Jerry's Protest


    Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds.

    Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew?

    Input

    The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar.

    The second line contains n integers ai (1 ≤ ai ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number.

    Output

    Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

    Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

    Examples
    input
    output
    input
    output
    Note

    In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total.

    In the second case, each game could've had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability .

    思路:暴力+概率;

    每次都是独立的,我们单独看第一次,那么暴力枚举下第一次的所有的方案数,那么总的符合条件的方案就是sum^3;然后统计前两个组成的方案数,因为第三组要大于1,2两组的和,

    那么我们只要枚举一二两组的和统计下前缀,然后枚举第三组,找小于第三组的就行。

    复杂(N*N)

     

     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<stdlib.h>
     5 #include<queue>
     6 #include<string.h>
     7 using namespace std;
     8 int ans[3000];
     9 int cnt[6000];
    10 int num[6000];
    11 long long sum[10005];
    12 int main(void)
    13 {
    14         int n;
    15         while(scanf("%d",&n)!=EOF)
    16         {       double am = 0;
    17                 memset(cnt,0,sizeof(cnt));
    18                 memset(num,0,sizeof(num));
    19                 int i ,j;
    20                 for(i = 1; i <= n; i++)
    21                         scanf("%d",&ans[i]);
    22                 for(i = 1; i <= n; i++)
    23                 {
    24                         for(j = i+1; j  <=n; j++)
    25                         {
    26                                 int x = abs(ans[i]-ans[j]);
    27                                 //printf("%d
    ",x);
    28                                 num[x]++;
    29                         }
    30                 }
    31                 for(i = 1; i < 6000; i++)
    32                 {
    33                         cnt[i]+=cnt[i-1]+num[i];
    34                 }
    35                 memset(sum,0,sizeof(sum));
    36                 for(i = 1; i < 5000; i++)
    37                 {
    38                         for(j = 1; j < i; j++)
    39                         {
    40                                 if(num[j]&&num[i-j])
    41                                 {
    42                                     sum[i]+=num[j]*num[i-j];
    43                                 }
    44                         }
    45                 }
    46                 for(i = 1;i <= 5000;i++)
    47                     sum[i] += sum[i-1];
    48                 for(i = 2;i <= 5000;i++)
    49                 {
    50                     if(num[i])
    51                     {
    52                         am += sum[i-1]*num[i];
    53                     }
    54                 }
    55                 double maxx = cnt[5000];
    56                 maxx = maxx*maxx*maxx;
    57 
    58                 double an = 1.0*am/maxx;
    59                 printf("%.10f
    ",an);
    60         }
    61         return 0;
    62 }

     

     

    油!油!you@
  • 相关阅读:
    ksframework的xlua版本
    unity摄像机脚本
    代码重构:用工厂+策略模式优化过多的if else代码块
    在Unity中创建攻击Slot系统
    Unity运用GPU代替CPU处理和计算简单测试
    程序员工具集
    Unity开发-你必须知道的优化建议
    unity向量计算
    ClassFoo-IT知识花园
    BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5889480.html
Copyright © 2020-2023  润新知