L2-014 列车调度 (25 分)
- set.lower_bound返回迭代器,如果等于end说明没找到
- set.erase()中参数是迭代器
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> se;
int n;
cin >> n;
for (int i = 0; i < n; ++ i) {
int x;
cin >> x;
auto it = se.lower_bound(x);
if (it != se.end()) {
se.erase(it);
}
se.insert(x);
}
cout << se.size();
}
L2-019 悄悄关注 (25 分)
#include <iostream>
#include <unordered_set>
#include <map>
using namespace std;
int main() {
int n;
cin >> n;
unordered_set<string> se;
for (int i = 0; i < n; ++ i) {
string x;
cin >> x;
se.insert(x);
}
map<string, int> ma;
int m;
cin >> m;
int sum = 0;
for (int i = 0; i < m; ++ i) {
string x;
int y;
cin >> x >> y;
ma[x] = y;
sum += y;
}
double avg = sum * 1.0 / m;
bool ok = false;
for (auto x : ma) {
if (x.second > avg && se.find(x.first) == se.end()) {
ok = true;
cout << x.first<< endl;
}
}
if (!ok) {
cout << "Bing Mei You";
}
}
L3-002 特殊堆栈 (30 分)
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main() {
int n;
cin >> n;
stack<int> stk;
vector<int> ve;
while (n -- ) {
string s;
cin >> s;
bool ok = true;
if (s == "Push") {
int key;
cin >> key;
stk.push(key);
ve.insert(lower_bound(ve.begin(), ve.end(), key), key);
} else if (s == "Pop") {
if (stk.size()) {
int now = stk.top();
stk.pop();
cout << now << endl;
ve.erase(lower_bound(ve.begin(), ve.end(), now));
} else {
ok = false;
}
} else {
if (stk.size()) {
int n = stk.size();
if (n % 2) {
cout << ve[(n + 1) / 2 - 1] << endl;
} else {
cout << ve[n / 2 - 1] << endl;
}
} else {
ok = false;
}
}
if (!ok) {
cout << "Invalid" << endl;
}
}
}