1078 Hashing

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be “H(key) = key % TSize” where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print “-” instead.

Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -

解题思路:坑爹的二次探测,一开始以为是一道很简单的题,结果还是被坑了,完全没有注意到冲撞处理策略,所以最后一个点一直过不了,后来看了别人的博客说要经过二次探测检测之后还是不能插入才输出“-”,不是一开始不能插入就直接输出“-”。too young
二次冲撞检测就是,在i位置无法插入的情况下,进行i+1^2,i+2^2,i+3^2….依次递增的位置检测,如果检测到可以插入那就插入,一直找不到才输出“-”,网上别人定的范围是到30000^2,自己发现到10000^2也可以过,关于这个范围的指定不是很清楚为什么。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
bool isPrime(int n){
    if (n < 2){
        return false;
    }
    for (int i = 2; i*i <= n+1; i++){
        if (n%i == 0){
            return false;
        }
    }
    return true;
}
int main(){
    //10007
    int primes[10050];
    int per_prime = 0;
    for (int i = 10049; i >= 0; i--){
        if (isPrime(i)){
            per_prime = i;
            primes[i] = i;
        }
        else{
            primes[i] = per_prime;
        }
    }
    for (int m, n; scanf("%d%d", &m, &n) != EOF;){
        int size = primes[m];
        int *flag = new int[size];
        memset(flag, 0, sizeof(int)*size);
        int test = 0;
        while (n > 1 ){
            int temp; 
            test = 0;
            scanf("%d", &temp);
            for (int i = 0; i <= 10000; i++){
                if (!flag[(temp + i*i) % size]){
                    flag[(temp + i*i) % size] = 1;
                    test = 1;
                    printf("%d ", (temp + i*i) % size);
                    break;
                }
            }
            if (!test){
                printf("- ");
            }
            n--;
        }
        if (n){
            int temp;
            test = 0;
            scanf("%d", &temp);
            for (int i = 0; i <= 10000; i++){
                if (!flag[(temp + i*i) % size]){
                    flag[(temp + i*i) % size] = 1;
                    test = 1;
                    printf("%d\n", (temp + i*i) % size);
                    break;
                }
            }
            if (!test){
                printf("-\n");
            }
        }
    }
    return 0;
}
### Voxel Hashing 技术详解 Voxel hashing 是一种用于高效存储和访问三维空间数据的技术,在计算机图形学和3D建模领域广泛应用。它通过将连续的三维空间划分为离散的小立方体(即体素),并利用哈希表来快速定位这些体素的位置。 #### 基本原理 Voxel hashing 的核心在于使用稀疏的数据结构表示三维场景中的体素[^1]。由于大多数实际应用中的三维模型并不完全填充整个空间,因此采用稠密数组会浪费大量内存资源。相反,voxel hashing 使用哈希函数将体素映射到一个紧凑的哈希表中,从而显著减少所需的存储量。 #### 数据结构设计 为了支持高效的查询操作,通常需要定义如下几个关键组件: - **Hash Function**: 将三维坐标 `(x, y, z)` 转换为唯一的键值,以便索引对应的哈希桶。 ```cpp uint64_t hash(uint32_t x, uint32_t y, uint32_t z) { const uint64_t prime = 73856093; return (uint64_t(x * prime ^ y * prime ^ z * prime)) % TABLE_SIZE; } ``` - **Table Entry Structure**: 每个哈希表条目保存有关该体素的信息,例如其位置、属性或其他元数据。 ```cpp struct VoxelEntry { int position_x, position_y, position_z; // 体素的空间位置 float density; // 密度或颜色等属性 bool occupied; // 是否被占用标志位 }; ``` #### 查询与更新机制 当程序接收到一个新的点云或者网格顶点时,可以通过以下方式将其分配至最近的体素单元,并执行相应的处理逻辑: 1. 计算目标点所属的体索单元编号; 2. 利用上述提到的哈希函数计算出对应于该体素的哈希槽地址; 3. 如果发现冲突,则尝试线性探测法解决碰撞问题直至找到空闲位置为止;如果未发生冲突则直接写入即可完成新增记录过程[^2]。 此外还需要注意边界条件以及溢出保护措施等问题以免造成错误结果输出甚至崩溃现象的发生。 ```python def insert_voxel(voxels_table, point_position): """Insert a new voxel into the table.""" key = compute_hash(point_position) while voxels_table[key].occupied and not compare_positions(voxels_table[key], point_position): key += 1 # Linear probing to resolve collisions if not voxels_table[key].occupied: initialize_entry(voxels_table[key], point_position) ``` 以上就是关于如何实现基于hash map方法来进行动态管理大规模复杂几何对象集合的一个简单介绍[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值