Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2048    Accepted Submission(s): 805

Problem Description
Recently, dobby is addicted in the Fruit Ninja. As you know, dobby is a free elf, so unlike other elves, he could do whatever he wants.But the hands of the elves are somehow strange, so when he cuts the fruit, he can only make specific move of his hands. Moreover, he can only start his hand in point A, and then move to point B,then move to point C,and he must make sure that point A is the lowest, point B is the highest, and point C is in the middle. Another elf, Kreacher, is not interested in cutting fruits, but he is very interested in numbers.Now, he wonders, give you a permutation of 1 to N, how many triples that makes such a relationship can you find ? That is , how many (x,y,z) can you find such that x < z < y ?
 
Input
The first line contains a positive integer T(T <= 10), indicates the number of test cases.For each test case, the first line of input is a positive integer N(N <= 100,000), and the second line is a permutation of 1 to N.
 
Output
For each test case, ouput the number of triples as the sample below, you just need to output the result mod 100000007.
 
Sample Input
2
6
1 3 2 6 5 4
5
3 5 2 4 1
 
Sample Output
Case #1: 10
Case #2: 1
 
Source
 

题意:给出1~N的一种排列,问存在多少个三元组(x,y,z) x < z < y 其中x y z 的位置递增

可先求出满足 x < y < z 和x < z < y的总数tot, 总数为:对于每一个数 x, 从x后面的位置中比x大的num个数中选择任意两个, 即 tot += comb[ num ][2]

再从总数tot中减去 x < y < z的数量即为答案, x < y < z 的个数为: 对于一个数y, 在y的前面比 y小的个数为 low, 在y的后面比y大的个数为 high, 根据组合原理,在low中选一个x, 在high中选一个z,共有low × high中情况

如何求 每个数的low值和high值, 就用树状数组来统计

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#define Lowbit(x) ((x) & (-x))
using namespace std;
typedef long long LL;
const int N = 100005;
const int M = 100000007;
int c[N], a[N];
LL low[N], high[N], comb[N][5];
int n;
void pre_c()
{
for(int i = 0; i < N; ++i)
for(int j = 0; j <= min(i,2); ++j)
comb[i][j] = (i == 0 || j == 0) ? 1:((comb[i - 1][j] % M + comb[i - 1][j - 1]) % M);
}
void update(int pos)
{
while(pos <= n) {
c[pos]++;
pos += Lowbit(pos);
}
}
LL sum(int pos)
{
LL res = 0;
while(pos) {
res += c[pos];
pos -= Lowbit(pos);
}
return res;
}
int main()
{
int _, v, cas = 1;
pre_c();
scanf("%d", &_);
while(_ --)
{
scanf("%d", &n);
memset(c, 0, sizeof c);
for(int i = 1; i <= n; ++i) {
scanf("%d", &v);
a[i] = v;
low[i] = sum(v); update(v);
high[i] = (n - v) - (i - 1 - low[i]);
} // for(int i = 1; i <= n; ++i) printf("%lld %lld\n", low[i], high[i]);
LL tot = 0;
for(int i = 1; i <= n; ++i) {
tot = tot % M + comb[ high[i] ][2];
tot = (tot - (low[i] % M * high[i] % M) + M) % M;
}
printf("Case #%d: %lld\n",cas++, tot % M ); }
}

  

hdu 4000Fruit Ninja 树状数组的更多相关文章

  1. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  2. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. hdu 4046 Panda 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...

  4. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  5. HDU 5493 Queue 树状数组

    Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...

  6. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  7. hdu 1541 (基本树状数组) Stars

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...

  8. hdu 4031(树状数组+辅助数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others)    ...

  9. HDU 4325 Flowers 树状数组+离散化

    Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...

随机推荐

  1. 20145213《Java程序设计》第一周学习总结

    20145213<Java程序设计>第一周学习总结 教材学习内容总结 期待了一个寒假,终于见识到了神秘的娄老师和他的Java课.虽说算不上金风玉露一相逢,没有胜却人间无数也是情理之中,但娄 ...

  2. selinux

    root@lujie ~]# vim /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # ...

  3. Get与Post数据长度的限制

    这个问题在我的开发中也遇到,所以在此贴出来(也是在网上搜出来的,呵呵)这是原贴地址http://blog.csdn.net/somat/archive/2004/10/29/158707.aspx两个 ...

  4. 常用邮箱的服务器(SMTP/POP3)地址和端口总结

    163.com: POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25) 126邮箱: POP3服务器地址:pop.126.com(端口 ...

  5. qt_文本编辑器实现_附带详细注释和源码下载

    源码下载: 链接: http://pan.baidu.com/s/1c21EVRy 密码: qub8 实现主要的功能有:新建,打开,保存,另存为,查找(查找的时候需要先将光标放到最下面位置才能查全,不 ...

  6. Linux解决关闭终端后终止服务问题

    可使用nohup. 具体使用方法,参见:http://zjking.blog.51cto.com/976858/1117828

  7. Codeforces Round #324 (Div. 2) C (二分)

    题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...

  8. PAL/NTSC 制电视广播技术有关知识--FPGA

    1.PAL和NTSC的区别 常见的电视信号制式是PAL和NTSC,另外还有SECAM等. NTSC即正交平衡调幅制,PAL为逐行倒像正交平衡调幅制. (1)PAL电视标准  PAL电视标准,每秒25帧 ...

  9. mysqli扩展库的预处理技术 mysqli stmt

    //预编译演示 //1,创建mysqli对象 $mysqli=new mysqli("localhost","root",""," ...

  10. lsof -ntP -i:端口取出 动行程序的PID 然后xargs kill -9 这个进程

    [root@ok ok]# lsof -ntP -i: [root@ok ok]# netstat -lnutp|grep tcp /dnsmasq tcp /sshd tcp ::: :::* LI ...