Advertisement
a53

masterpiece002_CU_MEMORIE_REDUSA_SPART_FLOAT_DEGEABA

a53
Mar 14th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #define Dmax 505050
  5. using namespace std;
  6. short int TI[Dmax],TF[Dmax]; /// TI=Partea intreaga, iat TF=partea fractionara
  7.  
  8. unsigned short readInt() /// Citirea numerelor intregi prin parsare
  9. {
  10. bool semn=false;
  11. unsigned short raspuns=0;
  12. char c;
  13. while(true)
  14. {
  15. c=getchar();
  16. if(c==EOF)
  17. return 1001;
  18. if(c=='-')
  19. {
  20. semn=true;
  21. break;
  22. }
  23. if(c>='0'&&c<='9')
  24. {
  25. raspuns=c-'0';
  26. break;
  27. }
  28. }
  29. while(true)
  30. {
  31. c=getchar();
  32. if(c<'0'||c>'9')
  33. break;
  34. raspuns=10*raspuns+c-'0';
  35. }
  36. if(semn)
  37. return -raspuns;
  38. return raspuns;
  39. }
  40.  
  41. int main()
  42. {
  43. int n=0; /// n=nr de segmente simple
  44. unsigned short X1,Y1,X2,Y2,a,b,r;
  45. bool SF_FIS=false;
  46. freopen("masterpiece002.in", "r",stdin);
  47. while(!SF_FIS)
  48. {
  49. X1=readInt();
  50. if(X1==1001)
  51. {
  52. SF_FIS=true;
  53. break;
  54. }
  55. Y1=readInt();X2=readInt();Y2=readInt();
  56. a=abs(X2-X1),b=abs(Y2-Y1); /// a=latura orizontala si b=latura verticala ale triunghiului
  57. if(b!=0)
  58. do /// Calculam CMMDC(a,b)
  59. {
  60. r=a%b;
  61. a=b;
  62. b=r;
  63. }
  64. while(r); /// in final a va fi cel mai mare divizor comun
  65. else
  66. if(a==0) /// pentru a prinde cazul particular in care ambele valori sunt 0
  67. a=1;
  68. /// (Y1==Y2&&abs(X2-X1)==1) <=> Segment orizontal de lungime 1
  69. /// (X1==X2&&abs(Y2-Y1)==1) <=> Segment vertical de lungime 1
  70. /// (X2==X1&&Y2==Y1&&abs(Y1-X1)==1) <=> Segment oblic de lungime 1
  71. float x;
  72. if(a==1||(Y1==Y2&&abs(X2-X1)==1)||(X1==X2&&abs(Y2-Y1)==1)||(X2==X1&&Y2==Y1&&abs(Y1-X1)==1)) /// Daca e segment simplu,
  73. {
  74. x=sqrt((X2-X1)*(X2-X1)+(Y2-Y1)*(Y2-Y1)); /// calculam distanta
  75. TI[n]=(int)x;TF[n++]=10000*(x-(int)x); /// si o memoram in cei doi vectori
  76. }
  77. }
  78. int mij,st,dr,Lmax=0;
  79. short int M_2[n]; /// M_2 este M fara ultimele 2 cifre
  80. char M2[n]; /// M2 este M cu ultimele doua cifre
  81. for(int i=0;i<n;++i)
  82. {
  83. st=1;
  84. dr=Lmax;
  85. while(st<=dr) /// Cautare binara
  86. {
  87. mij=(st+dr)/2;
  88. int mij_c=M_2[mij]*100+M2[mij]; /// Valoarea lui M[mij]
  89. if(TI[mij_c]<=TI[i])
  90. {
  91. if(TI[mij_c]<TI[i]) /// Intregii sunt in relatia <
  92. st=mij+1;
  93. else /// Intregii sunt egali
  94. if(TF[mij_c]<=TF[i]) /// Fractiile sunt mai mici sau egale
  95. st=mij+1;
  96. else /// Fractiile sunt mai mari
  97. dr=mij-1;
  98. }
  99. else /// Intregii sunt mai mari
  100. dr=mij-1;
  101. }
  102. if(st>Lmax)
  103. Lmax=st;
  104. M_2[st]=i/100;M2[st]=i%100; /// pun pe i in cei doi vectori
  105. }
  106. freopen ("masterpiece002.out","w",stdout);
  107. cout<<Lmax<<'\n';
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement