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">,
<6,2,"A">,<1,4,"B">,<11,0,"C">,<5,1,"D">,<10,0,"E">,<4,0,"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))。

代码:

  1 // http://www.careercup.com/question?id=4699414551592960
  2 #include <algorithm>
  3 #include <iostream>
  4 #include <string>
  5 #include <vector>
  6 using namespace std;
  7
  8 struct Person {
  9     int height;
 10     int taller;
 11     string name;
 12     Person(int _height = 0, int _taller = 0, string _name = ""):
 13         height(_height), taller(_taller), name(_name) {};
 14     bool operator < (const Person &other) {
 15         return this->height < other.height;
 16     };
 17
 18     friend ostream& operator << (ostream &cout, const Person &p) {
 19         cout << '<'<< p.name << p.height << '>';
 20         return cout;
 21     };
 22 };
 23
 24 template <class T>
 25 class BinaryIndexedTree {
 26 public:
 27     BinaryIndexedTree(int _n = 0): n(_n), v(_n + 1) {};
 28
 29     int size() {
 30         return n;
 31     };
 32
 33     void addAll(int x, const T &val) {
 34         while (x >= 1 && x <= n) {
 35             v[x] += val;
 36             x -= lowBit(x);
 37         }
 38     };
 39
 40     void addInterval(int x, int y, const T &val) {
 41         addAll(x - 1, -val);
 42         addAll(y, val);
 43     };
 44
 45     void clear() {
 46         v.resize(1);
 47         n = 0;
 48     };
 49
 50     void resize(int new_n) {
 51         v.resize(new_n + 1);
 52         n = new_n;
 53     }
 54
 55     T sum(int x) {
 56         T res = 0;
 57         while (x >= 1 && x <= n) {
 58             res += v[x];
 59             x += lowBit(x);
 60         }
 61
 62         return res;
 63     };
 64
 65     int lowerBound(const T &val) {
 66         int ll, mm, rr;
 67
 68         if (n == 0) {
 69             return 0;
 70         }
 71
 72         T res;
 73         if (val <= (res = sum(1))) {
 74             return 1;
 75         }
 76         if (val > (res = sum(n))) {
 77             return n + 1;
 78         }
 79
 80         ll = 1;
 81         rr = n;
 82         while (rr - ll > 1) {
 83             mm = (ll + rr) / 2;
 84             res = sum(mm);
 85             if (val > res) {
 86                 ll = mm;
 87             } else {
 88                 rr = mm;
 89             }
 90         }
 91
 92         return rr;
 93     }
 94 private:
 95     vector<T> v;
 96     int n;
 97
 98     int lowBit(int x) {
 99         return x & -x;
100     };
101 };
102
103 int main()
104 {
105     vector<Person> people;
106     vector<int> queue;
107     BinaryIndexedTree<int> bit;
108     int i, j;
109     int n;
110
111     while (cin >> n && n > 0) {
112         people.resize(n);
113         for (i = 0; i < n; ++i) {
114             cin >> people[i].height >> people[i].taller >> people[i].name;
115         }
116         sort(people.begin(), people.end());
117         bit.resize(n);
118         queue.resize(n);
119         for (i = 1; i <= n; ++i) {
120             bit.addInterval(i, n, 1);
121         }
122         for (i = 1; i <= n; ++i) {
123             j = bit.lowerBound(people[i - 1].taller + 1);
124             queue[j - 1] = i;
125             bit.addInterval(j, n, -1);
126         }
127         for (i = 0; i < n; ++i) {
128             cout << people[queue[i] - 1];
129         }
130         cout << endl;
131
132         people.clear();
133         queue.clear();
134         bit.clear();
135     }
136
137     return 0;
138 }

转载于:https://www.cnblogs.com/zhuli19901106/p/3711401.html

