【ZOJ3929 The 16th Zhejiang University Programming ContestC】【DP】Deque and Balls n个数放入双端队列2^n种方案有多少个位置
来源:互联网 发布:淘宝网虎皮鹦鹉窝 编辑:程序博客网 时间:2024/06/15 11:05
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
#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int casenum, casei;int n, x;int b[N];int lft[N], rgt[N];void add(int &x, int y){x += y;if (x >= Z)x -= Z;}void modify(int a[], int x, int val){for (; x <= n; x += x&-x)add(a[x], val);}int check(int a[], int x){int ret = 0;for (; x; x -= x&-x)add(ret, a[x]);return ret;}int main(){b[0] = 1;for (int i = 1; i <= 1e5; ++i)b[i] = b[i - 1] * 2 % Z;scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d", &n);for (int i = 1; i <= n; ++i)lft[i] = rgt[i] = 0;scanf("%d", &x);modify(lft, x, 2);modify(rgt, x, 2);int ans = 0;for (int i = 2; i <= n; ++i){scanf("%d", &x);ans = ans * 2 % Z;add(ans, check(lft, x - 1));add(ans, (Z + check(rgt, n) - check(rgt, x)) % Z);modify(lft, x, b[i-1]);modify(rgt, x, b[i-1]);}printf("%d\n", ans);}return 0;}/*【题意】有n(1e5)个数。每个数都有一个权值(1~n)我们有一个双端队列,每个数加入到双端队列的时候,可以加在该队列的前部或者尾部。这样我们最后一共可以生成2^n个队列。我们对于这2^n个队列,都扫描一遍。如果存在一个队列的一个位置i∈[1,n),满足a[i]>a[i+1],那么我们答案的贡献就+1问你最后的答案是多少(mod (1e9+7))【类型】DP 树状数组【分析】这题我们可以DP来做。如何做呢?我们用lft[i]表示数值为i的数出现在整个数列左侧的方案数 用rgt[i]表示数值为i的数出现在整个数列右侧的方案数那么,当新的一个数加进来时——1,之前的所有答案都要*2,因为队列的可能性种数*2了,所以之前的答案也要*22,对于之前的每个lft[i],它依然继续是lft的条件是当前的数放在了右侧,这样的话,对于当步决策,使得之前lft[]变化的延展系数为1,所以lft[i]保留不变3,对于之前的每个rgt[i],它依然继续是rgt的条件是当前的数放在了左侧,这样的话,对于当步决策,使得之前rgt[]变化的延展系数为1,所以rgt[i]保留不变于是,在ans*=2后,当步数新放置后,对答案的贡献是lft[1~x-1]+rgt[x+1~n]这样我们就可以AC啦。【时间复杂度&&优化】O()【数据】input31 2 3outputdetail1 22 11 2 32 1 33 1 23 2 1*/
- 【ZOJ3929 The 16th Zhejiang University Programming ContestC】【DP】Deque and Balls n个数放入双端队列2^n种方案有多少个位置
- The 16th Zhejiang University Programming Contest-
- 期望 zoj3929 Deque and Balls
- 【ZOJ3938 The 13th Zhejiang Provincial Collegiate Programming ContestC】【简单模拟】Defuse the Bomb 5个阶段正确决策
- The 16th Zhejiang University Programming Contest - E
- The 12th Zhejiang University Programming Contest
- The 14th Zhejiang University Programming Contest
- The 14th Zhejiang University Programming Contest
- [The 14th Zhejiang University Programming Contest]
- The 15th Zhejiang University Programming Contest
- The 15th Zhejiang University Programming Contest
- The 15th Zhejiang University Programming Contest
- The 15th Zhejiang University Programming Contest
- zoj3929 Deque and Balls 【找规律】
- 【ZOJ3939 The 13th Zhejiang Provincial Collegiate Programming ContestD】【日期前缀和预处理】The Lucky Week 第n个幸运
- 【ZOJ3931 The 16th Zhejiang University Programming ContestE】【小根堆 哈夫曼树 DP】Exact Compression 建立哈弗曼树并编码0
- 【ZOJ3927 The 16th Zhejiang University Programming ContestA】【水题】Programming Ability Test 简单求和比大小
- The 11th Zhejiang University Programming Contest / 解题报告 4.3
- 单单Scrum是不够的
- ACM_模板_求一个数的所有因子和(预处理打表)
- Linux 基础--D1
- 干细胞修复疗法动物实验获成功
- 微信支付,少1分钱bug
- 【ZOJ3929 The 16th Zhejiang University Programming ContestC】【DP】Deque and Balls n个数放入双端队列2^n种方案有多少个位置
- 工作常见问题汇总
- 单词表达中的语言规律Linguistic Regularities in word representations
- uva 10790
- 大型分布式C++框架《三:序列化与反序列化》
- LintCode_165_合并两个排序链表
- WLAN与Wi-Fi的联系与区别
- HDU 2018 母牛的故事
- Java异常处理机制