Wednesday, 20 March 2019

UVA Problem 12207 - That is Your Queue (C++)

The problem link can be found here. It has been solved using deque data structure.
Code:

 1  #include<bits/stdc++.h>
 2  using namespace std;
 3  int main(){
 4      int cases =0;
 5  int p,c;
 6 
 7  while(cin>>p>>c,p!=0,c!=0){
 8          deque<int>dq;
 9      if(p<c){
10          for(int i=1;i<=p;i++){
11            dq.push_back(i);
12          }
13      }
14      else{
15          for(int i=1;i<=c;i++){
16            dq.push_back(i);
17          }
18      }
19      cases++;
20      printf("Case %d:\n",cases);
21      while(c--){
22          string com;
23          cin>>com;
24          if(com == "N"){
25              int e = dq.front();
26              cout<<e<<endl;
27              dq.pop_front();
28              dq.push_back(e);
29          }
30          else{
31              int num;
32              cin>>num;
33              for(deque<int>::iterator it = dq.begin();it!=dq.end();it++){
34                  if(*it == num){
35                  dq.erase(it);
36                  break;
37                  }
38 
39              }
40              dq.push_front(num);
41          }
42      }
43  }
44 
45 
46  return 0;}

Tuesday, 19 March 2019

UVA Problem 540 - Team Queue (C++)


Problem link can be found here
 1  #include<bits/stdc++.h>
 2  using namespace std;
 3  int belong_to[1000001];
 4  queue<int>team_queue[1001];
 5  queue<int>combined;
 6  int main(){
 7    //  freopen("in.txt","r",stdin);
 8      //freopen("out.txt","w",stdout);
 9  int tc,k,cases=0;
10  while(cin>>tc){
11          if(tc==0)return 0;
12          for(int i=0;i<tc;i++){
13  cin>>k;
14  while(!team_queue[i].empty()){
15      team_queue[i].pop();
16  }
17  while(!combined.empty()){
18      combined.pop();
19  }
20  while(k--){
21      int elem;
22      cin>>elem;
23      belong_to[elem]= i;
24  }
25  }
26  cases++;
27  printf("Scenario #%d\n",cases);
28  string command;
29  int num;
30  while(cin>>command, command[0]!= 'S'){
31      if(command[0] == 'E'){
32          cin>>num;
33          int team = belong_to[num];
34          if(team_queue[team].empty()){
35              combined.push(team);
36          }
37          team_queue[team].push(num);
38      }
39 
40      else{
41      int team =  combined.front();
42      cout<<team_queue[team].front()<<endl;
43 
44      team_queue[team].pop();
45      if(team_queue[team].empty()){
46          combined.pop();
47      }
48 
49      }
50 
51  }
52  cout<<"\n";
53  }
54  return 0;}

Spoj Problem ACMCEG2C - Pick the candies (C++)

  The problem link may be found here.       Explanation: Use Deque to keep track of elements of the variety of candies. If i is gre...