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的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. Oracle存储过程知识汇总

    基本语法篇: CREATE OR REPLACE PROCEDURE 存储过程名 //CREATE OR REPLACE PROCEDURE 是一个SQL语句通知Oracle数据库去创建一个叫做ske ...

  2. Silverlight 独立存储(IsolatedStorageFile)

    1.在Web中添加天气服务引用地址 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 2.在Web中添加Wcf服务接口I ...

  3. Tomcat配置虚拟主机后的登录验证码问题

    先描述一下问题现象,在本地测试运行一个java web网站,一切正常.但把网站部署到Linux服务器上后,发现登录出了问题,提示验证码输入不正确.登录时需要输入验证码,而验证码的原值是先存入sessi ...

  4. mysql 排重查询

    GROUP BY 语句可以实现某一列的去重查询. 直接上语句: select io_dev_id from io_info where (TID=1 AND host_name='yang1') GR ...

  5. Microsoft Visual C++ Runtime error解决方法

    1: 当出现下图时提示Microsoft Visual C++ Runtime error 2:此时不要关闭该对话框,然后打开任务管理器(Ctrl+Shift+Esc)如下图: 找到Microsoft ...

  6. Windos中无法删除桌面IE图标的解决方法

    解决方法其实并不难,打开注册表,转到如下图的位置,详细地址在图片最下面: 需要注意的是,你需要在NameSpace中逐个查看各个项目的数据值,显示为数据值为Internet Explorer的项目即为 ...

  7. 倒水问题 (codevs 1226) 题解

    [问题描述] 有两个无刻度标志的水壶,分别可装x升和y升 ( x,y 为整数且均不大于100)的水.设另有一水缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知x升壶为空壶, ...

  8. C# 代码重启windows服务

    ServiceController service = new ServiceController("EnergyRecordService"); protected void b ...

  9. GoogleMapApi 发布后提示安全问题

    今天日本那边发过来一个Bug说是Google Map打不开,提示安全问题. 最后发现,日本那边的发布路径如下: https:xxxxx.gspserver.co.jp 而Source中Google M ...

  10. 小课堂week13 Clean Code Part2

    Clean Code Part2 对象与数据结构 首先让我们进行一个严肃的思考,对象与数据结构的区别在哪里? 如下两段代码分别用数据结构和对象的方法来描述了一个Point. public class ...