Careercup - Google面试题 - 4699414551592960
2014-05-06 13:34
原题:
we have a random list of people. each person knows his own height and the number of tall people in front of him. write a code to make the equivalent queue.
for example :
input: <"Height","NumberOfTall","Name">,
<,,"A">,<,,"B">,<,,"C">,<,,"D">,<,,"E">,<,,"F">
output: "F","E","D","C","B","A" --> end of queue
题目:有一队人正在排队,告诉你每个人的姓名、身高(目前默认都不相同),以及他/她所看到的前面比他高的人。请你计算出这个队伍里各个人的顺序。
解法:从最矮的人入手。对于最矮的人,他所看到的所有人都比他高,所以如果他看到有K个人比他高,那么他一定排在K + 1名。我的代码是当时琢磨了很久,采用树状数组写出来的。此处的树状数组提供快速修改区间、查询单个元素的能力,能做到对数时间。现在居然已经忘了当时的具体思路。总体思想是先按身高升序排序,然后逐个确定每个人在队列里的位置。关键的一点:每次都只能确定当前最矮的人排在哪儿。树状数组中维护的值c[i]的意义,是记录当前位置的前面有多少个比它高。其中还有个方法用到二分搜索,所以整体复杂度是比较奇怪的O(n * log(n) + n * log^2(n)) = O(n * log^2(n))。
代码:
// http://www.careercup.com/question?id=4699414551592960
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std; struct Person {
int height;
int taller;
string name;
Person(int _height = , int _taller = , string _name = ""):
height(_height), taller(_taller), name(_name) {};
bool operator < (const Person &other) {
return this->height < other.height;
}; friend ostream& operator << (ostream &cout, const Person &p) {
cout << '<'<< p.name << p.height << '>';
return cout;
};
}; template <class T>
class BinaryIndexedTree {
public:
BinaryIndexedTree(int _n = ): n(_n), v(_n + ) {}; int size() {
return n;
}; void addAll(int x, const T &val) {
while (x >= && x <= n) {
v[x] += val;
x -= lowBit(x);
}
}; void addInterval(int x, int y, const T &val) {
addAll(x - , -val);
addAll(y, val);
}; void clear() {
v.resize();
n = ;
}; void resize(int new_n) {
v.resize(new_n + );
n = new_n;
} T sum(int x) {
T res = ;
while (x >= && x <= n) {
res += v[x];
x += lowBit(x);
} return res;
}; int lowerBound(const T &val) {
int ll, mm, rr; if (n == ) {
return ;
} T res;
if (val <= (res = sum())) {
return ;
}
if (val > (res = sum(n))) {
return n + ;
} ll = ;
rr = n;
while (rr - ll > ) {
mm = (ll + rr) / ;
res = sum(mm);
if (val > res) {
ll = mm;
} else {
rr = mm;
}
} return rr;
}
private:
vector<T> v;
int n; int lowBit(int x) {
return x & -x;
};
}; int main()
{
vector<Person> people;
vector<int> queue;
BinaryIndexedTree<int> bit;
int i, j;
int n; while (cin >> n && n > ) {
people.resize(n);
for (i = ; i < n; ++i) {
cin >> people[i].height >> people[i].taller >> people[i].name;
}
sort(people.begin(), people.end());
bit.resize(n);
queue.resize(n);
for (i = ; i <= n; ++i) {
bit.addInterval(i, n, );
}
for (i = ; i <= n; ++i) {
j = bit.lowerBound(people[i - ].taller + );
queue[j - ] = i;
bit.addInterval(j, n, -);
}
for (i = ; i < n; ++i) {
cout << people[queue[i] - ];
}
cout << endl; people.clear();
queue.clear();
bit.clear();
} return ;
}
Careercup - Google面试题 - 4699414551592960的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- PUTTY使用Ctrl+s僵死的问题
算是分享个小经验吧! 一直都是使用VM+PUTTY的方式调试Linux程序,有时候在Vi中编辑了程序,Windowns下旧习难改,顺手就Ctrl+s了,尽管我知道Vi的保存是:w. 很不幸这时的PUT ...
- 设计模式-外观模式(Facade)
简介 外观模式(Facade),将外部与内部子系统的通信通过一个统一的门面对象进行. 由两部分组成: 门面角色:供外部调用,内部可能组装了多个子系统,多个方法. 子系统角色:子系统的方法也可以直接供外 ...
- HTML5学习笔记简明版(1):HTML5介绍与语法
HTML5介绍 HTML5是继HTML4以后的下一代HTML标准规范,它提供了一些新的元素和属性(例如<nav>网站导航块和<footer>).新型的标签有利于搜索引擎和语义分 ...
- SQLServer附加数据库5120错误
装有MSSQL的电脑 需要附加的数据库文件(*.mdf)及其日志文件(*.ldf) 1. 打开SQL Server Management Studio,并连接上数据库.右键"数据库" ...
- CentOS6 更改Mysql数据库的数据存放位置
mysql使用yum安装时,默认的数据是存储在/var/lib/mysql下.一般情况下,为了数据的安全性,建议将mysql数据库的数据文件存储在系统的第二块磁盘上的目录下可以按照以下步骤进行操作: ...
- 第一部分 CLR基础:第3章 共享程序集和强命名程序集
第一部分 CLR基础:第3章 共享程序集和强命名程序集
- 开篇 hello 内Cool超人
经过一年时间看到asp.net mvc一直被受微软开发团队的注重.与之相比的silverlight我感觉到有点力不从心.除去silverlight第一次运行要安装Runtime不说,产品不可能只运行在 ...
- JS获取网页属性包括宽、高等
JS获取网页属性包括宽.高等. function getInfo() { // www.jbxue.com var s = ""; s += " 网页可见区域宽:&q ...
- js中settimeout方法加参数
js中settimeout方法加参数的使用. 简单使用看w3school 里面没有参数调用, 例子: <script type="text/javascript"> ...
- [读书心得]资料分页的优化,以SQL 2012的 OFFSET-FETCH为例
这是我的文章备份,原始出处:[读书心得]资料分页的优化,以SQL 2012的 OFFSET-FETCH为例 http://www.dotblogs.com.tw/mis2000lab/archive/ ...