Nita_Cristian

coada1

Mar 5th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <queue>
  4. #include <string>
  5. using namespace std;
  6. ifstream fin("coada1.in");
  7. ofstream fout("coada1.out");
  8. queue <int>q;
  9. queue <int>q2;
  10. string cmd;
  11. int ap[10001];
  12. int main()
  13. {
  14.     int m,x,c=0;
  15.     fin>>m;
  16.     for(int i=1; i<=m; i++)
  17.     {
  18.         fin>>cmd>>x;
  19.         if(cmd=="push")
  20.         {
  21.             if(ap[x]==0)
  22.             {
  23.                 q.push(x);
  24.                 ap[x]++;
  25.             }
  26.             else
  27.             {
  28.                 while(!q.empty()&&q.front()!=x)
  29.                 {
  30.                     ap[q.front()]--;
  31.                     q.pop();
  32.                 }
  33.                 if(!q.empty())
  34.                     q.pop();
  35.                 q.push(x);
  36.             }
  37.         }
  38.         else if(cmd=="query")
  39.         {
  40.             if(ap[x]!=0)
  41.             {
  42.                 q2=queue<int>(q);
  43.                 c=1;
  44.                 while(!q2.empty() && q2.front()!=x)
  45.                 {
  46.                     q2.pop();
  47.                     c++;
  48.                 }
  49.                 fout<<c<<'\n';
  50.             }
  51.             else
  52.             {
  53.                 fout<<-1<<'\n';
  54.             }
  55.         }
  56.     }
  57.     fin.close();
  58.     fout.close();
  59.  
  60. }
Add Comment
Please, Sign In to add comment