|
1 | 1 | #include<iostream> |
2 | 2 | #include<queue> |
3 | | - |
4 | | -/* |
5 | | -6 |
6 | | -1 1 |
7 | | -1 -1 |
8 | | -2 0 |
9 | | -1 2 |
10 | | -2 -1 |
11 | | -2 1 |
12 | | -*/ |
13 | | - |
14 | 3 | using namespace std; |
15 | | - |
16 | 4 | struct Animal { |
17 | | - int index; |
18 | | - int id; |
19 | | - Animal(int a,int b):index(a),id(b) {} |
| 5 | + int order,id; |
| 6 | + Animal(int o,int i):order(o),id(i) {} |
20 | 7 | }; |
21 | 8 | int main() { |
22 | | - int n,m,t,order=0;//order表示索引 |
23 | | - scanf("%d",&n); |
24 | | - queue<Animal> dog; |
25 | | - queue<Animal> cat; |
26 | | - for(int i=0; i<n; i++) { |
27 | | - scanf("%d%d",&m,&t); |
28 | | - if(m==1) { |
29 | | - if(t>0) { |
30 | | - dog.push(Animal(order++,t)); |
31 | | - } else if(t<0) { |
32 | | - cat.push(Animal(order++,t)); |
33 | | - } |
34 | | - } else { |
35 | | - if(t==0&&!dog.empty()&&!cat.empty()) { |
36 | | - //收养最早的动物 |
37 | | - if(dog.front().index<cat.front().index) { |
38 | | - //如果狗的索引更小 |
| 9 | + int n; |
| 10 | + while(scanf("%d",&n)!=EOF) { |
| 11 | + queue<Animal> dog,cat; |
| 12 | + int order=0,m,t; |
| 13 | + for(int i=0; i<n; i++) { |
| 14 | + scanf("%d%d",&m,&t); |
| 15 | + if(m==1) { |
| 16 | + if(t>0) {//狗进入收容所 |
| 17 | + dog.push(Animal(order++,t)); |
| 18 | + } else { |
| 19 | + cat.push(Animal(order++,t)); |
| 20 | + } |
| 21 | + } else { |
| 22 | + bool isDog=true; |
| 23 | + if(t==0) { //收养动物中最早进入 |
| 24 | + if(dog.empty()&&cat.empty()) { |
| 25 | + continue; |
| 26 | + } else if(cat.empty()) { |
| 27 | + //狗中找 |
| 28 | + isDog=true; |
| 29 | + } else if(dog.empty()) { |
| 30 | + //猫中找 |
| 31 | + isDog=false; |
| 32 | + } else { |
| 33 | + if(dog.front().order>cat.front().order) { |
| 34 | + //收养猫 |
| 35 | + isDog=false; |
| 36 | + } |
| 37 | + } |
| 38 | + } else if(t>0) { //收养狗 |
| 39 | + if(dog.empty()) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + } else { |
| 43 | + if(cat.empty()) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + isDog=false; |
| 47 | + } |
| 48 | + if(isDog) { |
39 | 49 | printf("%d ",dog.front().id); |
40 | 50 | dog.pop(); |
41 | 51 | } else { |
42 | 52 | printf("%d ",cat.front().id); |
43 | 53 | cat.pop(); |
44 | 54 | } |
45 | | - } else if(t==0&&dog.empty()&&!cat.empty()) { |
46 | | - //收养最早的动物,狗为空 |
47 | | - printf("%d ",cat.front().id); |
48 | | - cat.pop(); |
49 | | - } else if(t==0&&!dog.empty()&&cat.empty()) { |
50 | | - //收养最早的动物,猫为空 |
51 | | - printf("%d ",dog.front().id); |
52 | | - dog.pop(); |
53 | | - } else if(t==1&&!dog.empty()) { |
54 | | - //收养狗 |
55 | | - printf("%d ",dog.front().id); |
56 | | - dog.pop(); |
57 | | - } else if(t==-1&&!cat.empty()) { |
58 | | - //收养猫 |
59 | | - printf("%d ",cat.front().id); |
60 | | - cat.pop(); |
61 | 55 | } |
62 | | - printf("\n"); |
63 | 56 | } |
64 | 57 | } |
65 | 58 | return 0; |
|
0 commit comments