[FJSC2014]異或之

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   資料   

【題目描述】

給定n個非負整數A[1], A[2], ……, A[n]。

對於每對(i, j)滿足1 <= i < j <= n,得到一個新的數A[i] xor A[j],這樣共有n*(n-1)/2個新的數。求這些數(不包含A[i])中前k小的數。

註:xor對應於pascal中的“xor”,C++中的“^”。

【輸入格式】

第一行2個正整數 n,k,如題所述。

以下n行,每行一個非負整數表示A[i]。

【輸出格式】

共一行k個數,表示前k小的數。

【範例輸入】

4 5

1

1

3

4

【範例輸出】

0 2 2 5 5

【範例解釋】

1 xor 1 = 0 (A[1] xor A[2])

1 xor 3 = 2 (A[1] xor A[3])

1 xor 4 = 5 (A[1] xor A[4])

1 xor 3 = 2 (A[2] xor A[3])

1 xor 4 = 5 (A[2] xor A[4])

3 xor 4 = 7 (A[3] xor A[4])

前5小的數:0 2 2 5 5

【資料範圍】

第一個資料點,n <= 1000; 

第二個資料點,k = 1;

對於40%的資料,n <= 10000; k <= 10;

對於60%的資料,n <= 20000;

對於100%的資料,2 <= n <= 100000; 1 <= k <= min{250000, n*(n-1)/2};0 <= A[i] < 2^31

 

Solution

由於出題人資料是隨機產生的就卡不掉我的暴力騙分啦~不過我是構了一組。

很明顯,a-b<=a^b<=a+b

先對A[]排序,選取k個可能成為答案的數,用堆或者線段樹維護修改和查詢。當插進去的數比當前堆中的最大數還大的話就break。

 

 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<ctime> 5 int main() 6 { 7     freopen("xorit.in","w",stdout); 8     srand(time(0)); 9     int n=1000,m=2500,mod=1<<30;mod--;10     printf("%d %d\n",n,m);11     for(int i=1;i<=n;i++)printf("%d ",rand());12 }
Data Maker
 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 int t[1000010],d,n,k,a[100010],tmp; 5 int main() 6 { 7     scanf("%d%d",&n,&k);int i,j,l; 8     memset(t,127,sizeof(t)); 9     for(d=1;d<k;d<<=1);10     for(i=1;i<=n;i++)scanf("%d",&a[i]);11     std::sort(a+1,a+1+n);12     for(i=2;i<=n;i++)13     for(j=i-1;j;j--)t[++tmp]=a[i]^a[j];14     std::sort(t+1,t+1+tmp);15     for(i=1;i<=k;i++)printf("%d ",t[i]);16 }
暴力
 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 int t[524288],d,n,k,a[100010],cnt,tmp,now; 5 int main() 6 { 7     scanf("%d%d",&n,&k);int i,j,l,o;k++; 8     for(d=1;d<k;d<<=1); 9     for(i=1;i<=n;i++)scanf("%d",&a[i]);10     std::sort(a+1,a+1+n);11     for(i=2;i<=n;i++)12     for(j=i-1;j;j--)13     {14         tmp=a[i]^a[j];now=0;15         for(l=k+d-2;l;l>>=1)now<t[l]?now=t[l]:1;16         if(now==0&&cnt==k)break;17         if(cnt<k)18         {19             if(now<tmp)now=tmp;20             for(t[l=d+cnt]=tmp,cnt++,l>>=1;l;l>>=1)21             {22                 tmp=t[l<<1]>t[l<<1|1]?t[l<<1]:t[l<<1|1];23                 t[l]=tmp;24             }25         }26         else27         {28             if(tmp>=now&&a[i]-a[j]>now)break;29             if(tmp<now)30             {31                 for(o=1;o<d;t[o<<1|1]==now?o=o<<1|1:o<<=1);32                 if(now<tmp)now=tmp;33                 for(t[o]=tmp,o>>=1;o;o>>=1)34                 {35                     tmp=t[o<<1]>t[o<<1|1]?t[o<<1]:t[o<<1|1];36                     t[o]=tmp;37                 }38             }39         }40     }41     std::sort(t+d,t+d+k);k--;42     for(i=d;i<d+k;i++)printf("%d ",t[i]);43 }
std

 

另外還有一種二進位分組的做法。考慮在二進位中前k位相同的數,k+1位對答案的貢獻為cnt[0]*cnt[1],cnt[i]表示k+1位為i的數。

於是我們可以找到第k小的答案範圍,然後暴力就行了。複雜度O(nlogn)

還有一種可持久化Trie樹的做法--詳見Noi超級鋼琴

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.