N多校2018d3t10 Hash Function

理解hash过程

首先可知,先放进去肯定是h[i]%n==i的位置,这些个位置放进set按照字典序顺序来,然后每安排一个这样点,对于它后一位的值,如果还没被放入set,则如果它%对应的位置已经放入set了则它放入set ~

ac代码:

#include <bits/stdc++.h>
using namespace std;
#define per(i,a,b) for(int i=a;i<b;i++)
#define MP make_pair
typedef pair<int,int> PII;
typedef vector<int> VI;
const int inf =0x3f3f3f;
#define siz 200005
int n,h[siz],f[siz];//f[i]表示i位置现在要考虑的点位置
bool vis[siz];
void init()
{
    memset(vis,false,sizeof(vis));
    per(i,1,n)f[i]=i;
}
int Find(int u){return f[u]==u?u:f[u]=Find(f[u]);}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        init();
        set<PII>st;//带排序功能嘛
        per(i,0,n){scanf("%d",&h[i]);if(h[i]==-1)vis[i]=true;}
        per(i,0,n)f[i]=i;
        per(i,0,n){
            if(vis[i])continue;
            if(h[i]%n == i){st.insert(MP(h[i],i));vis[i]=true;}
        }
        VI ans;
        while(!st.empty()){
            auto tmp=*st.begin();
            st.erase(st.begin());
            ans.push_back(tmp.first);
            assert(Find(tmp.second)==tmp.second);//表示这个点上一步刚处理
            f[tmp.second]=Find((tmp.second+1)%n);
            if(f[tmp.second]!=tmp.second){
                int to=f[tmp.second];
                if(vis[to]==false&&Find(h[to]%n)==to){
                    vis[to]=true;
                    st.insert(MP(h[to],to));
                }
            }
        }
        bool flag=true;
        per(i,0,n)if(vis[i]==false){flag=false;break;}
        if(!flag)puts("-1");
        else {
            if(ans.empty())puts("");
            else{
                per(i,0,(int)ans.size())printf("%d%c",ans[i]," \n"[i==(int)ans.size()-1]);
            }
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/WindFreedom/p/9385303.html

时间: 2024-11-05 06:28:08

N多校2018d3t10 Hash Function的相关文章

Hash function

Hash function From Wikipedia, the free encyclopedia A hash function that maps names to integers from 0 to 15. There is a collision between keys "John Smith" and "Sandra Dee". A hash function is any function that maps data of arbitrary

Lintcode: Hash Function &amp;&amp; Summary: Modular Multiplication, Addition, Power &amp;&amp; Summary: 长整形long

In data structure Hash, hash function is used to convert a string(or any other type) into an integer smaller than hash size and bigger or equal to zero. The objective of designing a hash function is to "hash" the key as unreasonable as possible.

hash function比较

http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用boost::unordered_map替代前者,发现效率上有很大的提升,但是还是无法达到我们的需求: stl::map  底层算法:B+tree 实现 boost::unordered_map 底层算法:hash 实现 所以可能要针对不同的数据类型编写hash function来优化查找和插入的效率,

General Purpose Hash Function Algorithms

General Purpose Hash Function Algorithms [email protected]: http://www.partow.net/programming/hashfunctions/index.html     Description Hashing Methodologies Hash Functions and Prime Numbers Bit Biases Various Forms Of Hashing String Hashing Cryptogra

2017 多校训练 1006 Function

Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 273    Accepted Submission(s): 99 Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. De

hash function 3种方法 1不好 2一般 3好

1. h(k) =  k mod m its is really bad in the practical. if m = even and k is all even.... ( m is size of hash table, modulo ['m?djul?u ) 2. multiplication method. 好一点 a multiplice the k and sum mod 2^w  w is the bit length integer.  two power of two.

lintcode 容易题:Hash Function 哈希函数

题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假设任何字符串都是基于33的一个大整数,比如: hashcode("abcd") = (ascii(a) * 333 + ascii(b) * 332 + ascii(c) *33 + ascii(d)) % HASH_SIZE = (97* 333 + 98 * 332 + 99 * 33

HDU 6050 17多校2 Funny Function(数学+乘法逆元)

Problem Description Function Fx,ysatisfies:For given integers N and M,calculate Fm,1 modulo 1e9+7. Input There is one integer T in the first line.The next T lines,each line includes two integers N and M .1<=T<=10000,1<=N,M<2^63. Output For eac

【HDU2019多校】K - Function (推式子)

SOLUTION: https://www.90yang.com/2019hdu-multi1-k-function/ phi 卷 id 不会线性筛 https://www.cnblogs.com/DeaphetS/p/11228116.html phi 卷 id 不会线性筛  这有一个nlogn的筛法 #include <bits/stdc++.h> typedef long long lld; using namespace std; const size_t MAXN = 1e7+5;