poj 2153 Rank List

时间:2022-11-07 13:38:20

原题链接:http://poj.org/problem?id=2153 
简单题,map,平衡树均可。。 
map:

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<map>
using std::cin;
using std::map;
using std::string;
map<string, int> ret;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
string buf;
int n, m, v, tmp;
while (cin >> n) {
getchar();
for (int i = ; i < n; i++) {
getline(cin, buf);
ret[buf] = ;
}
cin >> m;
while (m--) {
int ans = ;
for (int i = ; i < n; i++) {
cin >> v;
getchar();
getline(cin, buf);
ret[buf] += v;
}
tmp = ret["Li Ming"];
map<string, int>::iterator ite;
for (ite = ret.begin(); ite != ret.end(); ++ite) {
if (ite->second > tmp) ans++;
}
printf("%d\n", ans + );
}
ret.clear();
}
return ;
}

平衡树:

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
typedef char State[];
char *target = "Li Ming";
const int Max_N = ;
struct Node {
State name;
int data, s;
bool color;
Node *fa, *ch[];
inline void set(int _v, char *src, bool _color, int i, Node *p) {
data = _v, color = _color, s = i;
fa = ch[] = ch[] = p;
strcpy(name, src);
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline int cmp(char *src) const {
if ( == strcmp(src, name)) return -;
else if (- == strcmp(src, name)) return ;
return ;
}
};
struct RedBlackTree {
Node *root, *null;
Node stack[Max_N], *tail;
void init() {
tail = &stack[];
null = tail++;
null->set(, "", , , NULL);
root = null;
}
inline Node *newNode(char *name, int v) {
Node *p = tail++;
p->set(v, name, , , null);
return p;
}
inline void rotate(Node* &x, bool d) {
Node *y = x->ch[!d];
x->ch[!d] = y->ch[d];
if (y->ch[d] != null) y->ch[d]->fa = x;
y->fa = x->fa;
if (x->fa == null) root = y;
else x->fa->ch[x->fa->ch[] != x] = y;
y->ch[d] = x;
x->fa = y;
y->s = x->s;
x->push_up();
}
inline void insert(char *name, int v = ) {
int d = ;
Node *x = root, *y = null;
while (x->s) {
x->s++;
d = x->cmp(name);
y = x, x = x->ch[d];
}
x = newNode(name, v);
if (y != null) { d = x->cmp(y->name), y->ch[!d] = x; }
else root = x;
x->fa = y;
insert_fix(x);
}
inline void insert_fix(Node* &x) {
while (x->fa->color){
Node *par = x->fa, *Gp = par->fa;
bool d = par == Gp->ch[];
Node *uncle = Gp->ch[d];
if (uncle->color) {
par->color = uncle->color = ;
Gp->color = ;
x = Gp;
} else if (x == par->ch[d]) {
rotate(x = par, !d);
} else {
Gp->color = ;
par->color = ;
rotate(Gp, d);
}
}
root->color = ;
}
inline Node *find(Node *x, char *name) {
while (x->s) {
int d = x->cmp(name);
if (- == d) break;
x = x->ch[d];
}
return x;
}
inline void Modify(char *str, int v) {
Node* ret = find(root, str);
ret->data += v;
}
inline void dfs(Node *x, int v, int &ans) {
if (x != null){
dfs(x->ch[], v, ans);
if (x->data > v) ans++;
dfs(x->ch[], v, ans);
}
}
inline void query() {
int cnt = , ans = ;
int v = find(root, target)->data;
dfs(root, v, ans);
printf("%d\n", ans + );
}
}rbt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
char buf[];
int n, m, val;
while (~scanf("%d\n", &n)) {
rbt.init();
for (int i = ; i < n; i++) {
gets(buf);
rbt.insert(buf);
}
scanf("%d\n", &m);
while (m--) {
for (int i = ; i < n; i++) {
gets(buf);
sscanf(buf, "%d", &val);
rbt.Modify(strchr(buf, ' ') + , val);
}
rbt.query();
}
}
return ;
}