Fruit Ninja (tree-like array + thinking)

Source: Internet
Author: User

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 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?

Input The first line contains a positive integer t (t <=), indicates the number of test cases. For each test case, the first line of input was 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 1000000 07.

Sample Input261 3 2 6 5 45 3 5 2 4 1

Sample outputcase #1:10Case #2:1 Solution: Yushen write very well, the idea is to find the number of each number behind a[i] large n2;I < J < K && (A[i] < A[J] < A[k] | | a[i] < A[K] < A[j])to C (2,N2);I < J < K && A[i] < A[j] < A[k]Better, is actually the front than a[i] small number multiplied by the back than A[i] large number, in addition, to use the LL code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 #defineMem (x, y) memset (x,y,sizeof (x))8typedefLong LongLL;9 Const intmaxn=1e5+ -;Ten Const intMod=100000007; OneLL tree[maxn+1]; A intLowbit (intx) {returnx& (-x);} - voidUpdateintXinty) { -      while(x<=MAXN) { thetree[x]++; -x+=lowbit (x); -     } - } +LL sum (intx) { -LL sum=0; +      while(x>0){ Asum+=Tree[x]; atx-=lowbit (x); -     } -     returnsum; - } - intMain () { -     intt,n,temp,flot=0; inscanf"%d",&T); -      while(t--){ toscanf"%d",&N); +Mem (Tree,0); - LL n1,n2; theLL ans=0; *          for(intI=1; i<=n;i++){ $scanf"%d",&temp);Panax NotoginsengUpdate (temp,1); -N1=sum (temp-1);//smaller than the temp; then2=n-temp-(i-n1-1); +         //printf ("%d%d\n", n1,n2); Aans+=n2* (n2-1)/2; theAns-=n1*N2; +         } -printf"Case #%d:%lld\n", ++flot,ans%MOD); $     } $     return 0; -}

Fruit Ninja (tree-like array + thinking)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.