HDU5122 K.bro sorting tree Array class inverse number application

Source: Internet
Author: User

Test instructions is very simple, see the case can understand, a sequence of length n, to reorder, ascending, each time take this number and the following all the number of comparisons, need to move as far back as possible to move, asked to operate several times

Analysis of the case, from the back to the front, if found earlier than the current number of the number of answers to add 1, but found that it is difficult to discuss clearly, but also has always been analyzed, the current number, if the number is smaller than it, it must be operated once, it is thought that the tree array in reverse order number of an operation, The tree array to reverse the number is the need to find out the current tree array of how many number than the current number is small, so thought, the array of numbers from the back to the previous one into the tree array, each plus a, to determine whether the tree array is smaller than the current number of numbers, there is an answer to add 1, One of the reasons for this is that the title emphasizes that the numbers in this sequence must be different numbers from 1 to N.


int T;int n;int nnum[1000000 + 55];int c[1000000 + 55];int case = 0;void init () {memset (nnum,0,sizeof (Nnum)); Memset (C,0,si Zeof (c));} bool Input () {while (cin>>n) {for (int i=1;i<=n;i++) scanf ("%d", &nnum[i]); return false;} return true;} int lowbit (int x) {return x& (-X);} void Add (int i,int val) {while (I <= N) {c[i] + = Val;i + lowbit (i);}} int get_sum (int i) {int sum = 0;while (i > 0) {sum + = c[i];i-= Lowbit (i);} return sum;} void Cal () {int ans = 0;for (int i=n;i>0;i--) {Add (nnum[i],1); int now = Get_sum (Nnum[i] – 1); if (now > 0) ans++;} printf ("Case #%d:%d\n", ++case,ans);} void output () {}int main () {cin>>t;while (t--) {init (); if (input ()) return 0;cal (); output ();} return 0;}


HDU5122 K.bro sorting tree Array class inverse number application

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.