/************************************************************************* * * * Potyczki Algorytmiczne 2014 * * * * Zadanie: Kółko informatyczne [B] * * Autor: Jakub Sroka * * * *************************************************************************/ #include <cstdio> #include <vector> #include <algorithm> #include "message.h" #include "kollib.h" using namespace std; int binarySearch(vector< vector<int> > *students, int x, int begin, int end); bool mySort(vector<int> a, vector<int> b) { return a[0] < b[0]; } int main(int argc, const char * argv[]) { int n = NumberOfStudents(); vector<int> empty(2); if (MyNodeId() == 0) { vector< vector<int> > students; students.push_back(empty); students[0][0] = 1; //nr students[0][1] = 1; //pozycja students.push_back(empty); students[1][0] = FirstNeighbor(1); students[1][1] = 2; for (int i=2; i<n; i++) { students.push_back(empty); if (students[i-2][0] != FirstNeighbor(students[i-1][0])) { students[i][0] = FirstNeighbor(students[i-1][0]); students[i][1] = i+1; } else { students[i][0] = SecondNeighbor(students[i-1][0]); students[i][1] = i+1; } } sort(students.begin(), students.end(), mySort); int pos1 = 0, pos2 = 0, a, b, nr; for (int i=0; i<NumberOfQueries(); i++) { nr = i+1; a = QueryFrom(nr); b = QueryTo(nr); pos1 = binarySearch(&students, a, 0, students.size()); pos2 = binarySearch(&students, b, 0, students.size()); if (pos2>pos1) { if (pos2-pos1 <= n-pos2+pos1) { printf("%d\n", pos2-pos1); } else printf("%d\n", n-pos2+pos1); } else { if (pos1-pos2 <= n-pos1+pos2) { printf("%d\n", pos1-pos2); } else printf("%d\n", n-pos1+pos2); } } } // else if (MyNodeId() > 0 && n > 12000000) { // int a, b, pos1=0, pos2=0; // int old = 1, current = FirstNeighbor(1); // for (int i=MyNodeId(); i<=NumberOfQueries(); i+=(NumberOfNodes()-1)) { // pos1 = 0; // pos2 = 0; // old = 1, current = FirstNeighbor(1); // a = QueryFrom(i); // b = QueryTo(i); // if (a==1) pos1=1; // if (b==1) pos2=1; // if (a==current) pos1=2; // if (b==current) pos2=2; // int index = 2; // while (pos1 == 0 || pos2 == 0) { // if (FirstNeighbor(current) == old) { // old = current; // current = SecondNeighbor(current); // } // else { // old = current; // current = FirstNeighbor(current); // } // if (a==current) { // pos1 = index; // } // if (b==current) { // pos2 = index; // } // index++; // } // PutInt(0, i); // if (pos2>pos1) { // if (pos2-pos1 <= n-pos2+pos1) { // //printf("%d\n", pos2-pos1); // PutInt(0, pos2-pos1); // } // else PutInt(0, n-pos2+pos1); // } // else { // if (pos1-pos2 <= n-pos1+pos2) { // PutInt(0, pos1-pos2); // } // else PutInt(0, n-pos1+pos2); // } // Send(0); // } // // } // // else if (n > 12000000) { // vector<int> queries(NumberOfQueries()+1); // int nr, val; // for (int i=0; i<NumberOfQueries(); i++) { // Receive(-1); // nr = GetInt(-1); // queries[nr] = GetInt(-1); // } // for (int i=1; i<=NumberOfQueries(); i++) { // printf("%d\n", queries[i]); // } // } return 0; } int binarySearch(vector< vector<int> > *students, int x, int begin, int end) { int index = (begin+end)/2; if (students->at(index)[0] == x) { return students->at(index)[1]; } if (students->size() == 1) { return 0; } if (index>0 && students->at(index)[0] > x && students->at(index-1)[0] <x) { return 0; } if (index<students->size()-1 && students->at(index)[0] < x && students->at(index+1)[0] > x) { return 0; } if (students->at(index)[0] < x) { return binarySearch(students, x, index, end); } if (students->at(index)[0] > x) { return binarySearch(students, x, begin, index); } }
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 | /************************************************************************* * * * Potyczki Algorytmiczne 2014 * * * * Zadanie: Kółko informatyczne [B] * * Autor: Jakub Sroka * * * *************************************************************************/ #include <cstdio> #include <vector> #include <algorithm> #include "message.h" #include "kollib.h" using namespace std; int binarySearch(vector< vector<int> > *students, int x, int begin, int end); bool mySort(vector<int> a, vector<int> b) { return a[0] < b[0]; } int main(int argc, const char * argv[]) { int n = NumberOfStudents(); vector<int> empty(2); if (MyNodeId() == 0) { vector< vector<int> > students; students.push_back(empty); students[0][0] = 1; //nr students[0][1] = 1; //pozycja students.push_back(empty); students[1][0] = FirstNeighbor(1); students[1][1] = 2; for (int i=2; i<n; i++) { students.push_back(empty); if (students[i-2][0] != FirstNeighbor(students[i-1][0])) { students[i][0] = FirstNeighbor(students[i-1][0]); students[i][1] = i+1; } else { students[i][0] = SecondNeighbor(students[i-1][0]); students[i][1] = i+1; } } sort(students.begin(), students.end(), mySort); int pos1 = 0, pos2 = 0, a, b, nr; for (int i=0; i<NumberOfQueries(); i++) { nr = i+1; a = QueryFrom(nr); b = QueryTo(nr); pos1 = binarySearch(&students, a, 0, students.size()); pos2 = binarySearch(&students, b, 0, students.size()); if (pos2>pos1) { if (pos2-pos1 <= n-pos2+pos1) { printf("%d\n", pos2-pos1); } else printf("%d\n", n-pos2+pos1); } else { if (pos1-pos2 <= n-pos1+pos2) { printf("%d\n", pos1-pos2); } else printf("%d\n", n-pos1+pos2); } } } // else if (MyNodeId() > 0 && n > 12000000) { // int a, b, pos1=0, pos2=0; // int old = 1, current = FirstNeighbor(1); // for (int i=MyNodeId(); i<=NumberOfQueries(); i+=(NumberOfNodes()-1)) { // pos1 = 0; // pos2 = 0; // old = 1, current = FirstNeighbor(1); // a = QueryFrom(i); // b = QueryTo(i); // if (a==1) pos1=1; // if (b==1) pos2=1; // if (a==current) pos1=2; // if (b==current) pos2=2; // int index = 2; // while (pos1 == 0 || pos2 == 0) { // if (FirstNeighbor(current) == old) { // old = current; // current = SecondNeighbor(current); // } // else { // old = current; // current = FirstNeighbor(current); // } // if (a==current) { // pos1 = index; // } // if (b==current) { // pos2 = index; // } // index++; // } // PutInt(0, i); // if (pos2>pos1) { // if (pos2-pos1 <= n-pos2+pos1) { // //printf("%d\n", pos2-pos1); // PutInt(0, pos2-pos1); // } // else PutInt(0, n-pos2+pos1); // } // else { // if (pos1-pos2 <= n-pos1+pos2) { // PutInt(0, pos1-pos2); // } // else PutInt(0, n-pos1+pos2); // } // Send(0); // } // // } // // else if (n > 12000000) { // vector<int> queries(NumberOfQueries()+1); // int nr, val; // for (int i=0; i<NumberOfQueries(); i++) { // Receive(-1); // nr = GetInt(-1); // queries[nr] = GetInt(-1); // } // for (int i=1; i<=NumberOfQueries(); i++) { // printf("%d\n", queries[i]); // } // } return 0; } int binarySearch(vector< vector<int> > *students, int x, int begin, int end) { int index = (begin+end)/2; if (students->at(index)[0] == x) { return students->at(index)[1]; } if (students->size() == 1) { return 0; } if (index>0 && students->at(index)[0] > x && students->at(index-1)[0] <x) { return 0; } if (index<students->size()-1 && students->at(index)[0] < x && students->at(index+1)[0] > x) { return 0; } if (students->at(index)[0] < x) { return binarySearch(students, x, index, end); } if (students->at(index)[0] > x) { return binarySearch(students, x, begin, index); } } |