HDU1007 Quoit Design 【分治】,hdu1007quoit

來源:互聯網
上載者:User

HDU1007 Quoit Design 【分治】,hdu1007quoit
Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30505    Accepted Submission(s): 8017


Problem DescriptionHave you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
 


 

InputThe input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.
 


 

OutputFor each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
 


 

Sample Input
20 01 121 11 13-1.5 00 00 1.50
 


 

Sample Output
0.710.000.75

題意:給定n個點,求距離最短的兩點的距離的一半。

題解:開始用暴力法,結果逾時,然後換成分治就過了,分治的過程是先將每個點的座標讀入到數組裡,再將數組按照x座標排序,然後分治找最小值,遞迴終止條件是只剩兩個元素或三個元素,但是若僅按照x排序最終結果不一定是最小值,因為有可能左邊的元素與右邊的元素構成最小值,所以需要再根據y值進行一次排序,此時資料規模已經相當小了,可以用暴力直接求解。 

分治代碼:

#include <stdio.h>#include <math.h>#include <algorithm>#define maxn 100002using std::sort;struct Node{double x, y;} arr[maxn], temp[maxn];bool cmpx(Node a, Node b){return a.x < b.x;}bool cmpy(Node a, Node b){return a.y < b.y;}double calDist(int i, int j){double x = arr[i].x - arr[j].x;double y = arr[i].y - arr[j].y;return sqrt(x * x + y * y);}double divideAndConquer(int l, int r){if(r - l == 1) return calDist(l, r);else if(r - l == 2){double a = calDist(l, l + 1);double b = calDist(l + 1, r);double c = calDist(l, r);if(b > c) b = c;return a < b ? a : b;}int mid = (l + r) >> 1, i, j, id = 0;double a = divideAndConquer(l, mid);double b = divideAndConquer(mid + 1, r);double min = a < b ? a : b;for(i = l; i <= r; ++i)if(fabs(arr[i].x - arr[mid].x) < min) temp[id++] = arr[i];sort(temp, temp + id, cmpy);for(i = 0; i < id; ++i)for(j = i + 1; j < id; ++j){a = temp[j].y - temp[i].y;if(a >= min) break;b = temp[j].x - temp[i].x;a = sqrt(a * a + b * b);if(a < min) min = a;}return min;}int main(){//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);int n, i, j;double ans, x, y, len;while(scanf("%d", &n), n){for(i = 0; i < n; ++i)scanf("%lf%lf", &arr[i].x, &arr[i].y);sort(arr, arr + n, cmpx);printf("%.2lf\n", divideAndConquer(0, n - 1) / 2);}return 0;}


 

 

原TLE代碼:

#include <stdio.h>#include <math.h>#define maxn 100002struct Node{double x, y;} arr[maxn];double cal(int i, int j){double x = arr[i].x - arr[j].x;double y = arr[i].y - arr[j].y;return sqrt(x * x + y * y);}int main(){//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);int n, i, j;double ans, x, y, len;while(scanf("%d", &n), n){for(i = 0, ans = -1; i < n; ++i){scanf("%lf%lf", &arr[i].x, &arr[i].y);for(j = 0; j < i; ++j){len = cal(i, j);if(len < ans || ans < 0) ans = len;}}printf("%.2lf\n", ans / 2);}return 0;}


 


HDU 1007 Quoit Design 分治解最近對各種逾時 大牛指點

函數改成inline試試?。。。。對y我都用基數排序了。我也tle。。。
 
急航電的一道ACM題,正確答案

ac代碼(G++提交):

#include <iostream>
#include <cmath>
using namespace std;

struct P { double x, y;} p[100003];
int n;
double ans;

bool cmp1(const P&a, const P&b) { return a.x == b.x ? a.y < b.y : a.x < b.x;}
bool cmp2(const P&a, const P&b) { return a.y == b.y ? a.x < b.x : a.y < b.y;}

void work()
{
for (int i=0; i<n; i++)
for (int j=i+1; j<n&&j<i+50; j++)
ans = min((p[i].x-p[j].x)*(p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y), ans);
}

int main()
{
while (scanf("%d", &n), n!=0)
{
for (int i=0; i<n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
ans = 2100000000;
sort(p, p+n, cmp1), work();
sort(p, p+n, cmp2), work();
printf("%.2lf\n", sqrt(ans)/2);
}
}
 

聯繫我們

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