Ping pong (poj 3928)

Source: Internet
Author: User

Evaluation Portal

Question:

T-group data, N numbers, 3 are selected in order, and the sum of all the number pairs that meet the monotonic increasing or monotonic decreasing conditions is obtained.

Sample Input

1 3 1 2 3

Sample output

1
Ideas:

This is an advanced method of reverse order, because it is not just a reverse order.

We can first find all the reverse order pairs based on the conventional practice, but let's think about who is this reverse order?

After you create a table, you will find that this is the reverse order of the prefixes of all numbers. If we calculate the reverse order of all suffixes, isn't multiplication the correct number of all monotonically decreasing numbers?

The same is true for the order.

Note:

Don't open long to see our ancestors, ten years of OI empty

Code
# Include <stdio. h> # include <algorithm> # include <string. h> # define lowbit (x) X &-x using namespace STD; const int mxn = 20010; struct node {int Val, ID;} A [mxn]; bool operator <(const node & X, const node & Y) {return X. val <Y. val;} int N, F [mxn] [2], C [mxn]; void add (int x) {While (x <= N) {c [x] ++; x + = lowbit (x) ;}} int ask (int x) {int Re = 0; while (X) {re + = C [X]; X-= lowbit (x);} return re;} int main () {int t; scanf ("% d ", & T); While (t --) {long ans = 0; scanf ("% d", & N); For (INT I = 1; I <= N; ++ I) scanf ("% d", & A [A [I]. id = I]. val); sort (a + 1, A + 1 + n); memset (C, 0, sizeof (c); For (INT I = N; I; -- I) {Add (A [I]. ID); F [I] [0] = Ask (A [I]. ID-1); // forward/backward f [I] [1] = Ask (N)-ask (A [I]. ID); // backward peer} memset (C, 0, sizeof (c); For (INT I = 1; I <= N; ++ I) {Add (A [I]. ID); ans + = f [I] [1] * Ask (A [I]. ID-1); // backward x forward X ans + = f [I] [0] * (ASK (N)-ask (A [I]. ID); // forward/backward * backward} printf ("% LLD \ n", ANS);} return 0 ;}

 

Ping pong (poj 3928)

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.