Nyoj 233 &&nyoj 322 Sort (tree-like array)

Source: Internet
Author: User

Link: click here

Test instructions

Describe
You want to processe a sequence of n distinct integers by swapping II adjacent sequence elements until the sequence I s sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one Operation:swap 5 and 4.
Input
The input consists of T number of test cases. (<0t<1000) Each case consists of the lines:the first line contains a positive integer n (n <= 1000); The next line contains a permutation of the n integers from 1 to n.
Output
For each case, the output of the minimum times need to sort it in ascending order on a single line.
Sample input
231 2 344 3 2 1
Sample output
06
idea: is to ask for a set of data in reverse order number, tree-like array method, do not explain:

Code:

#include <math.h> #include <queue> #include <deque> #include <vector> #include <stack># Include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include < Iostream> #include <algorithm>using namespace std; #define LOWBIT (a) A&-a#define Max (A, b) a>b?a:b# Define Min (A, B) a>b?b:a#define mem (A, B) memset (A,b,sizeof (a)) int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};const Double EPS = 1e-6;const double Pi = ACOs ( -1.0); static const int inf= ~0u>>2;static const int MAXN =110;int h[100000        1],w[100],map[200];int m[10001], n;void Add (int i) {while (i<=1001) {m[i]++;    I+=lowbit (i);    }}int Sum (int i) {int res=0;        while (i>0) {res+=m[i];    I-=lowbit (i); } return res;    int main () {int t,temp;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        memset (M,0,sizeof (m));        int ans=0; for (int i=1; i<=n; i++) {SCANF ("%d", &temp);            ADD (temp);        ans+= (I-sum (temp));    } printf ("%d\n", ans); } return 0;}
When you want-give up, think for why are you persist until now


Nyoj 233 &&nyoj 322 Sort (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.