HDU 4000 Fruit Ninja (tree-like array)

Source: Internet
Author: User

The main idea is given a string of 1 to n arrangement (set to an array of a), which satisfies a[x]<a[z]<a[y], where the number of permutations, where x<y<z

The number of such permutations is not so good that we can convert to the number of a[x]<a[z]<a[y]+a[x]<a[y]<a[z] minus a[x]<a[z]<a[y].

Using the left array to record the number of elements that are smaller than a[i] before the position of I, then we can get the formula for the number of permutations (see Code), which can be preprocessed by a tree array.

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt  #include <string> #include <map> #include <set>using namespace std; #define LL Long Long const LL MAXN = 100000 + 5; LL c[2 * MAXN], LEF[MAXN], N; The lef array represents the number of smaller numbers before I than a[i] LL lowbit (ll x) {return x& (x);} void Add (ll x, ll D) {while (x <= N) {c[x] + = D; x + = Lowbit (x);}} ll sum (ll x) {ll ret = 0;while (x > 0) {ret + = c[x]; x-= Lowbit (x);} return ret;} int main () {LL T, Kase = 0; scanf ("%i64d", &t), while (t--) {memset (c, 0, sizeof (c)); scanf ("%i64d", &n);  ll ans = 0;for (ll i = 1; I <= n; i++) {LL tmp;scanf ("%i64d", &tmp), lef[i] = SUM (TMP), add (TMP, 1),//PRLLF ("%i64d\n", Lef[i]); ans = ans + (n + lef[i] + 1-tmp-i) * (n + lef[i]-tmp-i)/2-(n + lef[i] + 1-tmp-i) * Lef[i];} printf ("Case #%i64d:%i64d\n", ++kase, ans% 100000007);} return 0;}




HDU 4000 Fruit Ninja (tree-like array)

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.