Shocked! Vector two lines of code to reverse order, six lines of code through the normal balancing tree, vector Reverse Order

Source: Internet
Author: User

Shocked! Vector two lines of code to reverse order, six lines of code through the normal balancing tree, vector Reverse Order
Returns the reverse order of two lines of code in Vector.

Background

First, let's think about the Bubble sorting process. It's not hard to find that for each element, we actually keep comparing and exchanging with the previous element.

It is precisely because this process determines that in the Bubble Sorting Process, the number before the number at a position must be incremental (from small to large)

Then, during the exchange, we can find a suitable position directly in binary mode and insert it.

Obviously, this can be implemented using the Balance Tree Vector.

The Code is also very short,

 1 #include<cstdio> 2 #include<algorithm> 3 #include<vector> 4 using namespace std; 5 int n,m,ans,a[100001]; 6 vector<int>v; 7 int main() 8 { 9     scanf("%d",&n);10     for(int i=1;i<=n;i++)    scanf("%d",&a[i]);11     for(int i=1;i<=n;i++)12     {13         int now=upper_bound(v.begin(),v.end(),a[i])-v.begin();14         ans=ans+i-now-1,v.insert(v.begin()+now,a[i]);15     }16     printf("%d",ans);17     return 0;18 }

 

 

Vector, six lines of code, Balance Tree

This is a reference to Huang's blog, but I didn't use an iterator to implement it.

By the way, the code is simplified.

The Code should be easier to understand.

 1 #include<cstdio> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 vector<int>v; 6 int n,opt,x; 7 int main() 8 { 9     v.reserve(100001);10     scanf("%d",&n);11     while(n--)12     {13         scanf("%d%d",&opt,&x);14         if(opt==1)    v.insert(lower_bound(v.begin(),v.end(),x),x);15         if(opt==2)    v.erase (lower_bound(v.begin(),v.end(),x));16         if(opt==3)    printf("%d\n",lower_bound(v.begin(),v.end(),x)-v.begin()+1);17         if(opt==4)    printf("%d\n",v[x-1]);18         if(opt==5)    printf("%d\n",v[lower_bound(v.begin(),v.end(),x)-v.begin()-1]);19         if(opt==6)    printf("%d\n",v[upper_bound(v.begin(),v.end(),x)-v.begin()]);20     }21     return 0;22 }

 

In general

Vector is a good thing,

Imagine if I open O2 for every exam in the future,

Will this array disappear? Σ (° △° |) percentile )︴

 

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.