题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4000

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 ?
 
题意描述:给出n个数1~n的随机排列,找出这样的三元组(x,y,z)的个数:x<y<z && an[x]<an[z]<an[y]。
算法分析:对于每个x,求出x后面比an[x]大的数的个数,那么我们可以得到x**(小中大 和 小大中)的个数,之后在往数列后面遍历的时候,减去原来小中大的情况即可。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long LL;
const int maxn=+;
const int MOD=; int n,an[maxn];
LL x[maxn]; int lowbit(int u) {return u&(-u); }
void add(int i,int dd)
{
while (i<=maxn)
{
x[i] += dd;
i += lowbit(i);
}
}
LL sum(int i)
{
LL ret=;
while (i>)
{
ret += x[i];
i -= lowbit(i);
}
return ret;
} int main()
{
int t,ncase=;scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i= ;i<=n ;i++) scanf("%d",&an[i]);
memset(x,,sizeof(x));
LL ans=;
for (int i= ;i<=n ;i++)
{
add(an[i],);
LL temp1=sum(an[i]-);
LL temp2=n-an[i]-((i-)-temp1);
ans -= temp1*temp2;
if (temp2>=) ans += (temp2-)*temp2/;
}
printf("Case #%d: %I64d\n",ncase++,ans%MOD);
}
return ;
}

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

  1. HDU 4000 Fruit Ninja 树状数组 + 计数

    给你N的一个排列,求满足:a[i] < a[k] < a[j] 并且i < j < k的三元组有多少个. 一步转化: 求出所有满足 a[i] < a[k] < a[ ...

  2. Fruit Ninja(树状数组+思维)

    Fruit Ninja Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 4000 Fruit Ninja (树状数组+反向思维)

    题意:给你一串数且每个数都不同,问你(x,y,z)出现 x<z<y 的总次数 首先我们直接想的话不能使用O(n*log2 n)解决,所以可以正难则反 可以求得x<(y,z)的值,减去 ...

  4. hdu 4000Fruit Ninja 树状数组

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

  5. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  6. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  7. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  8. HDU 4325 Flowers(树状数组)

    Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. HDU 6348 序列计数 (树状数组 + DP)

    序列计数 Time Limit: 4500/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

随机推荐

  1. synchronized的重入

    /** * synchronized的重入 * */ public class SyncDubbo1 { public synchronized void method1(){ System.out. ...

  2. discuz 模拟批量上传附件发帖

    discuz 模拟批量上传附件发帖 简介 对于很多用discuz做资源下载站来说,一个个上传附件,发帖是很繁琐的过程.如果需要批量上传附件发帖,就需要去模拟discuz 上传附件的流程. 插件地址 h ...

  3. 什么是PHP魔术引号

    今天在读EcShop的源码中发现里面有几个地方涉及到了PHP魔术引号,之前也碰到过都忽略过去了,再次碰到该深入的理解,虽然自PHP 5.3.0 起魔术引号被废弃废弃并将自 PHP 5.4.0 起移除, ...

  4. PHP实例 表单数据插入数据库及数据提取 用户注册验证

    网站在进行新用户注册时,都会将用户的注册信息存入数据库中,需要的时候再进行提取.今天写了一个简单的实例. 主要完成以下几点功能: (1)用户进行注册,实现密码重复确认,验证码校对功能. (2)注册成功 ...

  5. C++基本数据类型解惑

    记得刚学C语言的时候,对那些double,float,long,unsigned int各种混乱,基本是随便用,对数据类型没有一个整体的框架.最近学习<<C++ primer plus&g ...

  6. 第五节:什么导致Finalize方法被调用

    Finalize方法在垃圾回收结束时被调用,下面有5种事件会导致开始垃圾回收 1.第0代已满    第0代已满,垃圾回收会自动开始.该事件是目前导致Finalize方法被调用的最常见的一种方式,因为虽 ...

  7. python中split与join

    1.split个人最喜欢的就是它能使输入的一连串数字变为list. str=raw_input("some ") str2=str.split(" ") str ...

  8. 6.python字符串-内置方法列举

    所谓内置方法,就是凡是字符串都能用的方法,这个方法在创建字符串的类中,下面是总结: 首先,我们要学习一个获取帮助的内置函数 help(对象) ,对象可以是一个我们创建出来的,也可以是创建对象的那个类, ...

  9. oracle自定义job名字,job调度

    一.调试创建 begin -- create_schedule dbms_scheduler.create_schedule(schedule_name => 's_change_send_da ...

  10. MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门

    MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshell.c ...