003:Set
#include <iostream>
#include<set>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
typedef multiset<int>::iterator MultIt;
void add(multiset<int> &MSet, set<int> &st,int num) {
MSet.insert(num);
st.insert(num);
MultIt l, h;
l = MSet.lower_bound(num);
h = MSet.upper_bound(num);
int count = 0;
while (l != h) {
count++;
l++;
}
cout << count << endl;
}
void ask(const multiset<int>& MSet, const set<int>& st, int num) {
if (st.find(num) == st.end())
cout << 0 << ' ' << 0 << endl;
else {
cout << 1 << ' ';
MultIt l, h;
l = MSet.lower_bound(num);
h = MSet.upper_bound(num);
int count = 0;
while (l != h) {
count++;
l++;
}
cout << count << endl;
}
}
int main()
{
//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
multiset<int> MSet;
set<int> st;
char cmd[10];
int n;
cin >> n;
while (n--) {
int num;
cin >> cmd >> num;
if (strcmp("add", cmd) == 0)
add(MSet, st, num);
else if (strcmp("del", cmd) == 0)
cout << MSet.erase(num) << endl;
else if (strcmp("ask", cmd) == 0)
ask(MSet,st,num);
}
return 0;
}
004:热血格斗场
#include <iostream>
#include<set>
#include<cmath>
#pragma warning(disable:4996)
using namespace std;
struct Player {
int id;
int attack;
};
struct rule{
bool operator() (const Player& p1, const Player& p2) const{
return p1.attack < p2.attack;
}
};
typedef multiset<Player, rule>::iterator iter;
int main()
{
//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
multiset<Player, rule> st;
int n;
cin >> n;
Player p = {1,1000000000 };
st.insert(p);
while (n--) {
cin >> p.id >> p.attack;
cout << p.id << ' ';
iter i;
i = st.upper_bound(p);
int l, h = i->attack, hid = i->id;
if (i != st.begin()) {
l = (--i)->attack;
if (abs(p.attack - l) <= abs(p.attack - h))
cout << i->id;
else
cout << hid;
}
else
cout << hid;
cout << endl;
st.insert(p);
}
return 0;
}