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 ...
随机推荐
- qt5.5实现 记事本程序
最近由于要做Qt相关的毕业设计课题,以前对Qt完全不了解,对于客户端图形界面程序,也只对Windows下的MFC熟悉, 所以,由于Qt的跨平台特性和相对比较纯的C++的特点,就准备学习一下吧.这两天逛 ...
- 标准的CSS盒子模型?与低版本IE的盒子模型有什么不同的?
CSS盒子模型:由四个属性组成的外边距(margin).内边距(padding).边界(border).内容区(width和height); 标准的CSS盒子模型和低端IE CSS盒子模型不同:宽高不 ...
- Linux下Mysql主从复制(Master-Slave)与读写分离(Amoeba)实践
一.为什么要做Mysql的主从复制(读写分离)?通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低.为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻 ...
- redis的lua使用(EVALSHA)
redis 127.0.0.1:6379> SCRIPT LOAD "local list=redis.call('KEYS', KEYS[1] .. '*') return (tab ...
- js闭包理解实例小结
Js闭包 闭包前要了解的知识 1. 函数作用域 (1).Js语言特殊之处在于函数内部可以直接读取全局变量 <script type="text/javascript"> ...
- 完全面向于初学者的Node.js指南
新的上班时间是周二至周六,工作之余当然要坚持学习啦. 希望这篇文章能解决你这样一个问题:“我现在已经下载好Node.Js了,该做些什么呢?” 原文URL:http://blog.modulus.io/ ...
- C 基于UDP实现一个简易的聊天室
引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...
- “Guess the number” game
项目描述:https://class.coursera.org/interactivepython-004/human_grading/view/courses/972072/assessments/ ...
- Windows Phone8.1 SDK中的新控件
前言 WP8.1对开发者的影响要远大于对用户的影响.这篇博客就来一起看看哪些WP8.0中的控件被移除或替换,这些控件的介绍在MSDN上都非常的详细,所以这里只给出一些简单的介绍,来对比8.1 ...
- IOS中 如何去除Tabview里面cell之间的下划线
可以利用Tabview的separatorStyle属性来设置,选择其中的UITableViewCellSeparatorStyleNone 即可去除cell之间的下划线 self.tableView ...