#include <cstdio> #include "message.h" #include "kollib.h" #include <vector> #include <algorithm> #define LL long long int #define DEBUG using namespace std; struct S { S () : n(0), pos(0) {} S(int n) : n(n), pos(0) {} S(int n, int pos) : n(n), pos(pos) {} int n, pos; }; inline bool cmp(const S& a, const S& b) { return a.n < b.n; } int main() { int ID = MyNodeId(); int M = NumberOfNodes(); int N = NumberOfStudents(); int m = NumberOfQueries(); //if(N < 500) //{ if(ID == 0) { vector<S> v(N); v[0] = 1; v[1] = FirstNeighbor(1); for(int i = 2; i < N; ++i) { int q1 = FirstNeighbor(v[i-1].n), q2 = SecondNeighbor(v[i-1].n); v[i].n = (q1 == v[i-2].n) ? q2 : q1; v[i].pos = i; } for(int i = 1; i <= m; ++i) { int q1 = QueryFrom(i), q2 = QueryTo(i); if(q1 == q2) { printf("0\n"); continue; } int counter = 1; int j = 0; while(v[j].n != q1 && v[j].n != q2) ++j; ++j; while(v[j].n != q1 && v[j].n != q2) { ++j; ++counter; } printf("%d\n", min(counter, N-counter)); } } return 0; /*} if(ID == 0) { vector<S> v; for(int i = 0; i < 2*m; ++i) { int r = Receive(-1); v.push_back(S(GetInt(r), GetInt(r))); } sort(v.begin(), v.end(), cmp); for(int i = 1; i <= m; ++i) { int q1 = QueryFrom(i), q2 = QueryTo(i); auto q1a = lower_bound(v.begin(), v.end(), S(q1), cmp); auto q2a = lower_bound(v.begin(), v.end(), S(q2), cmp); printf("%d\n", min(abs((*q1a).pos - (*q2a).pos), N - abs((*q1a).pos - (*q2a).pos))); } } else { vector<S> v; if(ID == 1) { //pierwsza ma najwięcej v.resize(N/(M-1)+N%(M-1)); int f, s; v[0].n = 1; v[0].pos = 0; v[1].n = FirstNeighbor(1); v[1].pos = 1; for(int i = 2; i < v.size(); ++i) { v[i].pos = i; f = FirstNeighbor(v[i-1].n); s = SecondNeighbor(v[i-1].n); if(f == v[i-2].n) v[i] = s; else v[i] = f; } PutInt(ID+1, v[v.size()-2].n); PutInt(ID+1, v[v.size()-1].n); Send(ID+1); } else if(ID == M-1) { v.resize(N/(M-1)); int f, s, prev, curr; Receive(ID-1); prev = GetInt(ID-1); curr = GetInt(ID-1); int off = (((LL)(ID-1))*(LL)(N))/(M-1) + N%(M-1); for(int i = 0; i < v.size(); ++i) { v[i].pos = i+off; f = FirstNeighbor(curr); s = SecondNeighbor(curr); int tmp = curr; if(f == prev) curr = s; else curr = f; prev = tmp; v[i].n = curr; } } else { v.resize(N/(M-1)); int f, s, prev, curr; Receive(ID-1); prev = GetInt(ID-1); curr = GetInt(ID-1); int off = (((LL)(ID-1))*(LL)(N))/(M-1) + N%(M-1); for(int i = 0; i < v.size(); ++i) { v[i].pos = i+off; f = FirstNeighbor(curr); s = SecondNeighbor(curr); int tmp = curr; if(f == prev) curr = s; else curr = f; prev = tmp; v[i].n = curr; } PutInt(ID+1, v[v.size()-2].n); PutInt(ID+1, v[v.size()-1].n); Send(ID+1); } sort(v.begin(), v.end(), cmp); for(int i = 1; i <= m; ++i) { int q1 = QueryFrom(i), q2 = QueryTo(i); auto q1a = lower_bound(v.begin(), v.end(), S(q1), cmp); auto q2a = lower_bound(v.begin(), v.end(), S(q2), cmp); if(q1a != v.end() && (*q1a).n == q1) { PutInt(0, (*q1a).n); PutInt(0, (*q1a).pos); Send(0); } if(q2a != v.end() && (*q2a).n == q2) { PutInt(0, (*q2a).n); PutInt(0, (*q2a).pos); Send(0); } } } return 0;*/ }
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 | #include <cstdio> #include "message.h" #include "kollib.h" #include <vector> #include <algorithm> #define LL long long int #define DEBUG using namespace std; struct S { S () : n(0), pos(0) {} S(int n) : n(n), pos(0) {} S(int n, int pos) : n(n), pos(pos) {} int n, pos; }; inline bool cmp(const S& a, const S& b) { return a.n < b.n; } int main() { int ID = MyNodeId(); int M = NumberOfNodes(); int N = NumberOfStudents(); int m = NumberOfQueries(); //if(N < 500) //{ if(ID == 0) { vector<S> v(N); v[0] = 1; v[1] = FirstNeighbor(1); for(int i = 2; i < N; ++i) { int q1 = FirstNeighbor(v[i-1].n), q2 = SecondNeighbor(v[i-1].n); v[i].n = (q1 == v[i-2].n) ? q2 : q1; v[i].pos = i; } for(int i = 1; i <= m; ++i) { int q1 = QueryFrom(i), q2 = QueryTo(i); if(q1 == q2) { printf("0\n"); continue; } int counter = 1; int j = 0; while(v[j].n != q1 && v[j].n != q2) ++j; ++j; while(v[j].n != q1 && v[j].n != q2) { ++j; ++counter; } printf("%d\n", min(counter, N-counter)); } } return 0; /*} if(ID == 0) { vector<S> v; for(int i = 0; i < 2*m; ++i) { int r = Receive(-1); v.push_back(S(GetInt(r), GetInt(r))); } sort(v.begin(), v.end(), cmp); for(int i = 1; i <= m; ++i) { int q1 = QueryFrom(i), q2 = QueryTo(i); auto q1a = lower_bound(v.begin(), v.end(), S(q1), cmp); auto q2a = lower_bound(v.begin(), v.end(), S(q2), cmp); printf("%d\n", min(abs((*q1a).pos - (*q2a).pos), N - abs((*q1a).pos - (*q2a).pos))); } } else { vector<S> v; if(ID == 1) { //pierwsza ma najwięcej v.resize(N/(M-1)+N%(M-1)); int f, s; v[0].n = 1; v[0].pos = 0; v[1].n = FirstNeighbor(1); v[1].pos = 1; for(int i = 2; i < v.size(); ++i) { v[i].pos = i; f = FirstNeighbor(v[i-1].n); s = SecondNeighbor(v[i-1].n); if(f == v[i-2].n) v[i] = s; else v[i] = f; } PutInt(ID+1, v[v.size()-2].n); PutInt(ID+1, v[v.size()-1].n); Send(ID+1); } else if(ID == M-1) { v.resize(N/(M-1)); int f, s, prev, curr; Receive(ID-1); prev = GetInt(ID-1); curr = GetInt(ID-1); int off = (((LL)(ID-1))*(LL)(N))/(M-1) + N%(M-1); for(int i = 0; i < v.size(); ++i) { v[i].pos = i+off; f = FirstNeighbor(curr); s = SecondNeighbor(curr); int tmp = curr; if(f == prev) curr = s; else curr = f; prev = tmp; v[i].n = curr; } } else { v.resize(N/(M-1)); int f, s, prev, curr; Receive(ID-1); prev = GetInt(ID-1); curr = GetInt(ID-1); int off = (((LL)(ID-1))*(LL)(N))/(M-1) + N%(M-1); for(int i = 0; i < v.size(); ++i) { v[i].pos = i+off; f = FirstNeighbor(curr); s = SecondNeighbor(curr); int tmp = curr; if(f == prev) curr = s; else curr = f; prev = tmp; v[i].n = curr; } PutInt(ID+1, v[v.size()-2].n); PutInt(ID+1, v[v.size()-1].n); Send(ID+1); } sort(v.begin(), v.end(), cmp); for(int i = 1; i <= m; ++i) { int q1 = QueryFrom(i), q2 = QueryTo(i); auto q1a = lower_bound(v.begin(), v.end(), S(q1), cmp); auto q2a = lower_bound(v.begin(), v.end(), S(q2), cmp); if(q1a != v.end() && (*q1a).n == q1) { PutInt(0, (*q1a).n); PutInt(0, (*q1a).pos); Send(0); } if(q2a != v.end() && (*q2a).n == q2) { PutInt(0, (*q2a).n); PutInt(0, (*q2a).pos); Send(0); } } } return 0;*/ } |