#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> #include "kollib.h" #include "message.h" using namespace std; typedef vector<int> VI; typedef long long LL; typedef pair<int, int> PII; #define FOR(x, b, e) for (int x = b; x <= (e); ++x) #define FORD(x, b, e) for (int x = b; x >= (e); --x) #define REP(x, n) for (int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int) (x).size()) #define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define INF 1000000001 using namespace std; vector<PII> V; int K, N, n; // K - uczniowie, N - węzły, n - id węzła int kp, Q; void CollectStudents() { int p, c; int dir = (N > 1 && n >= N / 2 ? -1 : 1); //fprintf(stderr, "CollectStudents node #%d dir = %d\n", n, dir); if (n == 0) { p = FirstNeighbor(1); c = 1; if (N > 1) { PutInt(N - 1, c); PutInt(N - 1, p); Send(N - 1); } } else { int r_id = (n - dir) % N; //fprintf(stderr, "CollectStudents node #%d waiting for msg from #%d\n", n, r_id); Receive(r_id); p = GetInt(r_id); c = GetInt(r_id); //fprintf(stderr, "CollectStudents node #%d p = %d c = %d\n", n, p, c); } int beg = n * kp; int end = min((n + 1) * kp, K) - 1; //fprintf(stderr, "CollectStudents resize vec to %d\n", end - beg + 1); if (beg <= end) { V.resize(end - beg + 1); if (dir == -1) swap(beg, end); for (int i = beg; i != end + dir; i += dir) { //fprintf(stderr, "CollectStudents i = %d p = %d c = %d\n", i, p, c); V[i - n * kp] = MP(c, i); int nx = SecondNeighbor(c); if (nx == p) nx = FirstNeighbor(c); p = c; c = nx; } } int s_id = (n + dir) % N; if (dir == 1 && s_id < N / 2 || dir == -1 && s_id >= N / 2) { //fprintf(stderr, "CollectStudents node #%d sending msg to #%d\n", n, s_id); PutInt(s_id, p); PutInt(s_id, c); Send(s_id); } sort(ALL(V)); //REP(i, SIZE(V)) // fprintf(stderr, "CollectStudents i = %d: (%d, %d)\n", i, V[i].ST, V[i].ND); } void ProcessQueries() { REP(i, Q) { int q[2] = { QueryFrom(i + 1), QueryTo(i + 1) }; REP(k, 2) { auto it = lower_bound(ALL(V), MP(q[k], -1)); if (it != V.end() && it->ST == q[k]) { PutInt(0, i); PutInt(0, k); PutInt(0, it->ND); Send(0); } } } } void GatherResults() { VI r[2]; r[0].resize(Q, -1); r[1].resize(Q, -1); REP(i, 2 * Q) { int s = Receive(-1); int q = GetInt(s); int k = GetInt(s); int t = GetInt(s); r[k][q] = t; } REP(i, Q) { //fprintf(stderr, "Query #%d: %d %d\n", i, r[0][i], r[1][i]); int d = abs(r[0][i] - r[1][i]); printf("%d\n", min(d, K - d)); } } int main() { K = NumberOfStudents(); N = NumberOfNodes(); n = MyNodeId(); Q = NumberOfQueries(); kp = (K + N - 1) / N; CollectStudents(); ProcessQueries(); if (n == 0) GatherResults(); 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 | #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> #include "kollib.h" #include "message.h" using namespace std; typedef vector<int> VI; typedef long long LL; typedef pair<int, int> PII; #define FOR(x, b, e) for (int x = b; x <= (e); ++x) #define FORD(x, b, e) for (int x = b; x >= (e); --x) #define REP(x, n) for (int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int) (x).size()) #define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define INF 1000000001 using namespace std; vector<PII> V; int K, N, n; // K - uczniowie, N - węzły, n - id węzła int kp, Q; void CollectStudents() { int p, c; int dir = (N > 1 && n >= N / 2 ? -1 : 1); //fprintf(stderr, "CollectStudents node #%d dir = %d\n", n, dir); if (n == 0) { p = FirstNeighbor(1); c = 1; if (N > 1) { PutInt(N - 1, c); PutInt(N - 1, p); Send(N - 1); } } else { int r_id = (n - dir) % N; //fprintf(stderr, "CollectStudents node #%d waiting for msg from #%d\n", n, r_id); Receive(r_id); p = GetInt(r_id); c = GetInt(r_id); //fprintf(stderr, "CollectStudents node #%d p = %d c = %d\n", n, p, c); } int beg = n * kp; int end = min((n + 1) * kp, K) - 1; //fprintf(stderr, "CollectStudents resize vec to %d\n", end - beg + 1); if (beg <= end) { V.resize(end - beg + 1); if (dir == -1) swap(beg, end); for (int i = beg; i != end + dir; i += dir) { //fprintf(stderr, "CollectStudents i = %d p = %d c = %d\n", i, p, c); V[i - n * kp] = MP(c, i); int nx = SecondNeighbor(c); if (nx == p) nx = FirstNeighbor(c); p = c; c = nx; } } int s_id = (n + dir) % N; if (dir == 1 && s_id < N / 2 || dir == -1 && s_id >= N / 2) { //fprintf(stderr, "CollectStudents node #%d sending msg to #%d\n", n, s_id); PutInt(s_id, p); PutInt(s_id, c); Send(s_id); } sort(ALL(V)); //REP(i, SIZE(V)) // fprintf(stderr, "CollectStudents i = %d: (%d, %d)\n", i, V[i].ST, V[i].ND); } void ProcessQueries() { REP(i, Q) { int q[2] = { QueryFrom(i + 1), QueryTo(i + 1) }; REP(k, 2) { auto it = lower_bound(ALL(V), MP(q[k], -1)); if (it != V.end() && it->ST == q[k]) { PutInt(0, i); PutInt(0, k); PutInt(0, it->ND); Send(0); } } } } void GatherResults() { VI r[2]; r[0].resize(Q, -1); r[1].resize(Q, -1); REP(i, 2 * Q) { int s = Receive(-1); int q = GetInt(s); int k = GetInt(s); int t = GetInt(s); r[k][q] = t; } REP(i, Q) { //fprintf(stderr, "Query #%d: %d %d\n", i, r[0][i], r[1][i]); int d = abs(r[0][i] - r[1][i]); printf("%d\n", min(d, K - d)); } } int main() { K = NumberOfStudents(); N = NumberOfNodes(); n = MyNodeId(); Q = NumberOfQueries(); kp = (K + N - 1) / N; CollectStudents(); ProcessQueries(); if (n == 0) GatherResults(); return 0; } |