HDU2275

/*
*Time: 125ms
*题目大意:
*        给定一个n,代表有n种操作,push代表把元素放入容器。
*        pop a,表示在容器中取出小于或等于a的最大元素。
*解题思路:
*        用multiset来模拟这个过程即可。
*/
View Code
 1 #include<iostream>
 2 #include<set>
 3 using namespace std;
 4 int main()
 5 {
 6 #ifndef ONLINE_JUDGE
 7     freopen("in.txt", "r", stdin);
 8 #endif
 9     int n, a;
10     char op[5];
11     while(cin >> n)
12     {
13         multiset<int>S;
14         multiset<int>::iterator p, q;
15         while(n--)
16         {
17             scanf("%s %d", op, &a);
18             if(op[1] == 'u')
19                 S.insert(a);
20             else
21             {
22                 p = S.begin();
23                 if(*p > a)
24                 {
25                     printf("No Element!\n");
26                     continue;
27                 }
28                 p = S.find(a);
29                 if(p != S.end())
30                 {
31                     printf("%d\n", *p);
32                     S.erase(p);
33                 }
34                 else
35                 {
36                     S.insert(a);
37                     p = q = S.find(a);
38                     p-- ;
39                     printf("%d\n", *p);
40                     S.erase(p);
41                     S.erase(q);
42                 }
43             }
44         }
45         printf("\n");
46     }
47     return 0;
48 }
原文地址:https://www.cnblogs.com/cchun/p/2519286.html