Advertisement
a53

semarun

a53
Feb 22nd, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5. struct Semaphore
  6. {
  7. int distance;
  8. int red;
  9. int green;
  10. int redGreenCycle;
  11. };
  12. int n,x,s,k;
  13. vector<Semaphore> semaphores;
  14. vector<int> solutions;
  15.  
  16. int gcd(int a,int b)
  17. {
  18. if(b==0)
  19. return a;
  20.  
  21. return gcd(b,a%b);
  22. }
  23.  
  24. int lcm(int a,int b)
  25. {
  26. return a*b/gcd(a,b);
  27. }
  28.  
  29. int main()
  30. {
  31. ifstream fin("semarun.in");
  32. ofstream fout("semarun.out");
  33. /// read data and compute lcm of red + green intervals
  34. fin>>n>>x>>s>>k;
  35. Semaphore tmpSemaphore;
  36. int redGreenLcm=1;
  37. for(int i=0;i<k;++i)
  38. {
  39. fin>>tmpSemaphore.distance>>tmpSemaphore.red>>tmpSemaphore.green;
  40. tmpSemaphore.redGreenCycle=tmpSemaphore.red + tmpSemaphore.green;
  41. redGreenLcm=lcm(redGreenLcm,tmpSemaphore.redGreenCycle);
  42. semaphores.push_back(tmpSemaphore);
  43. }
  44. /// check every moment of time between 0 and lcm-1
  45. int maxT=s-(int)ceil((float)n/x);
  46. for(int t=0;t<=min(maxT,redGreenLcm-1);++t)
  47. {
  48. bool isSolution=true;
  49. for(auto &semaphore:semaphores)
  50. {
  51. /// check if any semaphore will indicate red colour
  52. int timeAtSemaphore=t+(semaphore.distance/x);
  53. if(timeAtSemaphore%semaphore.redGreenCycle<semaphore.red)
  54. {
  55. isSolution=false;
  56. break;
  57. }
  58. }
  59.  
  60. if(isSolution)
  61. solutions.push_back(t);
  62. }
  63. /// compute solution on intervals [lcm, 2*lcm-1] ... [y*lcm, maxT]
  64. int initialSolutionsCount = solutions.size();
  65. for(int i=1;i<=maxT/redGreenLcm+1;++i)
  66. {
  67. for(int j=0;j<initialSolutionsCount;++j)
  68. {
  69. int newSol=redGreenLcm*i+solutions[j];
  70. if (newSol>maxT)
  71. break;
  72. solutions.push_back(newSol);
  73. }
  74. }
  75. /// write solutions
  76. fout<<solutions.size()<<'\n';
  77. for(int &solution:solutions)
  78. fout<<solution<<' ';
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement