ZOJ 3932 Deque and Balls
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
2
2
1 2
3
2 2 2
Sample Output
2 0 第一次遇到求期望的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 100000
LL 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的更多相关文章
- 期望+DP ZOJ 3929 Deque and Balls
题目链接 题意:给你n个数,按照顺序依次放入一个双端队列(可放在头部,也可以放在尾部),求xi > xi+1的期望 * 2^n mod (1e9 +7) 分析:期望*2^n=出现这种排法的概率* ...
- ZOJ 3929 Deque and Balls
答案=所有情况中总共递减次数*2 放完i个和放完i-1个之间的递减次数是可以递推的. 有一部分是放完i-1个之后产生的,还有一部分是放完第i个之后新产生的. 注意减去多加的部分. 2的i次方可以打个表 ...
- 【ZOJ 3929】Deque and Balls(普通dp)
题意:给出一个序列,按照顺序一个一个放入双端队列(可以放在头部也可以放在尾部),一个队列的美丽指数就是数列中a[i]>a[i+1]的个数,求美丽指数的期望*2^n的值. 解题思路:方便起见,我们 ...
- ZOJ - 3932 Handshakes 【水】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3932 题意 给出 N 个人,然后 1-N 然后 从 1 - N ...
- ZOJ-3929 Deque and Balls (DP+找规律)
题目大意:n个数,每个数的大小都在1~n之间.操作n次,第 i 次将第 i 个数放到一个双端队列里面,放到队列两端的概率是相等的.问操作n次之后双端队列中元素满足xi>xi+1的对数的期望,输出 ...
- ZOJ 3932 Handshakes
Last week, n students participated in the annual programming contest of Marjar University. Students ...
- Handshakes(思维) 2016(暴力)
Handshakes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
随机推荐
- Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处。用来指定播放器 1 2. <object> 标签用于包含对象,比如图像、音
Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处.用来指定播放器 1 2. <object> 标签用于包含对象,比如 ...
- shell脚本中执行mysql命令
1.mysql -hhostname -uuser -ppsword -e "mysql_cmd" 2. mysql -hhostname -uuser -ppsword < ...
- spring中事务传播解读:PROPAGATION_REQUIRES_NEW
第一步:获取事务状态,判断当前事务线程是否存在.第二步:如果当前事务的传播行为为PROPAGATION_REQUIRES_NEW,挂起当前线程绑定的事务,取消当前事务的sessionHolder和co ...
- C语言 · 数组查找及替换
算法训练 数组查找及替换 时间限制:1.0s 内存限制:512.0MB 问题描述 给定某整数数组和某一整数b.要求删除数组中可以被b整除的所有元素,同时将该数组各元素按从小到大排序. ...
- [I2C]pca9555应用层测试代码
注意点: 如果在设置I2C_SLAVE的时候,提示device_busy,可以使用I2C_SLAVE_FORCE, 在驱动里面二者对应同一个case语句 应用层可以调用接口:i2c_smbus_wri ...
- div上下滚动
<div class="table-scrollable" style='height:601px'></div> <style> .table ...
- cmake 安装 mysql5.5 版本
1.安装确保以下系统相关库文件 gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libmcrypt* libtool*(libtoo ...
- [velocity] velocity详解
(1)为什么要使用模版语言? 在服务器端可以使用 Velocity 处理模板和生成的动态内容(HTML.XML等).这和 JSP 技术的目标非常接近.但是,JSP 模型可以毫无阻碍地访问底层的 Ser ...
- 我为什么喜欢Go语言123123
从2000年至今,也写了11年代码了,期间用过VB.Delphi.C#.C++.Ruby.Python,一直在寻找一门符合自己心意和理念的语言.我很在意写代码时的手感和执行的效率,所以在Go出现之前一 ...
- Softmax vs. Softmax-Loss VS cross-entropy损失函数 Numerical Stability(转载)
http://freemind.pluskid.org/machine-learning/softmax-vs-softmax-loss-numerical-stability/ 卷积神经网络系列之s ...