Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4000
recently, Dobby is addicted in the Fruit Ninja. As you know, Dobby are a free elf, so unlike other elves, he could do whatever he wants. But the hands of the Elves is somehow strange, so if he cuts the fruit, he can only do 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 was the lowest, point B was the highest, and point C was in the middle. Another elf, Kreacher, is isn't interested in cutting fruits, but he's very interested in numbers. Now, he wonders, give you a permutation of 1 to N, what many triples that makes such a relationship can you find? That's, how many (x, y, Z) can-find such that x < Z < y? Test Instructions Description: Give a random arrangement of n number 1~n to find out the number of such triples (x, Y, z): X<y<z && an[x]<an[z]<an[y]. algorithm Analysis: For each x, to find out the number of x is larger than an[x], then we can get the number of x** (small medium and small large), and then in the back of the sequence, minus the original small medium large case can be.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineINF 0x7fffffff8 using namespacestd;9typedefLong LongLL;Ten Const intmaxn=100000+Ten; One Const intMod=100000007; A - intN,AN[MAXN]; - LL X[MAXN]; the - intLowbit (intu) {returnu& (-u); } - voidAddintIintdd) - { + while(i<=MAXN) - { +X[i] + =DD; Ai + =lowbit (i); at } - } -LL sum (inti) - { -LL ret=0; - while(i>0) in { -RET + =X[i]; toI-=lowbit (i); + } - returnret; the } * $ intMain ()Panax Notoginseng { - intT,ncase=1; scanf ("%d",&t); the while(t--) + { Ascanf"%d",&n); the for(intI=1; i<=n; i++) scanf ("%d",&an[i]); +memset (x,0,sizeof(x)); -LL ans=0; $ for(intI=1; i<=n; i++.) $ { -Add (An[i],1); -LL Temp1=sum (an[i]-1); theLL temp2=n-an[i]-((i-1)-temp1); -Ans-= temp1*Temp2;Wuyi if(temp2>=2) ans + = (temp2-1) *temp2/2; the } -printf"Case #%d:%i64d\n", ncase++,ans%MOD); Wu } - return 0; About}
HDU 4000 Fruit Ninja tree-like array