2014-05-12 07:44

题目链接

原题:

Given an array having  unique integers, each lying within the range <x<, how do u sort it. U can load only  numbers at a time in memory.

题目:一个数组里有16000个不重复的整数,所有元素都在1和20000之间。如果你一次至多能在内存里放1000个整数,你要如何排序整个数组?

解法1:这个题目表述是不是有问题?数组本身就是内存的数据吧?内存装不下还叫数组?既然所有元素都是不重复的,就可以用位向量了。用位向量标记,就可以通过一次扫描来排序所有元素了。如果按照一个整数4字节的话,1000个整数有4000字节,总共32000个位,是足够覆盖数据范围的。如果按照16位整数来算,这个算法就不可行了。《编程珠玑》和《Cracking the Coding Interview》上都有类似的题目。内存限制是任何时候都要考虑的实际问题。如果资源总是无限的,这个世界也就没问题需要解决了。

代码:

 // http://www.careercup.com/question?id=23123665
// all numbers are unique.
#include <cstdio>
#include <cstring>
using namespace std; int main()
{
const int MAXN = ;
FILE *fin, *fout;
unsigned bit[MAXN >> ];
int i; memset(bit, , MAXN / ); fin = fopen("in.txt", "r");
while (!feof(fin)) {
fscanf(fin, "%u", &i);
bit[i >> ] |= ( << (i & ));
}
fclose(fin);
fin = nullptr; fout = fopen("out.txt", "w");
for (i = ; i < MAXN; ++i) {
if (bit[i >> ] & ( << (i & ))) {
fprintf(fout, "%u\n", i);
}
}
fclose(fout);
fout = nullptr; return ;
}

解法2:如果元素存在重复的话,就不能用位向量了。可以用数组来分段统计每个数据范围元素出现的个数,这样能通过多次扫描达到排序的效果。

代码:

 // http://www.careercup.com/question?id=23123665
// may contain duplicates.
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; int main()
{
const int MAXN = ;
const int n = ;
FILE *fin, *fout;
int count[n];
int i, j;
int offset; fin = fopen("in.txt", "r");
fout = fopen("out.txt", "w");
if (fin == nullptr || fout == nullptr) {
printf("File doesn't exist.\n");
exit();
} offset = ;
while (offset < MAXN) {
memset(count, , n * sizeof(int)); fseek(fin, , SEEK_SET);
while (!feof(fin)) {
fscanf(fin, "%d", &i);
if (i >= offset && i < offset + n) {
++count[i - offset];
}
} for (i = ; i < n; ++i) {
for (j = ; j < count[i]; ++j) {
fprintf(fout, "%d\n", i + offset);
}
} offset += n;
}
fclose(fin);
fin = nullptr;
fclose(fout);
fout = nullptr; return ;
}

Careercup - Microsoft面试题 - 23123665的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. 构建第一个Spring Boot2.0应用之集成mybatis、Druid(七)

    一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.说明:      本文综合之 ...

  2. linux中python安装

    1.查看当前环境中是否存在python安装包 [zyj@localhost ~]$ rpm -qa | grep python gnome-python2-gnome--.el6.x86_64 pyt ...

  3. python3基础08(exec、bytearray使用等)

    #!/usr/bin/env python# -*- coding:utf-8 -*- str="test"print(ascii(str))a=bytearray("a ...

  4. 解决IE下面诡异地使用quickrIE5模式打开页面的有关问题

    解决IE下面诡异地使用quickrIE5模式打开页面的有关问题 <!doctype html public "-//w3c//dtd html 4.01 transitional//e ...

  5. Missing map from Nullable`1 to String. Create using Mapper.CreateMap<Nullable`1, String>. 解决办法

    这是一个叫做AutoMapper的插件,主要功能是让两个类的内容进行映射,最常见的例子就是EF查询出的内容映射到一个实体类上去然后返回这个实体类例如: Mapper.CreateMap(); 如果这时 ...

  6. 简析平衡树(二)——Treap

    前言 学完了替罪羊树,我决定再去学一学\(Treap\).一直听说\(Treap\)很难,我也花了挺久才学会. 简介 \(Treap\)这个名字真的挺有内涵: \(\color{red}{Tree}\ ...

  7. 索引属性 unique指定

    比较重要的属性有: 名字 db.collection.ensureIndex({},{name:''}) 在创建索引时,mongodb会自己给索引创建默认的名字,这种名字并不好记,我们看一下mongo ...

  8. 【SQL】连接 —— 内连接、外连接、左连接、右连接、交叉连接

    连接 · 内连接 · 外连接 · 左连接 · 右连接 · 全连接 · 交叉连接 · 匹配符号(+)  连接  根据表之间的关系,呈现跨表查询的结果.     外连接     内连接 左连接 右连接 全 ...

  9. GreenPlum查看表和数据库大小

    表大小 zwcdb=# select pg_size_pretty(pg_relation_size('gp_test')); pg_size_pretty ---------------- 1761 ...

  10. vue 服务代理 调用第三方api

    项目中前期需要调用第三方API来获取汇率.因为直接调用会有跨域的问题,所以使用来服务代理. 在config配置代理可以这样写: 而调用接口就可以这样写: 坑:配置完成后一直报500,开始怀疑人生.最后 ...