HDU 5775 Bubble Sort (multi-university Training Contest 4--tree-like array)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5775

Bubble SortTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) Total Submission (s): 636 Accepted Submission (s): 378

Problem Descriptionp is a permutation of the integers from 1 to N (index starting from 1).
Here are the code of Bubble Sort in C + +.
for (int i=1;i<=n;++i) for    (int j=n,t;j>i;-j)        if (p[j-1] > P[j])            t=p[j],p[j]=p[j-1],p[j-1]=t;

After the sort, the array was in increasing order. ?? Wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.
Inputthe first line of the input gives the number of test cases T; T test Cases follow.
Each consists of one line with one integer N, followed by another line with a permutation of the integers from 1 to N, Inc Lusive.

Limits
T <= 20
1 <= N <= 100000
N is larger than 10000 in only one case.

Outputfor each test case output ' case #x: y1 y2 ... yN ' (without quotes), where x is the ' Test Case Number ' (starting from 1), And Yi is the difference of rightmost place and leftmost place of number I.
Sample Input
233 1 231) 2 3

Sample Output
Case #1:1 1 2Case #2:0 0 0Hintin first case, (3, 1, 2), (3, 1, 2)--(1, 3, 2)--(1, 2, 3) the LEFTM OST place and rightmost place of 1 was 1 and 2, 2 is 2 and 3, 3 was 1 and 3In second case, the array had already in Increasi Ng order. The answer of every number is 0.

Authorfzu
Source2016 multi-university Training Contest 4 The main topic: Give a sequence, for each number in the bubble sort process of the maximum minimum difference.

Problem Solving Ideas:

We consider each number of groups from small to large, calculate his left than his small number, and then use n minus is the right is smaller than his number, the use of a tree array array method to do the operation.


See the code.

#include <iostream> #include <cstdio> #include <cmath> #include <cstring>using namespace std;# Define N 100000+10int c[n],a[n],ans[n];int lowbit (int k) {return k& ((k) XOR (k-1));        void Add (int num,int k) {while (k<=n) {c[k]+=num;    K+=lowbit (k);    }}int sum (int k) {int s=0;        while (k) {s+=c[k];    K-=lowbit (k); } return s;}    int main () {int t,s,case=1;    scanf ("%d", &t);        while (t--) {int n,k,max,min;        scanf ("%d", &n);        Memset (C,0,sizeof (c));            for (int i=1; i<=n; i++) {k=0;            scanf ("%d", &a[i]);            Add (1,a[i]),//The position of a[i] plus 1 s=a[i]-sum (a[i]),//a[i] behind the number of smaller than the current Max=max (a[i],i+s);            Min=min (A[i],i);        Ans[a[i]]=max-min;        } printf ("Case #%d:", case++);        for (int i=1; i<=n; i++) printf ("%d", ans[i]);    printf ("\ n"); } return 0;}


HDU 5775 Bubble Sort (multi-university Training Contest 4--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.