poj 2153 Rank List
原题链接: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 ;
}
poj 2153 Rank List的更多相关文章
- poj 2153 Rank List(查找,Map)
题目链接:http://poj.org/problem?id=2153 思路分析: 判断Li Ming的成绩排名,需要在所有的数据章查找成绩比其高的人的数目,为查找问题. 查找问题可以使用Hash表, ...
- POJ 2153 Rank List (map映射)
水题,竟然花了那么多时间...主要是不知道为什么,明明在本机上编译过去了,但是用c++提交却编译错误...最后用g++提交AC 题意:给出n个学生的名字,然后给出m个测验. 每个测验给出n个学生的分数 ...
- 成绩累加排名,poj(2153)
题目链接:http://poj.org/problem?id=2153 解题报告: 注意map中的string,因此要将char[]转换为string型. #include <iostream& ...
- poj 2153
题意:题目还是很简单的,就是求Li Ming 在班上的排名,而且成绩是相加的. 思路:用map就行.不然好像用qsort+二分也可以,不过我在那里碰到了一些状况,然后就没用这种方法了,简单的map就可 ...
- POJ 2153 stl
#include<iostream> #include<map> #include<string> using namespace std; int main() ...
- POJ - 1245 Programmer, Rank Thyself
POJ - 1245 Programmer, Rank Thyself Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d ...
- POJ 2379 ACM Rank Table(排序)
题很水,数据注意一下四点即可: 1.有些team会在一道题AC了之后还提交,这个时候只需要算第一次ac的时间以及这之前的wa,之后的全部忽略.2.如果一道题没有ac,那么在计算时间时不应该加上它的wa ...
- 字符串专题:KMP POJ 3561
http://poj.org/problem?id=3461 KMP这里讲的不错next的求法值得借鉴 http://blog.sina.com.cn/s/blog_70bab9230101g0qv. ...
- poj 并查集
http://poj.org/problem?id=1611 水题 题意:就是找一共有多少个人感染了,0是感染学生的编号. #include <stdio.h> #include < ...
随机推荐
- 第4章 sed命令
1 sed命令基本用法 sed(stream editor)是流编辑器,可对文本文件和标准输入进行编辑: sed只是对缓冲区中原始文件的副本进行编辑,并不编辑原始的文件,如果需要保存改动内容,可以选择 ...
- Spring Boot导出jar包发布
一:事由 现在的项目组开发项目使用的是Spring Boot的技术,开发的时候是直接通过一个入口主函数来启动项目的.如果将项目交给客户,怎样才能正确的发布运行呢?百度了一下有关的知识,大概了解到是通过 ...
- HTML DOM(学习笔记二)
嗯,在HTML DOM(学习笔记一)中简单描述了一下HTML DOM 是什么,这一篇将记录下来有关HTML DOM的内容! 1:DOM节点 首先,再来看一下HTML DOM的树状结构,如下图所示: 这 ...
- SQL where 1=1的作用
浅谈where 1=1 1.简单理解的话where 1=1 永真, where 1<>1 永假 2.1<>1 的用处: 用于只取结构不取数据的场合 例如: ...
- Android 操作系统的内存回收机制
参考 http://www.ibm.com/developerworks/cn/opensource/os-cn-android-mmry-rcycl/index.html
- javascript设计模式-观察者模式
观察者模式定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个对象的状态发生变化时就会通知所有的观察者对象,使得它们能够自动更新自己. UML示意图: 其中的角色: Subject:主 ...
- pyhton标准库 json
使用loads方法即可将json字符串转换成python对象,对应关系如下: #JSON Python object dict array list string unicode number (in ...
- npm命令大全
参考网站:https://docs.npmjs.com/ 有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. npm 常用命令 npm install <name ...
- javascript之delete操作符
理解delete 理论 代码段的类型 执行上下文 活动对象 / 变量对象 属性的特性 内置属性与 DontDelete 未声明的赋值 Firebug的困惑 在eval中删除变量 浏览器兼容性 Geck ...
- Button的图像位置设置
[button setImageEdgeInsets:UIEdgeInsetsMake(2, 70, 2,70 )]; [button setImageEdgeInsets:UIEdgeInsetsM ...