#include <algorithm> #include <cstdio> #include <numeric> #include <random> #include <vector> #include "kollib.h" #include "message.h" typedef long long LL; const int N_NODES = 100; const int N_QUERIES = 201; const int N_BUCKETS = 512; const int seed = 4711; int nodeStud[N_NODES]; std::vector<std::pair<int, int> > neighborNodes[N_NODES]; int nodeLen[N_NODES]; int nStudents; int nQueries; int nNodes; int myNode; int hashValue(int key){ return (N_BUCKETS - 1) & (key ^ (key >> 4)); } struct EndQuery { int end; int queryId; EndQuery(int end, int queryId):queryId(queryId), end(end){}; }; struct StudentNode { int stud; int node; StudentNode(int stud, int node):stud(stud), node(node){}; }; std::vector<EndQuery> queries[N_BUCKETS]; // multimap from end to id std::vector<StudentNode> students[N_BUCKETS]; //map from students to instances std::vector<EndQuery> localInfo; // multimap from id to index std::vector<int> queryEnds[N_QUERIES]; const int FIRST = -1; const int SECOND = 1; inline int step(int &stud1, int &stud2){ int next = FirstNeighbor(stud2); if(next == stud1) next = SecondNeighbor(stud2); stud1 = next; std::swap(stud1, stud2); } int owner(int stud){ for(auto stud2 : students[hashValue(stud)]) if(stud2.stud == stud) return stud2.node; return -1; } bool inspect(int stud, int index){ for(auto query : queries[hashValue(stud)]) if(query.end == stud){ //printf("myNode=%d info+=(%d, %d)\n", myNode, index, query.queryId); localInfo.emplace_back(index, query.queryId); } } std::pair<int, int> traverse(int stud, int direction){ int prevStud = stud; if(direction == FIRST) stud = FirstNeighbor(stud); else if(direction == SECOND) stud = SecondNeighbor(stud); int nextNode0 = -1; int index; for(index = direction; (nextNode0 = owner(stud)) == -1; index += direction){ // printf("stud=%d index=%d\n", stud, index); inspect(stud, index); step(prevStud, stud); } return std::make_pair(nextNode0, index); } int main(){ nNodes = NumberOfNodes(); myNode = MyNodeId(); nStudents = NumberOfStudents(); nQueries = NumberOfQueries(); if(nQueries == 0) return 0; for(int index = 1; index <= nQueries; ++index){ int from = QueryFrom(index); queries[hashValue(from)].emplace_back(from, index); int to = QueryTo(index); queries[hashValue(to)].emplace_back(to, index); } std::uniform_int_distribution<> distr(1, nStudents); std::mt19937 gen(seed); std::generate(nodeStud, nodeStud + nNodes, [&distr, &gen](){return distr(gen);}); std::sort(nodeStud, nodeStud + nNodes); nNodes = std::unique(nodeStud, nodeStud + nNodes) - nodeStud; if(nNodes == 2) nNodes--; for(int i = 0; i < nNodes; i++) students[hashValue(nodeStud[i])].emplace_back(nodeStud[i], i); if(nNodes <= myNode) return 0; inspect(nodeStud[myNode], 0); auto next1 = traverse(nodeStud[myNode], FIRST); /// < 0 auto next2 = traverse(nodeStud[myNode], SECOND); //// >0 //printf("%d has Interval %d %d\n", myNode, next1.second, next2.second); if(myNode){ PutInt(0, next1.first); PutInt(0, next1.second); PutInt(0, next2.first); PutInt(0, next2.second); Send(0); PutInt(0, localInfo.size()); for(auto info : localInfo){ // printf("sending query %d end %d\n", info.queryId, info.end); PutInt(0, info.queryId); PutInt(0, info.end); } Send(0); return 0; } neighborNodes[0].emplace_back(next1.first, next1.second); neighborNodes[0].emplace_back(next2.first, next2.second); for(int i = 1; i < nNodes; i++){ Receive(i); int node = GetInt(i); int diff = GetInt(i); neighborNodes[i].emplace_back(node, diff); node = GetInt(i); diff = GetInt(i); neighborNodes[i].emplace_back(node, diff); } for(auto info : localInfo){ queryEnds[info.queryId].push_back(info.end); // printf("End (%d %d)\n", info.queryId, info.end); } if(nNodes > 1){ int prevNode = 0; int node = neighborNodes[0][1].first; int offset = neighborNodes[0][1].second; //printf("%d %d\n", prevNode, node); for(; node != 0;){ int directionFlag = neighborNodes[node][0].first == prevNode ? 1 : -1; //printf("offset = %d df=%d\n", offset, directionFlag); //printf("receiving from %d\n", node); Receive(node); int infoLen = GetInt(node); //printf("expecting %d\n", infoLen); for(int i = 0; i < infoLen; i++){ int queryId = GetInt(node); int queryEnd = GetInt(node); //printf("got %d %d\n", queryId, queryEnd); //printf("EndQuery (%d %d)\n", queryId, offset + queryEnd * directionFlag); queryEnds[queryId].push_back(offset + queryEnd * directionFlag); } if(nNodes == 2) break; //printf("prev=%d\n", prevNode); for(auto neighbor : neighborNodes[node]){ //printf("jest sasiad %d\n", neighbor.first); if(neighbor.first != prevNode){ prevNode = neighbor.first; std::swap(node, prevNode); offset += abs(neighbor.second); break; } } } } for(int queryId = 1; queryId <= nQueries; queryId++){ //printf("queryId=%d size=%d\n", queryId, queryEnds[queryId].size()); int a = queryEnds[queryId][0]; int i = 1; //for(auto x : queryEnds[queryId]) printf("%d ", x); while(i < queryEnds[queryId].size() && queryEnds[queryId][i] == a) i++; //printf("%d\n", i); int b = (i == queryEnds[queryId].size()) ?a : queryEnds[queryId][i]; int dist0 = a - b; while(dist0 < 0) dist0 += nStudents; int dist1 = b - a; while(dist1 < 0) dist1 += nStudents; printf("%d\n", std::min(dist0 % nStudents, dist1 % nStudents)); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | #include <algorithm> #include <cstdio> #include <numeric> #include <random> #include <vector> #include "kollib.h" #include "message.h" typedef long long LL; const int N_NODES = 100; const int N_QUERIES = 201; const int N_BUCKETS = 512; const int seed = 4711; int nodeStud[N_NODES]; std::vector<std::pair<int, int> > neighborNodes[N_NODES]; int nodeLen[N_NODES]; int nStudents; int nQueries; int nNodes; int myNode; int hashValue(int key){ return (N_BUCKETS - 1) & (key ^ (key >> 4)); } struct EndQuery { int end; int queryId; EndQuery(int end, int queryId):queryId(queryId), end(end){}; }; struct StudentNode { int stud; int node; StudentNode(int stud, int node):stud(stud), node(node){}; }; std::vector<EndQuery> queries[N_BUCKETS]; // multimap from end to id std::vector<StudentNode> students[N_BUCKETS]; //map from students to instances std::vector<EndQuery> localInfo; // multimap from id to index std::vector<int> queryEnds[N_QUERIES]; const int FIRST = -1; const int SECOND = 1; inline int step(int &stud1, int &stud2){ int next = FirstNeighbor(stud2); if(next == stud1) next = SecondNeighbor(stud2); stud1 = next; std::swap(stud1, stud2); } int owner(int stud){ for(auto stud2 : students[hashValue(stud)]) if(stud2.stud == stud) return stud2.node; return -1; } bool inspect(int stud, int index){ for(auto query : queries[hashValue(stud)]) if(query.end == stud){ //printf("myNode=%d info+=(%d, %d)\n", myNode, index, query.queryId); localInfo.emplace_back(index, query.queryId); } } std::pair<int, int> traverse(int stud, int direction){ int prevStud = stud; if(direction == FIRST) stud = FirstNeighbor(stud); else if(direction == SECOND) stud = SecondNeighbor(stud); int nextNode0 = -1; int index; for(index = direction; (nextNode0 = owner(stud)) == -1; index += direction){ // printf("stud=%d index=%d\n", stud, index); inspect(stud, index); step(prevStud, stud); } return std::make_pair(nextNode0, index); } int main(){ nNodes = NumberOfNodes(); myNode = MyNodeId(); nStudents = NumberOfStudents(); nQueries = NumberOfQueries(); if(nQueries == 0) return 0; for(int index = 1; index <= nQueries; ++index){ int from = QueryFrom(index); queries[hashValue(from)].emplace_back(from, index); int to = QueryTo(index); queries[hashValue(to)].emplace_back(to, index); } std::uniform_int_distribution<> distr(1, nStudents); std::mt19937 gen(seed); std::generate(nodeStud, nodeStud + nNodes, [&distr, &gen](){return distr(gen);}); std::sort(nodeStud, nodeStud + nNodes); nNodes = std::unique(nodeStud, nodeStud + nNodes) - nodeStud; if(nNodes == 2) nNodes--; for(int i = 0; i < nNodes; i++) students[hashValue(nodeStud[i])].emplace_back(nodeStud[i], i); if(nNodes <= myNode) return 0; inspect(nodeStud[myNode], 0); auto next1 = traverse(nodeStud[myNode], FIRST); /// < 0 auto next2 = traverse(nodeStud[myNode], SECOND); //// >0 //printf("%d has Interval %d %d\n", myNode, next1.second, next2.second); if(myNode){ PutInt(0, next1.first); PutInt(0, next1.second); PutInt(0, next2.first); PutInt(0, next2.second); Send(0); PutInt(0, localInfo.size()); for(auto info : localInfo){ // printf("sending query %d end %d\n", info.queryId, info.end); PutInt(0, info.queryId); PutInt(0, info.end); } Send(0); return 0; } neighborNodes[0].emplace_back(next1.first, next1.second); neighborNodes[0].emplace_back(next2.first, next2.second); for(int i = 1; i < nNodes; i++){ Receive(i); int node = GetInt(i); int diff = GetInt(i); neighborNodes[i].emplace_back(node, diff); node = GetInt(i); diff = GetInt(i); neighborNodes[i].emplace_back(node, diff); } for(auto info : localInfo){ queryEnds[info.queryId].push_back(info.end); // printf("End (%d %d)\n", info.queryId, info.end); } if(nNodes > 1){ int prevNode = 0; int node = neighborNodes[0][1].first; int offset = neighborNodes[0][1].second; //printf("%d %d\n", prevNode, node); for(; node != 0;){ int directionFlag = neighborNodes[node][0].first == prevNode ? 1 : -1; //printf("offset = %d df=%d\n", offset, directionFlag); //printf("receiving from %d\n", node); Receive(node); int infoLen = GetInt(node); //printf("expecting %d\n", infoLen); for(int i = 0; i < infoLen; i++){ int queryId = GetInt(node); int queryEnd = GetInt(node); //printf("got %d %d\n", queryId, queryEnd); //printf("EndQuery (%d %d)\n", queryId, offset + queryEnd * directionFlag); queryEnds[queryId].push_back(offset + queryEnd * directionFlag); } if(nNodes == 2) break; //printf("prev=%d\n", prevNode); for(auto neighbor : neighborNodes[node]){ //printf("jest sasiad %d\n", neighbor.first); if(neighbor.first != prevNode){ prevNode = neighbor.first; std::swap(node, prevNode); offset += abs(neighbor.second); break; } } } } for(int queryId = 1; queryId <= nQueries; queryId++){ //printf("queryId=%d size=%d\n", queryId, queryEnds[queryId].size()); int a = queryEnds[queryId][0]; int i = 1; //for(auto x : queryEnds[queryId]) printf("%d ", x); while(i < queryEnds[queryId].size() && queryEnds[queryId][i] == a) i++; //printf("%d\n", i); int b = (i == queryEnds[queryId].size()) ?a : queryEnds[queryId][i]; int dist0 = a - b; while(dist0 < 0) dist0 += nStudents; int dist1 = b - a; while(dist1 < 0) dist1 += nStudents; printf("%d\n", std::min(dist0 % nStudents, dist1 % nStudents)); } } |