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 ...
随机推荐
- 移植u-boot-1.1.6之mtdparts分区
和u-boot高版本不同,mtdparts命令没有cmd_mtdparts这么一个单独的文件来实现. 不过,搜索uboot可以在cmd_jffs2.c里面看到如下代码: U_BOOT_CMD( mtd ...
- ref和out的区别,值类型和引用类型的使用
今天刚刚明白ref和out的区别,只限于个人理解如有不同请赐教,谢谢 首先我感觉ref和out是针对于值类型来说,以前一直认为是针对于引用类型看下面的一段代码 1.首先结果 i=0:ints[0]=0 ...
- ie8以下不兼容document.getElementsByName解决方法
在IE8以认为只有文本标签才有name属性的,一些元素标签用document.getElementsByName获取不到DOM,如DIV,span等,这里做一下兼容. HTML: <div na ...
- input 文本框和 img 验证码对齐问题
input 文本框和 img 验证码对齐问题 在网页制作中,常将 input 和 img 放在同一行,img标签总是比input高出一个头,非常难看. CCS实现input和img水平对齐的方法 同时 ...
- Win2003打不开https的问题
碰到客户做问题是能打开https://www.baidu.com 这个网页 打不开 https://sha256.alipay.com/SHA256/index.htm支付宝这个网页 解决办 ...
- 如何调节datagridview中字体
设置ColumnHeaderDefaultCellStyle的Font属性 或者 编程 datagridview.Columns[index].DefaultCellStyle.Font.Size=“ ...
- 【转】Spark性能测试报告
RDD可以很好地适用于支持数据并行的批量分析应用,包括数据挖掘,机器学习,图算法等,因为这些程序通常都会在很多记录上执行相同的操作.RDD不太适合那些异步更新共享状态的应用,例如并行web爬行器.因此 ...
- LayoutInflater中四种类型inflate方法的介绍
转自:http://blog.csdn.net/aa4790139/archive/2011/05/07/6401556.aspx 第一种: public View inflate (int reso ...
- WCF全面解析第一章 WCF 简介
1.WCF中的 "A","B","C" 介绍 我们先看个生活中的例子,某一天,公司的领导让你去送一份合同文件,送文件的过程你可以选择的交通方 ...
- SQL基础篇----select语句与排序问题
一.检索--输出所有的列 SELECT * FROM my_friends WHEREfirst_name = 'cake'; 知识点1 * 代表选择出所有的行-----(什么行呢?)就是first_ ...