Advertisement
a53

mana_cu_cautare_binara

a53
Apr 20th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <fstream>
  2. #include <cmath>
  3. #define ld long double
  4. using namespace std;
  5. ifstream f("mana.in");
  6. ofstream g("mana.out");
  7. const int NMax=105;
  8. const ld eps=1e-9;
  9. struct point
  10. {
  11. ld x,y;
  12. };
  13. point a[NMax];
  14. point C1,C2;
  15. int n,k;
  16.  
  17. ld dist2(point A,point B)
  18. {
  19. return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);
  20. }
  21.  
  22. void center(point A,point B,ld r)
  23. {
  24. ///2 * r trebuie sa fie mai mic decat dist(A,B)!!!
  25. if(A.y!=B.y)
  26. {
  27. ///constantele care ne usureaza calculul
  28. ld Cc=A.x*A.x-B.x*B.x+A.y*A.y-B.y*B.y;
  29. ld Ac=Cc/(2*(A.y-B.y));
  30. ld Bc=(A.x-B.x)/(A.y-B.y);
  31. ld C2c=A.x*A.x+B.x*B.x+A.y*A.y+B.y*B.y;
  32. ///EC DE GR2 in x0 (x-ul centrului cercului)
  33. ld a=2+2*Bc*Bc;
  34. ld b=(-2*A.x-2*B.x-4*Ac*Bc+2*A.y*Bc+2*B.y*Bc);
  35. ld c=C2c+2*Ac*Ac-2*A.y*Ac-2*B.y*Ac-2*r*r;
  36. ld sqrtDelta=sqrt(b*b-4*a*c);
  37. ld x01=(-b+sqrtDelta)/(2*a);
  38. ld y01=Ac-Bc*x01;
  39. ld x02=(-b-sqrtDelta)/(2*a);
  40. ld y02=Ac-Bc*x02;
  41. C1.x=x01;C1.y=y01;
  42. C2.x=x02;C2.y=y02;
  43. }
  44. else
  45. {
  46. ld x01=(A.x+B.x)/2;
  47. ld a=1;
  48. ld b=-2*A.y;
  49. ld c=A.x*A.x+x01*x01-2*x01*A.x-r*r+A.y*A.y;
  50. ld sqrtDelta=sqrt(b*b-4*a*c);
  51. ld y01=(-b-sqrtDelta)/(2*a);
  52. ld y02=(-b+sqrtDelta)/(2*a);
  53. C1.x=x01;C1.y=y01;
  54. C2.x=x01;C2.y=y02;
  55. }
  56. }
  57.  
  58. bool inside(point C,ld r,point a)
  59. {
  60. if((dist2(C,a)-r*r)<=eps)
  61. return 1;
  62. return 0;
  63. }
  64.  
  65. bool verif(ld r)
  66. {
  67. for(int i=1;i<=n;++i)
  68. for(int j=i+1;j<=n;++j)
  69. {
  70. if(i==j)
  71. continue;
  72. ld radius=r;
  73. if((2*r)*(2*r)-dist2(a[i],a[j])<0)
  74. continue;
  75. C1.x=0;C1.y=0;C2.x=0;C2.y=0;
  76. center(a[i],a[j],radius);
  77. int ans1=0;
  78. for(int t=1;t<=n;++t)
  79. if(inside(C1,r,a[t]))
  80. ++ans1;
  81. if(ans1>=k)
  82. return 1;
  83. int ans2=0;
  84. for(int t=1;t<=n;++t)
  85. if(inside(C2,r,a[t]))
  86. ++ans2;
  87. if(ans2>=k)
  88. return 1;
  89. }
  90. return 0;
  91. }
  92.  
  93. int cautbin(int lo,int hi)
  94. {
  95. int mid;
  96. while(lo<=hi)
  97. {
  98. mid=(lo+hi)/2;
  99. int ok1=verif(1.0*mid);
  100. int ok2=verif(1.0*(mid-1));
  101. if(ok1==1&&ok2==0)
  102. return mid;
  103. else
  104. if(ok1==1)
  105. hi=mid-1;
  106. else
  107. if(ok2==0)
  108. lo=mid+1;
  109. }
  110. return 0;
  111. }
  112.  
  113. int main()
  114. {
  115. f>>n>>k;
  116. for(int i=1;i<=n;++i)
  117. f>>a[i].x>>a[i].y;
  118.  
  119. if(k==1)
  120. {
  121. g<<1<<'\n';
  122. return 0;
  123. }
  124. g<<cautbin(1,150000)<<'\n';
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement