Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
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的更多相关文章
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- 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 ...
- 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). 解法:分别从左到右,由右到 ...
- Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- 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 ...
随机推荐
- date.plugin.js 日期插件
//定义命名空间 var DatePlugin; if (!DatePlugin) DatePlugin = {}; /*整理时间:2015-05-28*/ var defaultFormat = & ...
- Android-判断当前网络是否可用
1.声明权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 2. ...
- 构建属于自己的ORM框架之二--IQueryable的奥秘
上篇文章标题乱起,被吐槽了,这次学乖了. 上篇文章中介绍了如何解析Expression生成对应的SQL语句,以及IQueryable的一些概念,以及我们所搭建的框架的思想等.但还没把它们结合并应用起来 ...
- HDU 5875 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- [Everyday Mathematics]20150122
设 $f:[0,1]\to [0,1]$. (1). 若 $f$ 连续, 试证: $\exists\ \xi\in [0,1],\st f(\xi)=\xi$. (2). 若 $f$ 单调递增, 试证 ...
- [Everyday Mathematics]20150111
设 $n$ 阶方阵 $A=(\al_1,\cdots,\al_n)$ 非奇异, $B=(0,\al_2,\cdots,\al_n)$. 试证: $BA^{-1}$, $A^{-1}B$ 的秩均为 $n ...
- delphi ole word
源代码如下: //Word打印(声明部分) wDoc,wApp:Variant; function PrnWordBegin(tempDoc,docName:String):boolean; func ...
- UIButton 设置为圆形,并且使用图片(UIImage)当做背景
-(UIButton *)shareButtonWithIcon:(NSString *)iconName { UIButton *button = [UIButtonbuttonWithType:U ...
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ...
- json字符串转换为JSONObject和JSONArray
一.下载json 具体到http://www.json.org/上找java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了 二.具体转化过程 //JSONObject String ...