Hdoj 4911 inversion tree-like array + discretization

Source: Internet
Author: User

Test instructions: give you n a sequence of unordered numbers that can be repeated, and ask how many pairs of reverse order are at least after the K-second exchange.

The inverse pair can be used in a tree-like array, and for duplicate elements, the number may be exchanged at sort

Pay attention to the weight when summing, there is another way is to stabilize the sort Stable_sort

#include <string.h> #include <stdio.h> #include <algorithm>using namespace std; #define LL __int64# Define N 100000+10struct ln{int id,va;} In[n];int a[n],c[n];int n;int cmp (ln x,ln y) {return x.va<y.va;} int lowbit (int x) {return x& (-X);} void Updata (int t,int value) {for (int i=t;i<=n;i+=lowbit (i)) C[i]+=value;}        int getsum (int x) {int temp=0;        for (int i=x;i>=1;i-=lowbit (i)) temp+=c[i]; return temp;}        int main () {int k;                        while (scanf ("%d%d", &n,&k)!=eof) {for (int i=1;i<=n;i++) {                        scanf ("%d", &in[i].va);                In[i].id=i;                } memset (C,0,sizeof (c));                Sort (in+1,in+n+1,cmp);                A[in[1].id]=1; for (int i=2;i<=n;i++) {if (In[i].va!=in[i-1].va) a[in[i].i               D]=i;         else a[in[i].id]=a[in[i-1].id];                } ll Ans=0;                   for (int i=1;i<=n;i++) {updata (a[i],1);                   printf ("%d%d\n", Getsum (A[i]), Getsum (a[i]-1));                   int temp=i-getsum (A[I]-1);                   temp=temp-(Getsum (A[i])-getsum (a[i]-1));                Ans+=temp;                } if (ans-k<0) printf ("0\n");        else printf ("%i64d\n", ans-k); } return 0;}
#include <string.h> #include <stdio.h> #include <algorithm>using namespace std; #define LL __int64# Define N 100000+10struct ln{int id,va;} In[n];int a[n],c[n];int n;int cmp (ln x,ln y) {return x.va<y.va;} int lowbit (int x) {return x& (-X);} void Updata (int t,int value) {for (int i=t;i<=n;i+=lowbit (i)) C[i]+=value;}        int getsum (int x) {int temp=0;        for (int i=x;i>=1;i-=lowbit (i)) temp+=c[i]; return temp;}        int main () {int k;                        while (scanf ("%d%d", &n,&k)!=eof) {for (int i=1;i<=n;i++) {                        scanf ("%d", &in[i].va);                In[i].id=i;                } memset (C,0,sizeof (c));                Stable_sort (IN+1,IN+N+1,CMP);                for (int i=1;i<=n;i++) a[in[i].id]=i;                ll Ans=0; for (int. i=1;i<=n;i++) {UpData (a[i],1);                   int temp=i-getsum (a[i]);                Ans+=temp;                } if (ans-k<0) printf ("0\n");        else printf ("%i64d\n", ans-k); } return 0;}



Hdoj 4911 inversion tree-like array + discretization

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.