Codeforces 799B T-shirt buying (模拟+队列)

vj链接:

https://vjudge.net/contest/235444#problem/E

题意:

有n件T恤,正面与背面印有颜色,有m个顾客依次来买,每人有一种喜欢的颜色,他会买正面或背面有这种颜色的最便宜的衣服,如果没有就不买,问每人花的钱数。

例如 输入:

5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
5表示5件衣服 第一行分别是衣服的价格,第二第三行分别是衣服正面和背面印的颜色,6代表顾客的数量,下一行输入该顾客最喜欢的颜色对应的数字,
输出:
200 400 300 500 911 -1 
表示该顾客从包含他想要颜色对应的数字的衣服里面选一件最便宜的输出,如果没有他想要的颜色,则输出-1。
思路:
先将衣服的价格从小到大排序,便于入队时的顺序,然后定义三个队列q1,q2,q3,将有一面是1的入q1队...以此类推。定义一个结构体,其中f表示该衣服是否已经售出。具体看代码。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 #include<algorithm>
  6 using namespace std;
  7 struct node{
  8     int p;
  9     int f;
 10     int a;
 11     int b;
 12     
 13 }t[200010];
 14 queue<int>q1;
 15 queue<int>q2;
 16 queue<int>q3;
 17 void Clear()
 18 {
 19     while(!q1.empty())
 20     {
 21         q1.pop();
 22     }
 23     while(!q2.empty())
 24     {
 25         q2.pop();
 26     }
 27     while(!q3.empty())
 28     {
 29         q3.pop();
 30     }
 31 }
 32 int n,m,x;
 33 int cmp(node s,node t)
 34 {
 35     return s.p<t.p;
 36 }
 37 int main()
 38 {
 39     while(~scanf("%d",&n))
 40     {
 41         Clear();
 42         for(int i=0;i<n;i++)
 43         {
 44             cin>>t[i].p;
 45             t[i].f=1;
 46         }
 47         for(int i=0;i<n;i++)
 48         {
 49             cin>>t[i].a;
 50         }
 51         for(int i=0;i<n;i++)
 52         {
 53             cin>>t[i].b;
 54         }
 55      sort(t,t+n,cmp);
 56      for(int i=0;i<n;i++)
 57      {
 58          if(t[i].a==1||t[i].b==1)
 59          q1.push(i);
 60          if(t[i].a==2||t[i].b==2)
 61          q2.push(i);
 62          if(t[i].a==3||t[i].b==3)
 63          q3.push(i);
 64      }
 65      cin>>m;
 66      while(m--)
 67      { 
 68          int ff=0;
 69          cin>>x;
 70          if(x==1)
 71          {
 72              while(!q1.empty())
 73              {
 74                  int b=q1.front();
 75                  if(t[b].f)
 76                  {
 77                      t[b].f=0;
 78                      cout<<t[b].p;
 79                     ff=1;  
 80                 }
 81              q1.pop();
 82              if(ff)
 83              break;
 84             }
 85          if(!ff)
 86          cout<<"-1";
 87         }
 88         if(x==2)
 89         {
 90             while(!q2.empty())
 91             {
 92                 int b=q2.front();
 93                 if(t[b].f)
 94                 {
 95                     t[b].f=0;
 96                     cout<<t[b].p;
 97                     ff=1;
 98                 }
 99              q2.pop();
100              if(ff)
101              break; 
102             }
103         if(!ff)
104         cout<<"-1";
105         }    
106         if(x==3)
107         {
108             while(!q3.empty())
109             {
110                 int b=q3.front();
111                 if(t[b].f)
112                 {
113                     t[b].f=0;
114                     cout<<t[b].p;
115                     ff=1;
116                 }
117              q3.pop();
118              if(ff)
119              break; 
120             }
121         if(!ff)
122         cout<<"-1";
123         }
124         if(m==0)
125         cout<<endl;
126         else
127         cout<<" ";    
128      } 
129     }
130 return 0;
131 } 


 
原文地址:https://www.cnblogs.com/1013star/p/9280435.html