-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_A1057.cpp
More file actions
166 lines (156 loc) · 3.29 KB
/
13_A1057.cpp
File metadata and controls
166 lines (156 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* P466 Stack (30)
* 分块思想
*/
#include <iostream>
#include <stack>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
// 可能的最大数
#define MAXN 1000010
// block 的个数
#define SQRTN 316
using namespace std;
// 将 string 转换为小写
string str_toLower(string str)
{
string result = "";
for (int i = 0; i < str.length(); i++)
{
result += tolower(str[i]);
}
return result;
}
// 建立 command 映射
map<string, int> commandMap;
// stack
stack<int> extendStack;
// block
int block[SQRTN] = {0};
// table
int table[MAXN] = {0};
// total count of the numbers
int totalCount = 0;
// peekMedian
void peekMedian()
{
// 求第 K 大的数
int K;
if (totalCount % 2 == 0)
{
K = totalCount / 2;
}
else
{
K = (totalCount + 1) / 2;
}
if (K == 0)
{
K = 1;
}
// cout << "totalCount: " << totalCount << endl;
// cout << "K: " << K << endl;
// 开始查找 block
int sum = 0;
int i;
for (i = 0; i < SQRTN; i++)
{
if (sum + block[i] >= K)
{
break;
}
sum += block[i];
}
// 在第 i 个 block 中,开始查找 table
int j;
for (j = i * SQRTN; j < (i + 1) * SQRTN; j++)
{
// 找到了
if (sum + table[j] >= K)
{
cout << j << endl;
return;
}
sum += table[j];
}
}
/* Process the command.
* 分块思想
* cmdNum: command 的映射值
* arg: 可能的参数 (push 的值)
*/
void doFunction(int cmdNum, int arg)
{
// cout << "cmdNum: " << cmdNum << " arg: " << arg << endl;
// // push
if (cmdNum == 0)
{
// 对应的 block 的元素个数 + 1
block[arg / SQRTN] += 1;
// table 对应的该元素的个数 + 1
table[arg] += 1;
// totalCount += 1
totalCount += 1;
extendStack.push(arg);
}
// peekmedian
else if (cmdNum == 1)
{
if (extendStack.empty())
{
cout << "Invalid. " << endl;
return;
}
// do peekmedian
peekMedian();
}
// pop
else if (cmdNum == 2)
{
if (extendStack.empty())
{
cout << "Invalid. " << endl;
return;
}
// delete
int deleteArg = extendStack.top();
cout << deleteArg << endl;
block[deleteArg / SQRTN] -= 1;
table[deleteArg] -= 1;
totalCount -= 1;
extendStack.pop();
}
}
// initialize
void init()
{
// push: 0, peekmedian: 1, pop: 2
commandMap.insert(make_pair(str_toLower("Push"), 0));
commandMap.insert(make_pair(str_toLower("PeekMedian"), 1));
commandMap.insert(make_pair(str_toLower("Pop"), 2));
cout << "Input the number of commands: ";
int commandNum;
cin >> commandNum;
// 接收 commands
for (int i = 0; i < commandNum; i++)
{
// 接收 command, arg
string cmdtemp;
int arg = -1;
cin >> cmdtemp;
// 如果是 push, 则需要接收一个参数
if (commandMap[str_toLower(cmdtemp)] == 0)
{
cin >> arg;
}
// 然后进行 process the command
doFunction(commandMap[str_toLower(cmdtemp)], arg);
}
}
int main()
{
init();
return 0;
}