POJ1804逆序對

來源:互聯網
上載者:User

求逆序對的題目,可以用暴力、樹狀數組來實現。

 

1。暴力法:O(n^2)對於1000的規模完全可以做,直接找第i個元素之後有多少個比其小的元素即可。

 

代碼:

#include <iostream><br />#include <cstdio><br />#include <cstring><br />#include <cstdlib></p><p>using namespace std;</p><p>int main()<br />{<br />int t,n,cnt=0;<br />int a[1005];<br />int ret;</p><p>scanf("%d",&t);<br />while(t--)<br />{<br />cnt++;<br />scanf("%d",&n);<br />for(int i=0;i<n;i++)<br />scanf("%d",&a[i]);</p><p>ret=0;<br />for(int i=0;i<n;i++)<br />for(int j=i+1;j<n;j++)<br />if(a[i]>a[j])<br />ret++;<br />printf("Scenario #%d:/n",cnt);<br />printf("%d/n",ret);<br />if(t)<br />printf("/n");<br />}</p><p>return 0;<br />}<br />

 

運用樹狀數組,每次維護小於K的數的個數,從後往前求和,時間複雜度是O(nlogn)的,注意數的範圍很大,是2000000,這樣用這種方法反而不快。

 

代碼:

#include <iostream><br />#include <cstdio><br />#include <cstring><br />#include <cstdlib></p><p>using namespace std;</p><p>const int MAX=2000100;<br />int seg[MAX],a[1100];</p><p>void add(int k)<br />{<br />while(k<MAX)<br />{<br />seg[k]++;<br />k+=k&-k;<br />}<br />}</p><p>int sum(int k)<br />{<br />int ret=0;</p><p>while(k)<br />{<br />ret+=seg[k];<br />k-=k&-k;<br />}</p><p>return ret;<br />}</p><p>int main()<br />{<br />int t,n,cnt=0;<br />int ret;</p><p>scanf("%d",&t);<br />while(t--)<br />{<br />cnt++;<br />scanf("%d",&n);<br />for(int i=0;i<n;i++)<br />{<br />scanf("%d",&a[i]);<br />a[i]+=1000001;<br />}</p><p>ret=0;<br />memset(seg,0,sizeof(seg));<br />for(int i=n-1;i>=0;i--)<br />{<br />ret+=sum(a[i]-1);<br />add(a[i]);<br />}<br />printf("Scenario #%d:/n",cnt);<br />printf("%d/n",ret);<br />if(t)<br />printf("/n");<br />}</p><p>return 0;<br />}<br />

 

我也嘗試寫了一下,但是逾時了,由於數的範圍大,而線段樹的初始化時間效率低,線段樹的初始化時間是O(nlogn)而樹狀數組的初始化時間是O(n)的,這裡差距很大。

 

總結:能用樹狀數組代替線段樹的,盡量代替,不僅代碼量小,而且效率高。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.