5-18 Hashing - Hard Version (30分)
#include <iostream>
#include <vector>
#include <queue>using namespace std;struct node
{int key, index;bool operator < (const node &nod) const{return nod.key < key;}
};node initnode(int key, int index)
{node nod;nod.key = key;nod.index = index;return nod;
}int main()
{int n;scanf("%d", &n);int hash[1010], i, indegree[1010];for(i = 0; i < n; i++){scanf("%d", &hash[i]);if(hash[i] < 0){indegree[i] = -1;}else{indegree[i] = 0;}}int curindex, j;vector<int> v[1010];for(i = 0; i < n; i++){if(indegree[i] == -1){continue;}curindex = hash[i] % n;indegree[i] = (i - curindex + n) % n;for(j = 0; j < indegree[i]; j++){v[(curindex + j) % n].push_back(i);}}priority_queue<node> q;for(i = 0; i < n; i++){if(indegree[i] == 0){q.push(initnode(hash[i], i));}}int size, nextindex;vector<int> res;node curnod;while(q.empty() == false){curnod = q.top();q.pop();res.push_back(curnod.key);size = v[curnod.index].size();for(i = 0; i < size; i++){nextindex = v[curnod.index][i];indegree[nextindex]--;if(indegree[nextindex] == 0){q.push(initnode(hash[nextindex], nextindex));}}}size = res.size();for(i = 0; i < size; i++){if(i > 0){printf(" ");}printf("%d", res[i]);}printf("\n");system("pause");return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/980863.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!