karlangadas

10901 - Ferry Loading III

Dec 13th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <map>
  4. using namespace std;
  5. queue<int> lef;
  6. queue<int> righ;
  7. queue<int> lef1;
  8. queue<int> righ1;
  9. map<int,int> m;
  10.  
  11. int tim;
  12. int max (int a, int b){
  13.     if (a>b)
  14.         return a;
  15.     else
  16.         return b;
  17. }
  18. int main() {
  19.     int ncases;
  20.     cin>>ncases;
  21.     while(ncases--){
  22.         int capacity, t, ncars,llegada,bank;
  23.         string bankside;
  24.        
  25.         cin>>capacity>>t>>ncars;
  26.         for (int i=0;i<ncars;i++){
  27.             cin>>llegada>>bankside;
  28.             if (i==0){
  29.                 tim=llegada;
  30.             }
  31.             if (bankside=="left"){
  32.                 lef1.push(i);
  33.                 lef.push(llegada);}
  34.             else{
  35.                 righ1.push(i);
  36.                 righ.push(llegada);}
  37.         }
  38.         //0 es izq y 1 es der;
  39.        
  40.         bank=0;
  41.            
  42.         while (!lef.empty() || !righ.empty()){
  43.            
  44.             if (bank==0){
  45.                 int j=0;
  46.                 int flag=0;
  47.                 if (!lef.empty()){
  48.                     //cout<<"left front "<<lef.front()<<endl;
  49.                     if (lef.front()>tim && ((!righ.empty() && lef.front()<righ.front()) || righ.empty())){
  50.                        
  51.                         flag=1;
  52.                         tim=lef.front();
  53.                     }
  54.                     while (!lef.empty() && j<capacity && lef.front()<=tim){
  55.                         //cout<<lef.front()<<endl;
  56.                         //cout<<"time: "<<tim+t<<endl;
  57.                         flag=1;
  58.                         m[lef1.front()]=tim+t;
  59.                         lef.pop();
  60.                         lef1.pop();
  61.                         j++;
  62.                     }  
  63.                 }
  64.                 if (flag==1)
  65.                     tim=tim+t;
  66.                 else
  67.                     tim=max(righ.front(),tim)+t;
  68.                 //cout<<"tim es "<<tim<<endl;
  69.                 bank=1;
  70.             }
  71.             else{
  72.                 int j=0;
  73.                 int flag=0;
  74.                 if (!righ.empty()){
  75.                     if (righ.front()>tim && ((!lef.empty() && lef.front()>righ.front()) || lef.empty())){
  76.                         flag=1;
  77.                         tim=righ.front();
  78.                     }
  79.                     while (!righ.empty() && j<capacity && righ.front()<=tim){
  80.                         //cout<<righ.front()<<'r'<<endl;
  81.                         flag=1;
  82.                         m[righ1.front()]=tim+t;
  83.                         righ1.pop();
  84.                         righ.pop();
  85.                         j++;
  86.                     }
  87.                 }
  88.                 if (flag==1)
  89.                     tim=tim+t;
  90.                 else
  91.                     tim=max(lef.front(),tim)+t;
  92.                 bank=0;
  93.             }
  94.         }
  95.        
  96.         for (int i=0;i<ncars;i++){
  97.             cout<<m[i]<<endl;
  98.         }
  99.         if (ncases>0)
  100.             cout<<endl;
  101.         m.clear();
  102.     }
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment