C實現之燈塔問題

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   for   sp   strong   div   


描述

海上有許多燈塔,為過路船隻照明。從平面上看,海域範圍是[1, 10^8] × [1, 10^8] 。



(圖一)


一所示,每個燈塔都配有一盞探照燈,照亮其東北、西南兩個對頂的直角地區。探照燈的功率之大,足以覆蓋任何距離。燈塔本身是如此之小,可以假定它們不會彼此遮擋。



(圖二)


若燈塔A、B均在對方的照亮範圍內,則稱它們能夠照亮彼此。比如在圖二的執行個體中,藍、紅燈塔可照亮彼此,藍、綠燈塔則不是,紅、綠燈塔也不是。


現在,對於任何一組給定的燈塔,請計算出其中有多少對燈塔能夠照亮彼此。


輸入

共n+1行。

第1行為1個整數n,表示燈塔的總數。

第2到n+1行每行包含2個整數x, y,分別表示各燈塔的橫、縱座標。


輸出

1個整數,表示可照亮彼此的燈塔對的數量。


輸入範例
32 24 35 1

輸出範例
1



1 # include<stdio.h> 2 3 int quick(int a[],int A[],int b, int c){ 4 int inversion = 0; 5 if (c > b){ 6 int middle = (int)((b + c) / 2); 7 int u = b; int v = c; 8 inversion+=quick(a,A,b,middle); 9 inversion+= quick(a, A, middle + 1, c);10 inversion+= merge(a, A, u, middle, v);11 }12 return inversion;13 }14 15 16 int merge(int a[],int B[], int b, int m, int c){17 int i = b; int j = m + 1; int k = 0; int inversion = 0;18 while (i <= m && j <= c)19 {20 if (a[i] <= a[j])21 { 22 B[k++] = a[i++]; 23 inversion= inversion +c-j+1;24 }25 else{26 B[k++] = a[j++];27 }28 }29 while (i <= m)30 B[k++] = a[i++];31 while (j <= c)32 B[k++] = a[j++];33 34 for (i = 0; i < k; i++)35 a[b + i] = B[i];36 return inversion;37 }38 39 void main(){40 int A[18] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int a[18];41 int Source_inversion = 0; int inversion = 0;42 for (int i = 0; i < 18; i++){43 a[i] = rand() % 20;44 }45 for (int i = 0; i < 18; i++){46 printf("%d ", a[i]);47 }48 49 for (int t = 0; t < 17; t++){50 for (int m = t + 1; m < 18;m++ )51 {52 if (a[m] >= a[t])53 Source_inversion++;54 }55 }56 57 printf("\nSource method: %d\n", Source_inversion);58 59 60 inversion=quick(a,A,0,17);61 printf("\n\n");62 for (int i = 0; i < 18; i++){63 printf("%d ", a[i]);64 }65 printf("\nMerge sort count:%d",inversion);66 system("pause");67 }

 

C實現之燈塔問題

聯繫我們

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