Advertisement
nicuvlad76

Untitled

Dec 17th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #define N 1005
  4. using namespace std;
  5. ifstream fin("cursuri.in");
  6. ofstream fout("cursuri.out");
  7. ///cu problema specctacolelor = sort dupa of
  8. struct curs
  9. {
  10. int oi, of;
  11. bool p;
  12. }a[N];
  13. int n, cer, k;
  14.  
  15. void Sort()
  16. {
  17. ///bubble sort
  18. int i,m=n;
  19. bool ord=0;
  20. while(!ord)
  21. {
  22. ord=1;
  23. for(i=1;i<m;i++)
  24. if(a[i].of>a[i+1].of)
  25. {
  26. ord=0;
  27. swap(a[i], a[i+1]);
  28. }
  29. m--;
  30. }
  31. }
  32. void Citire()
  33. {
  34. fin>>cer>>n>>k;
  35. for(int i=1;i<=n;i++)
  36. {
  37. fin>>a[i].oi>>a[i].of;
  38. a[i].p=0;
  39. }
  40. }
  41. int Cerinta1()
  42. {
  43. int cate=0,i,j;
  44. curs sal[N];
  45. for(i=0;i<=k;i++)sal[i].oi=sal[i].of=0;
  46. ///Greedy
  47. for(i=1;i<=n;i++)
  48. {
  49. int p=0;
  50. for(j=1;j<=k;j++)
  51. if(a[i].oi>=sal[j].of &&a[i].oi-sal[j].of<=a[i].oi-sal[p].of)
  52. p=j;
  53. if(p)
  54. sal[p]=a[i], cate++;
  55. }
  56. return cate;
  57. }
  58.  
  59. bool Test(int x)
  60. {
  61. int i;
  62. for(i=1;i<=n;i++)
  63. a[i].of=a[i].oi+x, a[i].p=0;
  64. Sort();
  65. if(Cerinta1()==n)return 1;
  66. else return 0;
  67. }
  68. int cerinta2()
  69. {
  70. int dmax=0,i,gasit=1;
  71. ///durata maxima
  72. for(i=1;i<=n;i++)
  73. if(a[i].of-a[i].oi>dmax)
  74. dmax=a[i].of-a[i].oi;
  75. ///cautare binara
  76. int st=1, dr=dmax,mij;
  77. while(st<=dr)
  78. {
  79. mij=(st+dr)/2;
  80. if(Test(mij))
  81. gasit=mij, st=mij+1;
  82. else dr=mij-1;
  83. }
  84. return gasit;
  85. }
  86. int main()
  87. {
  88. Citire();
  89. Sort();
  90. if(cer==1) fout<<Cerinta1();
  91. else fout<<cerinta2();
  92. return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement