hdu 5753 Permutation Bo(2016 Multi-University Training Contest 3——组合)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5753

Permutation Bo

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 232    Accepted Submission(s): 140
Special Judge

Problem Description
There are two sequences  h1hn  and  c1cn h1hn  is a permutation of  1n . particularly,  h0=hn+1=0 .

We define the expression  [condition]  is 1 when  condition  is True,is 0 when  condition  is False.

Define the function  f(h)=ni=1ci[hi>hi1  and  hi>hi+1]

Bo have gotten the value of  c1cn , and he wants to know the expected value of  f(h) .
 

Input
This problem has multi test cases(no more than  12 ).

For each test case, the first line contains a non-negative integer  n(1n1000) , second line contains  n  non-negative integer  ci(0ci1000) .
 

Output
For each test cases print a decimal - the expectation of  f(h) .

If the absolute error between your answer and the standard answer is no more than  104 , your solution will be accepted.
 

Sample Input
  
  
4 3 2 4 5 5 3 5 99 32 12
 

Sample Output
  
  
6.000000 52.833333
 

Source
 
由于题目中有很多公式,导致贴过来都显示不完全,具体题目请大家点开链接去参照~~(*^__^*) 嘻嘻……

题目大意:有两个序列,h1-hn,c1-cn,题目中给了c这个序列的值,然后h序列是随机的1-n随便排列。
这里要找到符合这个条件的值。

解题思路:
由于数据量比较大,所以我们只要统计每一个数被加了多少次就可以了。
对于第一组数据:
4
3 2 4 5
c  0 3 2 4 5 0
h 0 1 2 3 4 0
   0 1 2 4 3 0
   0 1 3 2 4 0
..........
只列举了这一点就可想而知有多少了,接下来统计每个数被加的次数。
0 3 5 4 5 0
加3的次数 :1、第一个数取2 : 1*1*A22  2、第一个数取3: 1(2*A22  3、第一个数取4 :  1*3*A22
(这个数就可以代表两边的数被加的次数)
加2的次数:1、第二个数取3: 2*1*1*A11 2、第二个数取4: 3*1*2*A11。
(这个数可以代表所有中间的数被加的次数)
依据上述推出来的等式,进行化简即可。(其中A11,A22这些表示的是全排列)

特别注意:
1、这里需要注意n=1和n=2的时候,要进行特殊判断。
2、在中间计算的过程很可能超范围,要用long long

详见代码。
#include <iostream>
#include <cstdio>

using namespace std;

#define ll long long

int a[1010];

int main()
{
    int n;
    ll ss,sum;
    while (~scanf("%d",&n))
    {
        sum=ss=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        if (n==1)
        {
            printf("%.6lf\n",(double)a[0]);
            continue;
        }
        if (n==2)
        {
            printf("%.6lf\n",n*(n-1)/2*((a[0]+a[n-1])*1.0/(n*(n-1))));
            continue;
        }
        ll s=n*(n-1)/2;//边上两个数被加的次数
        double sl=s*(a[0]+a[n-1])*1.0/(n*(n-1));
        for (int i=1;i<n-1;i++)
        {
            sum+=a[i];
            //cout<<sum<<endl;
            ss+=i*(i+1);
            //cout<<ss<<endl;
        }

        double sz=ss*sum*1.0/(n*(n-1)*(n-2));
        double ans=sl+sz;
        printf ("%.6lf\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值