zoukankan      html  css  js  c++  java
  • BestCoder Round #76 (div.2) DZY Loves Balls

    DZY Loves Balls

     
     Accepts: 662
     
     Submissions: 1393
     Time Limit: 4000/2000 MS (Java/Others)
     
     Memory Limit: 262144/262144 K (Java/Others)
    Problem Description

    DZY loves playing balls.

    He has nn balls in a big box. On each ball there is an integer written.

    One day he decides to pick two balls from the box. First he randomly picks a ball from the box, and names it AA. Next, without putting AA back into the box, he randomly picks another ball from the box, and names it BB.

    If the number written on AA is strictly greater than the number on BB, he will feel happy.

    Now you are given the numbers on each ball. Please calculate the probability that he feels happy.

    Input

    First line contains tt denoting the number of testcases.

    tt testcases follow. In each testcase, first line contains nn, second line contains nn space-separated positive integers a_iai, denoting the numbers on the balls.

    (1le tle 300, 2le n le 300,1le a_i le 3001t300,2n300,1ai300)

    Output

    For each testcase, output a real number with 6 decimal places.

    Sample Input
    2
    3
    1 2 3
    3
    100 100 100
    
    Sample Output
    0.500000
    0.000000
    
    
    一道求期望的水题~~~~~~~~~~~~~~~~~~~~~~~~~·
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int t, sq[1000], n;
    bool cmp(int x, int y)
    {
    	return x < y;
    }
    int same(int x) {
    	if((x + 1) < n && sq[x] == sq[x + 1])
    		return 1 + same(x + 1);
    	return 0;
    }
    int main()
    {
    	scanf("%d", &t);
    	while (t--) {
    		double ans = 0;
    		scanf("%d", &n);
    		for (int i = 0; i < n; i++) {
    			scanf("%d", &sq[i]);
    		}
    		sort(sq, sq + n,cmp);
    		for (int i = 0; i < n; i++) {
    			if(same(i)) {
    				ans += n -1 - i - same(i);
    			}
    			else ans += n - i - 1;
    		}
    		printf("%lf
    ", ans / (n * (n - 1)));
    	}
    	return 0;
    } 
    


  • 相关阅读:
    RavenScheme简介
    我终于理解了LISP『代码即数据|数据即代码』的含义
    汉编随想(一)
    SICP的一些练习题
    linux下Script小计
    vscode小计
    软硬连接区别
    find过滤大小执行操作
    pyqt5和qt designer安装
    pyenv和virtualenv结合使用管理多python环境
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770967.html
Copyright © 2011-2022 走看看