码迷,mamicode.com
首页 > 其他好文 > 详细

【枚举】Vijos P1012 清帝之惑之雍正

时间:2016-04-03 20:15:45      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

  https://vijos.org/p/1012

题目大意

  给n个坐标(n<=100 000),求直线距离最短是多少。数据较大用long long 或 double 

题目思路:

  【枚举】

  正解貌似是分治,不过我一看就暴力枚举+剪枝了。

  先按x y为第一、第二关键字排序。

  设当前最优解为c,如果当前的点对x坐标差的平方比最优解大就可以break了。

   

  

技术分享
 1 //
 2 //by coolxxx
 3 //
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 #include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define eps 1e-8
22 #define J 10000
23 #define MAX 0x7f7f7f7f
24 #define PI 3.1415926535897
25 #define N 100004
26 using namespace std;
27 int n,m,lll,ans,cas;
28 double b,c;
29 struct xxx
30 {
31     double x,y;
32 }a[N];
33 bool cmp(xxx aa,xxx bb)
34 {
35     if(aa.x!=bb.x)return aa.x<bb.x;
36     return aa.y<bb.y;
37 }
38 int main()
39 {
40     #ifndef ONLINE_JUDGE
41 //    freopen("1.txt","r",stdin);
42 //    freopen("2.txt","w",stdout);
43     #endif
44     int i,j,k;
45 //    while(~scanf("%s%d",s,&n))
46     while(~scanf("%d",&n) && n)
47     {
48         c=1000000000000;
49         for(i=1;i<=n;i++)
50             scanf("%lf%lf",&a[i].x,&a[i].y);
51         sort(a+1,a+1+n,cmp);
52         for(i=1;i<=n;i++)
53         {
54             for(j=i+1;j<=n;j++)
55             {
56                 if(sqr(a[i].x-a[j].x)>c)break;
57                 b=sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y);
58                 c=min(c,b);
59             }
60         }
61         c=sqrt(c);
62         printf("%.3lf\n",c);
63     }
64     return 0;
65 }
66 
67 
68 /*
69 //
70 
71 //
72 */
View Code

 

【枚举】Vijos P1012 清帝之惑之雍正

标签:

原文地址:http://www.cnblogs.com/Coolxxx/p/5350389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!