Careercup - Google面试题 - 4699414551592960相关推荐

  1. Careercup - Google面试题 - 5424071030341632

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

  2. Careercup - Google面试题 - 5377673471721472

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

  3. Careercup - Google面试题 - 4877486110277632

    2014-05-08 05:16 题目链接 原题: Given a circle with N defined points and a point M outside the circle, fin ...

  4. Google 面试题和详解

    Google的面试题在刁钻古怪方面相当出名,甚至已经有些被神化的味道.这个话题已经探讨过很多次,而科技博客 BusinessInsider这两天先是贴出15道Google面试题并一一给出了答案,其中不 ...

  5. 【Google面试题】有四个线程1、2、3、4同步写入数据…C++11实现

    Google面试题 有四个线程1.2.3.4.线程1的功能就是输出1,线程2的功能就是输出2,以此类推-现在有四个文件ABCD.初始都为空.现要让四个文件呈如下格式: A:1 2 3 4 1 2- B ...

  6. Google面试题之100层仍两个棋子

    一道Google面试题,题目如下:"有一个100层高的大厦,你手中有两个相同的玻璃围棋子.从这个大厦的某一层扔下围棋子就会碎,用你手中的这两个玻璃围棋子,找出一个最优的策略,来得知那个临界层 ...

  7. Google面试题:找几百亿数据的中值

    Google面试题:找几百亿数据的中值 http://blog.csdn.net/jiyanfeng1/article/details/8088237 有几百亿的整数,分布的存储到几百台通过网络连接的 ...

  8. google面试题,生男生女比例?

    Google面试题: 在一个重男轻女的国家里,每个家庭都想生男孩,如果他们生的孩子是女孩,就再生一个,直到生下的是男孩为止,这样的国家,男女比例会是多少? 答案:1:1 分析:  出生男女概率是50% ...

  9. 扔玻璃球 [ Google面试题 ]

    这是一道 Google 面试题,考察的是对于 粗调 和 精调 工程思维 对于扔玻璃球国内也叫扔鸡蛋. ta不用您有什么基础,只需要一个基本工程思维. 粗调和精调,这个已是统计学里最优法,所以不用担心复 ...

最新文章

  1. Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo
  2. windows下重启mysql数据库_windows下重启mysql的方法
  3. ubuntu-基本命令篇-12-磁盘管理
  4. UnityVS(Visual Studio Tools For Unity)的安装与使用
  5. 分析和解析PHP代码的7大工具
  6. VMware vSphere@cloudstack基本功能测试报告
  7. 【LeetCode】617. 合并二叉树
  8. Ubuntu和Win7双系统,ubuntu被删,重新启动之后显示,no such partition
  9. 本地搭建WordPress (XAMPP环境)
  10. 主机屋linux怎么连,全网最详细的samba文件共享服务!
  11. Python快速使用jira模块调用Jira接口
  12. 想拥有高清壁纸不用那么麻烦!
  13. HTTP和HTTPS的区别及HTTPS加密算法
  14. ComputeColStats UDF中 近似算法的介绍(续)
  15. Openbox-桌面图标设置
  16. android获取组件id,Android 获取控件id的三种方式
  17. rdpwrap 在 GitHub上 被禁了,谁能提供 autoupdate-v07.09.2019.zip ? 给分
  18. java实现小写转大写_人民币小写转大写(Java实现)
  19. 【Go】dep使用介绍
  20. Java面试题----基础

热门文章

  1. c#_textbox显示刷新规定行数的数据
  2. android游戏画面抖动,抖音游戏主播是怎么直播手机画面的?
  3. mc pe Linux服务器,MC 基岩版(PE) 服务器来了
  4. qml 自定义消息框_Qt qml 自定义消息提示框
  5. excel mysql日报_Excel日报自动化
  6. typeorm 修改事务_nest.js + typeORM: 身份认证, 事务管理
  7. 基类和派生类写在一个文件中_BootISO:从 ISO 文件中创建一个可启动的 USB 设备...
  8. git如何查看sshkey_Jenkins配置SSH Key下载代码
  9. 乐高机器人亮剑_2500名选手大比拼 全球机器人广州从化“亮剑”
  10. 转子接地保护原理_罗茨鼓风机(压缩机)原理和操作规程