ZOJ 3932 Deque and Balls
来源:互联网 发布:厦门广电网络数字电视 编辑:程序博客网 时间:2024/05/18 20:11
There are n balls, where the i-th ball is labeled as pi. You are going to put n balls into a deque. In the i-th turn, you need to put the i-th ball to the deque. Each ball will be put to both ends of the deque with equal probability.
Let the sequence (x1, x2, ..., xn) be the labels of the balls in the deque from left to right. The beauty of the deque B(x1, x2, ..., xn) is defined as the number of descents in the sequence. For the sequence (x1, x2, ..., xn), a descent is a position i (1 ≤ i < n) with xi > xi+1.
You need to find the expected value of B(x1, x2, ..., xn).
Deque is a double-ended queue for which elements can be added to or removed from either the front (head) or the back (tail).
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (2 ≤ n ≤ 100000) -- the number of balls. The second line contains n integers: p1, p2, ..., pn (1 ≤ pi ≤ n).
Output
For each test case, if the expected value is E, you should output E⋅2n mod (109 + 7).
Sample Input
221 232 2 2
Sample Output
20
第一次遇到求期望的DP题目,每一个数a[i],都可以和前面的数相邻,只要不和自己相同都可以产生新的逆序。所以加上i-1已经
产生的逆序数*2,再加上新产生的逆序数,要减去和相邻是自己相同的数的排列,用一个数组去维护这个排列数
#include <iostream>#include <string.h>#include <algorithm>#include <stdlib.h>#include <math.h>#include <stdio.h>using namespace std;typedef long long LL;const LL mod=1e9+7;#define MAX 100000LL dp[MAX+5];LL p[MAX+5];LL num[MAX+5];int n;int fun(){ p[0]=0;p[1]=1; for(int i=2;i<=MAX+5;i++) { p[i]=(p[i-1]*2)%mod; }}int main(){ int t; scanf("%d",&t); int a; fun(); while(t--) { scanf("%d",&n); memset(dp,0,sizeof(dp)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++) { scanf("%d",&a); dp[i]=(2*dp[i-1]+p[i-1]-num[a]+mod)%mod; if(i==1) num[a]=(num[a]+1)%mod; else num[a]=(num[a]+p[i-1])%mod; } dp[n]=(dp[n]*2)%mod; printf("%lld\n",dp[n]); } return 0;}
- ZOJ 3932 Deque and Balls
- ZOJ 3929 Deque and Balls
- ZOj 3929Deque and Balls
- ZOJ 3929 Deque and Balls
- ZOJ 3929 Deque and Balls
- ZOJ-3929-Deque and Balls【思维】【好题】
- zoj 3929 Deque and Balls 树状数组 递推
- 期望 zoj3929 Deque and Balls
- ZOJ 3929 Deque and Balls (放球问题_DP思维好题)
- zoj3929 Deque and Balls 【找规律】
- ZJU/ZOJ Deque and Balls 3929 计数:小的情况对总情况的贡献。 小心mod运算陷阱
- Bear and Three Balls
- Bear and Three Balls
- Bear and Three Balls
- Balls and Boxes
- Bear and Three Balls
- Bear and Three Balls
- Bear and Three Balls
- c++ 大整数类
- 基于数字高程模型的冲沟提取
- E舞成名模拟器分析及下载地址
- nyoj 71
- Hdu 1432 Lining Up【简单几何】
- ZOJ 3932 Deque and Balls
- 架设NFS服务器
- hdoj--1881--毕业bg(dfs)
- numpy.array()
- liunx bash 学习记录1
- Android学习(声明)
- 关于const是否分配空间和常量折叠的问题
- javascript入门
- nyoj 6