hdu 4000 Fruit Ninja 树状数组

题目链接: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**(小中大 和 小大中)的个数,之后在往数列后面遍历的时候,减去原来小中大的情况即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 const int maxn=100000+10;
11 const int MOD=100000007;
12
13 int n,an[maxn];
14 LL x[maxn];
15
16 int lowbit(int u) {return u&(-u); }
17 void add(int i,int dd)
18 {
19     while (i<=maxn)
20     {
21         x[i] += dd;
22         i += lowbit(i);
23     }
24 }
25 LL sum(int i)
26 {
27     LL ret=0;
28     while (i>0)
29     {
30         ret += x[i];
31         i -= lowbit(i);
32     }
33     return ret;
34 }
35
36 int main()
37 {
38     int t,ncase=1;scanf("%d",&t);
39     while (t--)
40     {
41         scanf("%d",&n);
42         for (int i=1 ;i<=n ;i++) scanf("%d",&an[i]);
43         memset(x,0,sizeof(x));
44         LL ans=0;
45         for (int i=1 ;i<=n ;i++)
46         {
47             add(an[i],1);
48             LL temp1=sum(an[i]-1);
49             LL temp2=n-an[i]-((i-1)-temp1);
50             ans -= temp1*temp2;
51             if (temp2>=2) ans += (temp2-1)*temp2/2;
52         }
53         printf("Case #%d: %I64d\n",ncase++,ans%MOD);
54     }
55     return 0;
56 }
时间: 2024-08-02 10:58:33

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

Fruit Ninja(树状数组+思维)

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 e

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it 

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank

HDU 1541 Stars(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 解析: 题意:大概就是计算每颗星星左下边包括了多少颗星星,这个数值就是level.左下边不包括本身,不超过本身的x,y的坐标,可以等于.问每种level有多少颗星星. 这题,一开始想不到怎么用到树状数组,后来看了一下,发现题目给的数据是已经按x,y排好序的,所以我们可以不用管y的值. 注意: 1.每次输入一个坐标对之后,都要计算一下这个它的level. 2.此题的x坐标可以为0,而树状数组是从

HDU 1892 二维树状数组

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3485    Accepted Submission(s): 1103 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kno

hdu 4000 Fruit Ninja(树状数组)

题目大意是给定一串1到n的排列(设为数组a),求其中满足a[x]<a[z]<a[y]的排列个数,其中x<y<z 直接求这样的排列个数并不好求,我们可以转化为求a[x]<a[z]<a[y]+a[x]<a[y]<a[z]的个数减去a[x]<a[z]<a[y]的个数 用left数组记录i位置前比a[i]小的元素个数,left数组可由树状数组预处理得到,那么我们可以得到求排列个数的公式(具体见码) #include<cstdio> #incl