C. Kleofáš and the n-thlon

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/601/problem/C

Description

Kleofáš is participating in an n-thlon - a tournament consisting of n different competitions in n different disciplines (numbered 1 throughn). There are m participants in the n-thlon and each of them participates in all competitions.

In each of these n competitions, the participants are given ranks from 1 to m in such a way that no two participants are given the same rank - in other words, the ranks in each competition form a permutation of numbers from 1 to m. The score of a participant in a competition is equal to his/her rank in it.

The overall score of each participant is computed as the sum of that participant's scores in all competitions.

The overall rank of each participant is equal to 1 + k, where k is the number of participants with strictly smaller overall score.

The n-thlon is over now, but the results haven't been published yet. Kleofáš still remembers his ranks in each particular competition; however, he doesn't remember anything about how well the other participants did. Therefore, Kleofáš would like to know his expected overall rank.

All competitors are equally good at each discipline, so all rankings (permutations of ranks of everyone except Kleofáš) in each competition are equiprobable.

Input

The first line of the input contains two space-separated integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 1000) — the number of competitions and the number of participants respectively.

Then, n lines follow. The i-th of them contains one integer xi (1 ≤ xi ≤ m) — the rank of Kleofáš in the i-th competition.

Output

Output a single real number – the expected overall rank of Kleofáš. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Sample Input

4 10
2
1
2
1

Sample Output

1.0000000000000000

HINT

题意

有n场比赛,每场比赛有m个人参加,每场比赛都会排名次

比赛比完了,但是这个人只知道自己每场比赛的名次,并不知道其他人怎么样

于是让你求出这个人排名的期望值

题解:

期望减一后即为所有方案中得分少于主角的人数之和除去总的方案数,换个角度发现这相当于统计一个人得分少于主角的概率再乘以人数,于是dp[i][j]表示前i项比完后得分为j的概率,维护一下区间和可以优化到O(n^2*m)。

啊,我比较蠢,所以就直接用树状数组来优化了。。。

代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std; int a[];
double dp[][*];
int d[][*];
struct Bit
{
double a[*];
int lowbit(int x)
{
return x&(-x);
}
double query(int x)
{
x++;
if(x<=)return ;
double ans = ;
for(;x;x-=lowbit(x))
ans+=a[x];
return ans;
}
void updata(int x,double v)
{
x++;
if(x<=)return;
for(;x<*;x+=lowbit(x))
a[x]+=v;
}
}T[];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int sum = ;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
a[]=;
if(m==)
return puts("1.00000000000000");
int now = ;
dp[now][]=;
T[now].updata(,1.0);
for(int i=;i<=n;i++)
{
now = now ^ ;
memset(dp[now],,sizeof(dp[now]));
memset(T[now].a,,sizeof(T[now].a));
for(int j=;j<=n*m;j++)
{
double K = (T[now^].query(j-) - T[now^].query(j-m-));
if(j-a[i]>=)K-=dp[now^][j-a[i]];
K*=1.0/(m-1.0);
dp[now][j] += K;
T[now].updata(j,dp[now][j]);
}
}
double res = ;
for(int i=;i<sum;i++)
res+=dp[now][i];
printf("%.15f\n",res*(m-)+1.0);
}

Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp的更多相关文章

  1. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  2. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  3. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  4. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  5. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  6. Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  7. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

  8. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  9. VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组

    B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...

随机推荐

  1. UVA 10047 The Monocycle

    大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...

  2. hdu 3461 Code Lock(并查集)2010 ACM-ICPC Multi-University Training Contest(3)

    想不到这还可以用并查集解,不过后来证明确实可以…… 题意也有些难理解—— 给你一个锁,这个所由n个字母组成,然后这个锁有m个区间,每次可以对一个区间进行操作,并且区间中的所有字母要同时操作.每次操作可 ...

  3. [Everyday Mathematics]20150116

    设 $\al_n\geq 0$ 且 $\dps{\vlm{n}\al_n=0}$, 试求 $$\bex \vlm{n}\frac{1}{n}\sum_{k=1}^n \ln\sex{\frac{k}{ ...

  4. 为redis分配一个新的端口

    为redis分配一个8888端口,操作步骤如下:1.$REDIS_HOME/redis.conf重新复制一份,重命名为redis8888.conf.2.打开redis8888.conf配置文件,找到p ...

  5. LeetCode题解——Palindrome Number

    题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等. 代码: class Solution { public: ...

  6. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  7. CSS 高级:尺寸、分类、伪类、伪元素

    CSS 尺寸:允许你控制元素的高度和宽度.同样,还允许你增加行间距. CSS 分类:允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素,使用绝对值来定位元素,以及元素的 ...

  8. 把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进)

    #把JSON数据载入到页面表单的两种思路(对easyui自带方法进行改进) ##背景 项目中经常需要把JSON数据填充到页面表单,一开始我使用easyui自带的form load方法,觉得效率很低,经 ...

  9. Spring Framework 中启动 Redis 事务操作

    背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ...

  10. vim对erlang语法支持

    发现vim写erlang代码语法缩进都不对,后来发现vim是7.0的,vim7.3开始才对erlang这块进行了支持,所以升级vim git上下载源码包,然后一系列配置安装 http://www.2c ...