• Source
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. struct data
    5. {
    6. int D, R, G;
    7. char status;
    8. int total;
    9. int t_now;
    10. } typedef data;
    11.  
    12. int main ()
    13. {
    14. int n, l;
    15. cin>>n>>l;
    16. data Tr_lights[102];
    17. for (int i=1; i<=n; i++)
    18. {
    19. cin>>Tr_lights[i].D>>Tr_lights[i].R>>Tr_lights[i].G;
    20. Tr_lights[i].total=Tr_lights[i].R+Tr_lights[i].G;
    21. Tr_lights[i].t_now=0;
    22. Tr_lights[i].status='R';
    23. }
    24. int Begin=0;
    25. int t=0;
    26. for (int i=1; i<=n; i++)
    27. {
    28. int leng = Tr_lights[i].D-Begin;
    29. Begin = Tr_lights[i].D;
    30. t+=leng;
    31. for (int j=i; j<=n; j++)
    32. {
    33. Tr_lights[j].t_now+=leng;
    34. Tr_lights[j].t_now=Tr_lights[j].t_now % Tr_lights[j].total;
    35. if (Tr_lights[j].t_now>=0 && Tr_lights[j].t_now<Tr_lights[j].R) Tr_lights[j].status='R';
    36. else Tr_lights[j].status='G';
    37. }
    38. if (Tr_lights[i].status=='G') continue;
    39. else if (Tr_lights[i].status=='R')
    40. {
    41. leng = Tr_lights[i].R-Tr_lights[i].t_now;
    42. t+=leng;
    43. for (int j=i+1; j<=n; j++)
    44. {
    45. Tr_lights[j].t_now+=leng;
    46. Tr_lights[j].t_now=Tr_lights[j].t_now % Tr_lights[j].total;
    47. if (Tr_lights[j].t_now>=0 && Tr_lights[j].t_now<Tr_lights[j].R) Tr_lights[j].status='R';
    48. else Tr_lights[j].status='G';
    49. }
    50. }
    51. }
    52. t+=l-Begin;
    53. cout<<t;
    54. return 0;
    55. }