Fruit Ninja

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

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
题解:
给你一个1到n的排列,让求满足posx < posy < posz  && x < z < y 的组数有多少。
宇神写的非常好,思路是对于每个数找出后面比a[i]大的数n2;则可以组成的i < j < k && (a[i] < a[j] < a[k] || a[i] < a[k] < a[j])的组合数为C(2,n2);再减去i < j < k && a[i] < a[j] < a[k]就好了,其实就是前面比a[i]小的数乘以后面比a[i]大的数;另外,要用LL
代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
const int MAXN=1e5+;
const int MOD=;
LL tree[MAXN+];
int lowbit(int x){return x&(-x);}
void update(int x,int y){
while(x<=MAXN){
tree[x]++;
x+=lowbit(x);
}
}
LL sum(int x){
LL sum=;
while(x>){
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main(){
int T,N,temp,flot=;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
mem(tree,);
LL n1,n2;
LL ans=;
for(int i=;i<=N;i++){
scanf("%d",&temp);
update(temp,);
n1=sum(temp-);//比temp小的;
n2=N-temp-(i-n1-);
// printf("%d %d\n",n1,n2);
ans+=n2*(n2-)/;
ans-=n1*n2;
}
printf("Case #%d: %lld\n",++flot,ans%MOD);
}
return ;
}

Fruit Ninja(树状数组+思维)的更多相关文章

  1. hdu 4000 Fruit Ninja 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4000 Recently, dobby is addicted in the Fruit Ninja. ...

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

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

  3. hdu 4000Fruit Ninja 树状数组

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

  4. 【树状数组 思维题】luoguP3616 富金森林公园

    树状数组.差分.前缀和.离散化 题目描述 博艾的富金森林公园里有一个长长的富金山脉,山脉是由一块块巨石并列构成的,编号从1到N.每一个巨石有一个海拔高度.而这个山脉又在一个盆地中,盆地里可能会积水,积 ...

  5. noi.ac #44 链表+树状数组+思维

    \(des\) 给出长度为 \(n\) 的序列,全局变量 \(t\),\(m\) 次询问,询问区间 \([l, r]\) 内出现次数为 \(t\) 的数的个数 \(sol\) 弱化问题:求区间 \([ ...

  6. The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)

    Here is a square matrix of n * nn∗n, each lattice has its value (nn must be odd), and the center val ...

  7. [JZOJ 5908] [NOIP2018模拟10.16] 开荒(kaihuang)解题报告 (树状数组+思维)

    题目链接: https://jzoj.net/senior/#contest/show/2529/1 题目: 题目背景:尊者神高达作为一个萌新,在升级路上死亡无数次后被一只大黄叽带回了师门.他加入师门 ...

  8. zoj 2112 单点修改的主席树(树状数组套主席树)

    题目大意: 区间第k大问题+单点修改 基本思路: 这个题有用整体二分,cdq分治,还有主席树+平衡树的,还有就是主席树+树状数组. 我采用的是b站电子科大大佬的主席树写法,尤其喜欢他的离散化方法,所以 ...

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

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

随机推荐

  1. (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

    原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...

  2. php如何在原来的时间上加一天?一小时

    php如何在原来的时间上加一天?一小时? <?phpecho "今天:",date('Y-m-d H:i:s'),"<br>";echo &q ...

  3. HDU 3015 Disharmony Trees

    题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...

  4. 运行于64操作系统上的C#客户端通过WCF访问Oracle数据库不兼容问题

    运行平台: Windows 7  64位操作系统 运行环境: IIS 7 编程语言:C# 数据库: 32位的Oracle 10g 运行原因:64位操作系统C#客户端程序通过WCF访问ORACLE数据库 ...

  5. Java学习之DBUtils工具的学习

    简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影 ...

  6. opencv中遇到的的一些错误

    一:错误提示:OpenCV Error:Bad argument<src and dst have different formats> in unkown function,file.. ...

  7. Js动态设置Img大小

    function ResizePic() {        $('img').each(function () {            var maxWidth = 450; // 图片最大宽度   ...

  8. C++基础梳理--Class、Struct、Union

    C++学习一段时间后,反过头来看我发现我忘了一下最基础的东西:strcut(结构体),union(联合体)我学会了类的一堆东西却忘了这两个最基础的: 现在就好好的重新学习一下这里的东西: 一.Clas ...

  9. Notepad++中Windows,Unix,Mac三种格式

    Notepad++中Windows,Unix,Mac三种格式之间的转换 http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htm ...

  10. jquery自定义分页插件

    //每次只显示5个页码(function ($) { //设定页码方法,初始化 $.fn.setPager = function (options) { var opts = $.extend({}, ...