题目链接: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
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) .
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) .
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.
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
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;
}