2016 Multi-University Training Contest 3 hdu 5753 Permutation Bo【打表+递推】

本文解析了PermutationBo题目,介绍了如何通过观察规律找到解决方案的过程,并给出了AC代码实现。该问题涉及序列和全排列,核心在于理解期望值计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Permutation Bo

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

Problem Description

There are two sequences h1∼hn and c1∼cn. h1∼hn is a permutation of 1∼n. 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>hi−1  and  hi>hi+1]

Bo have gotten the value of c1∼cn, 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(1≤n≤1000), second line contains n non-negative integer ci(0≤ci≤1000).

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 10−4, your solution will be accepted. 

Sample Input

4

3 2 4 5

5

3 5 99 32 12

Sample Output

6.000000

52.833333

 

题目大意:给你长度为n的ci序列,已知hi是一个全排列,求期望。


思路:


1、首先没有思路的时候,暴力打表,判断每一位上的ci都使用过多少次(累加过多少次)。

发现4的时候是:12 8 8 12

发现5的时候是:60 40 40 40 60

发现6的时候是:360 240 240 240 240 360.


2、这个时候突然开始发愁,我如果每个数字都乘上一个这么大的数,必然会有爆Long Long的存在啊,不必担心,因为期望的分母是情况数,也就是n的阶乘,直接令每一位使用次数除以n的阶乘之后再乘以Ci就好啦!


3、然后我们又发现:

12/24=1/2,8/24=1/3

60/120=1/2,40/120=1/3

360/720=1/2,240/720=1/3


4、那么整理其规律,ans=if(i==0||i==n-1)ans+1/2*ci,else ans+=1/2*ci【0<=i<n】


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        double ans=0;
        for(int i=0;i<n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            if(n==1)
            {
                ans+=tmp;
                continue;
            }
            if(i==0||i==n-1)ans+=tmp*1.0/2*1.0;
            else ans+=tmp*1.0/3*1.0;
        }
        printf("%.6lf\n",ans);
    }
}



用户表: Users +-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | user_name | varchar | +-------------+---------+ user_id 是该表的主键(具有唯一值的列)。 该表中的每行包括用户 ID 和用户名。 注册表: Register +-------------+---------+ | Column Name | Type | +-------------+---------+ | contest_id | int | | user_id | int | +-------------+---------+ (contest_id, user_id) 是该表的主键(具有唯一值的列的组合)。 该表中的每行包含用户的 ID 和他们注册的赛事。 编写解决方案统计出各赛事的用户注册百分率,保留两位小数。 返回的结果表按 percentage 的 降序 排序,若相同则按 contest_id 的 升序 排序。 返回结果如下示例所示。 示例 1: 输入: Users 表: +---------+-----------+ | user_id | user_name | +---------+-----------+ | 6 | Alice | | 2 | Bob | | 7 | Alex | +---------+-----------+ Register 表: +------------+---------+ | contest_id | user_id | +------------+---------+ | 215 | 6 | | 209 | 2 | | 208 | 2 | | 210 | 6 | | 208 | 6 | | 209 | 7 | | 209 | 6 | | 215 | 7 | | 208 | 7 | | 210 | 2 | | 207 | 2 | | 210 | 7 | +------------+---------+ 输出: +------------+------------+ | contest_id | percentage | +------------+------------+ | 208 | 100.0 | | 209 | 100.0 | | 210 | 100.0 | | 215 | 66.67 | | 207 | 33.33 | +------------+------------+ 解释: 所有用户都注册了 208、209 和 210 赛事,因此这些赛事的注册率为 100% ,我们按 contest_id 的降序排序加入结果表中。 Alice 和 Alex 注册了 215 赛事,注册率为 ((2/3) * 100) = 66.67% Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值