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 < ...
随机推荐
- 出现win8.1蓝屏重启故障该如何解决?
出现win8.1蓝屏重启故障该如何解决?电脑出现win8.1蓝屏重启故障,系统提示“你的电脑遇到问题,需要重新启动,我们只收集某些错误信息,然后为你重新启动.”怎么解决?本教程为你提供了win8.1蓝 ...
- 洛谷P1631 序列合并
P1631 序列合并 236通过 657提交 题目提供者xmyzwls 标签堆 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 为什么不行? 题目描述 有两个长度都是N的序列A和B,在A和B中 ...
- 【HTML/XML 12】URI、URN、URL的联系和区别
导读:在学习XML的时候,书中有很多个地方都提到URL等几个概念,再之前做项目的时候,重定向或是转发时,也用到了这个URL,在学习Ajax时,ajax破坏了统一资源定位(URN)都或多或少的接触到了这 ...
- Windows API学习---用户方式中的线程同步
前言 当所有的线程在互相之间不需要进行通信的情况下就能够顺利地运行时, Micrsoft Windows的运行性能最好.但是,线程很少能够在所有的时间都独立地进行操作.通常情况下,要生成一些线程来处理 ...
- ionic 不同view的數據交互
angular中通過service factory 等服務來對不同的控制器進行數據交互 ,ionic 也一樣... var app = angular.module('ionicApp', ['ion ...
- 使用throws抛出异常
声明抛出异常实在一个方法声明的throws子句中指明的.throws子句方法的基本形式如下.方法 throws 异常列表{ }throws子句中可以指明多个异常,说明该方法不对这些异常进行处理,而是抛 ...
- 011OK6410开发板介绍
1.系统资源: (1)处理器:三星ARM11,S3C6410A,主频533MHz/667MHz (2)nor flash (3)nand flash:1G字节NAND Flash (4)RAM:128 ...
- THINKPHP 清除HTML注释、换行符、空格、制表符等
thinkphp3.2 3.2中取消了配置文件中的 'TMPL_STRIP_SPACE' 属性,所以我们先来修改:\ThinkPHP\Library\Think\Template.class.php ...
- php中使用end方法报错
<b>Strict Standards</b>: Only variables should be passed by reference in <b> 1.如果 ...
- 使用plupload绕过服务器,批量上传图片到又拍云
本文最初发布于我的个人博客:Jerry的乐园 综述 论坛或者贴吧经常会需要分享很多图片,上传图片比较差的做法是上传到中央服务器上,中央服务器再转发给静态图片服务器.而这篇文章讲介绍如何使用pluplo ...