2015 Multi-University Training Contest 5 hdu 5349 MZL's simple problem

MZL's simple problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 740    Accepted Submission(s): 357


Problem Description
A simple problem
Problem Description
You have a multiple set,and now there are three kinds of operations:
1 x : add number x to set
2 : delete the minimum number (if the set is empty now,then ignore it)
3 : query the maximum number (if the set is empty now,the answer is 0)
 
Input
The first line contains a number N (N106),representing the number of operations.
Next N line ,each line contains one or two numbers,describe one operation.
The number in this set is not greater than 109.
 
Output
For each operation 3,output a line representing the answer.
 
Sample Input
6
1 2
1 3
3
1 3
1 4
3
 
Sample Output
3 4
 
Source
 
解题:由于我们只需要查询最大值,所以最大值总是最后一个被删的,如果当前集合只有一个元素,那么肯定是删最大的元素了,否则,随便删就是了,不影响最大值
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main() {
 4     int n,op,val;
 5     while(~scanf("%d",&n)) {
 6         int sz = 0,maxval = INT_MIN;
 7         while(n--) {
 8             scanf("%d",&op);
 9             switch(op) {
10             case 1:
11                 scanf("%d",&val);
12                 maxval = max(val,maxval);
13                 sz++;
14                 break;
15             case 2:
16                 sz = max(0,sz-1);
17                 if(!sz) maxval = INT_MIN;
18                 break;
19             case 3:
20                 printf("%d
",sz?maxval:0);
21                 break;
22             default:
23                 ;
24             }
25         }
26     }
27     return 0;
28 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4704600.html