Example 3.7 Ping pong match UVa1428

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Solution thinking: According to test instructions, consider the case of the first person when the referee, if the 1th person to the i-1 person, there is NUM1 personal experience value is less than a[i], then there is i-1-num1 personal experience is greater than a[i]; Similarly, assuming that section i+ 1 person to nth person's opinion have num2 personal experience value is less than a[i], then there is n-i-num2 personal experience value is greater than a[i]. Therefore, according to the multiplication principle, the num1* (I-1-NUM1) +num2* (n-i-num2) race is the first person to be the referee. Finally add up.

But how do you calculate the num1,num2 here? It is not difficult to find, from left to right scan the first person, the a[i] as subscript in the C array, assuming that there is another array x,X[a[i]] indicates how many of the former I-1 people experience value equals A[i]. Then NUM1 is the prefix and x[1]+x[2]+...+x[a[i]-1]. After statistics NUM1, then make x[j]++ can, and this is the standard use of bit! Similarly, you can scan in reverse order and calculate num2. But here we don't need to open an extra x array, just a C array, and the introduction of array x is just for easy understanding. As can be seen from the above narrative, the calculation prefix and should be treated as "subscript" in the C array a[i .

The subject also has a skill: in order to facilitate the management of the number of races when I am a referee, a structure is used to record the number of people who have experience value less than a[i] in the previous i-1 individuals lmin, which is greater than the number of people lmax; Use Rmin,rmax to indicate after N-i personal experience value is less than a[i] The number of people. Facilitates subsequent accumulation of results.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std;typedef long long LL; #define N 100000+10# Define M 20000+10int c[n];int a[m];struct node{int Lmax, Lmin;int rmax, Rmin;} F[m];int lowbit (int x) {return x&-x;} int sum (int x)///Here X to be understood as subscript for C array {int ret = 0;while (x > 0) {ret + = c[x];x-= lowbit (x);} return ret;} void Add (int x, int D)//x understanding of {while (x <= N) {C[x] + d;x + = Lowbit (x);}} int main () {freopen ("T.txt", "R", stdin), int t;scanf ("%d", &t), while (t--) {int n;scanf ("%d", &n); for (int i = 1; i <= N; i++) scanf ("%d", A + i), memset (c, 0, sizeof (c)), for (int i = 1; I <= n; i++)//I personal when referee, consider before i-1 personal {int num = SUM (a[i]);//Calculation Former i-1 personalHow many people experience a value less than a[i]f[i].lmin = Num;f[i].lmax = i-1-Num;add (A[i], 1);//Update c[i]}memset (c, 0, sizeof (c)); for (int i = n; I & Gt;= 1; i--)//I personal when referee, consider after n-i personal {int num = SUM (a[i]);//After calculation n-i How many people experience value less than a[i]f[i].rmin = Num;f[i].rmax = N-i-Num;add (A[i], 1); Update C[i]}ll ans = 0;for (int i = 1; I <= n; i++) ans + = (ll) F[i].lmax*f[i].rmin + (LL) f[i].lmin*f[i].rmax;printf ("%lld\n ", ans);} return 0;}

Example 3.7 Ping pong match UVa1428

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.