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 (x1x2, ..., xn) be the labels of the balls in the deque from left to right. The beauty of the deque B(x1x2,
..., xn) is defined as the number of descents in the sequence. For the sequence (x1x2, ..., xn), a descent is a position i (1 ≤ i < n)
with xi > xi+1.

You need to find the expected value of B(x1x2, ..., 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: p1p2,
..., 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的更多相关文章

  1. 期望+DP ZOJ 3929 Deque and Balls

    题目链接 题意:给你n个数,按照顺序依次放入一个双端队列(可放在头部,也可以放在尾部),求xi > xi+1的期望 * 2^n mod (1e9 +7) 分析:期望*2^n=出现这种排法的概率* ...

  2. ZOJ 3929 Deque and Balls

    答案=所有情况中总共递减次数*2 放完i个和放完i-1个之间的递减次数是可以递推的. 有一部分是放完i-1个之后产生的,还有一部分是放完第i个之后新产生的. 注意减去多加的部分. 2的i次方可以打个表 ...

  3. 【ZOJ 3929】Deque and Balls(普通dp)

    题意:给出一个序列,按照顺序一个一个放入双端队列(可以放在头部也可以放在尾部),一个队列的美丽指数就是数列中a[i]>a[i+1]的个数,求美丽指数的期望*2^n的值. 解题思路:方便起见,我们 ...

  4. ZOJ - 3932 Handshakes 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3932 题意 给出 N 个人,然后 1-N 然后 从 1 - N ...

  5. ZOJ-3929 Deque and Balls (DP+找规律)

    题目大意:n个数,每个数的大小都在1~n之间.操作n次,第 i 次将第 i 个数放到一个双端队列里面,放到队列两端的概率是相等的.问操作n次之后双端队列中元素满足xi>xi+1的对数的期望,输出 ...

  6. ZOJ 3932 Handshakes

    Last week, n students participated in the annual programming contest of Marjar University. Students ...

  7. Handshakes(思维) 2016(暴力)

    Handshakes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. 主席树[可持久化线段树](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 ...

随机推荐

  1. 小米手机解锁bootload教程及常见问题

    小米手机解锁bl需要在官网提交申请,然后电脑解锁,具体步骤如下: 1.首先需要注册一个小米账号,并登陆. 2.到官网解锁网页提交申请:http://www.miui.com/unlock/index. ...

  2. ngModelController的方法和属性的使用

    ngModelController方法 $render(); 当视图需要更新的时候会被调用.使用ng-model的指令应该自行实现这个方法. $isEmpty(value); 该方法用于判断输入值是否 ...

  3. 严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.Invoc

    错误提示:严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTa ...

  4. 李洪强iOS开发之数据存储

    李洪强iOS开发之数据存储 iOS应用数据存储的常用方式 1.lXML属性列表(plist)归档 2.lPreference(偏好设置) 3.lNSKeyedArchiver归档(NSCoding) ...

  5. SAP ERP 6.0 EHP7 SR2(WINDOWS MSSQL版)安装说明

    原文 by 枫竹丹青 ⋅ 1.安装准备 1.1.版本说明 本文是描述在一个Windows虚拟机.SQL Server数据库环境下,安装SAP ERP 6.0 EHP7 SR2服务器,安装完成虚拟机文件 ...

  6. error: No implicit Ordering defined for Any

    scala中经常遇到最头疼的问题,就是类型不匹配或者带Any,Option的提示错误信息. 最近碰到的是取最大值,但是明明已经Long类型的,却提示下面这个错误信息. 相关的源程序如下: // 获取o ...

  7. 大数据处理-Bloom Filter

    大数据处理--Bloom Filter 布隆过滤器(Bloom Filter)是由巴顿.布隆于一九七零年提出的.它实际上是一个很长的二进制向量和一系列随机映射函数. 如果想判断一个元素是不是在一个集合 ...

  8. Maple重点知识总结

    Maple中的evalf与evalhf evalf 可作用于单值 可作用于List 可作用于Set 可作用于Vector(<..>) 可作用于Matrix(<..|..|..> ...

  9. SQL语句字符串处理大全

    常用的字符串函数有: 一.字符转换函数 1.ASCII() 返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来,但含其它字符的字符串必须用‘’括起来使用 ...

  10. Linux上的free命令简介

    每次使用free时都比较迷惑,对于上面的内容一直都不是很清楚,今天仔细查了以下,和大家一起分享以下: 先看一下free的运行结果: free打印出的内存信息主要分为两种,一种是安装的内存,一种是用磁盘 ...