hdu 5753

Permutation Bo

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


Problem Description
There are two sequences h1hn and c1cnh1hn 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
 

 题意:给你一个有n个数的数列c1~cn,h1~hn为1~n的排列,求ci[hi>hi-1  and  hi>hi+1]的期望和。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
const double eps = 1e-10;
const int  inf =0x7f7f7f7f;
const double pi=acos(-1);
const int mod=1e9+7;
const int maxn=100000+10;

#define MM(a,b) memset(a,b,sizeof(a));
#define FOR(i,n) for(int i=1;i<=n;i++)
#define SC scanf
#define PF printf
#define PB push_back
#define CT continue

double c[1009];
int main()
{
    int n;
    while(~SC("%d",&n)){
        FOR(i,n) SC("%lf",&c[i]);
        double ans=(c[1]+c[n])/2;
        for(int i=2;i<=n-1;i++){
            ans+=c[i]/3;
        }
        PF("%.9f\n",ans);
    }
    return 0;
}

  总是看了题解后才觉得题目好水,,,需要改变一下比赛时的心态了,,

 因为假设两端的数字为大和小两种情况,中间的数字为大中小三种情况;

002 Permutation Bo

根据期望的线性性,我们可以分开考虑每个位置对答案的贡献。

可以发现当ii不在两边的时候和两端有六种大小关系,其中有两种是对答案有贡献的。

那么对答案的贡献就是\frac{c_i}{3}3ci​​​​

在两端的话有两种大小关系,其中有一种对答案有贡献。

那么对答案的贡献就是\frac{c_i}{2}2ci​​​​

复杂度是O(n)O(n)。

注意特判n=1n=1的情况。

 

posted @ 2016-07-27 21:01  快点说我帅  阅读(200)  评论(0编辑  收藏  举报