#include <iostream> #include <vector> #include <cmath> using namespace std; struct Coords { int32_t row; int32_t column; Coords() { } Coords(int32_t x, int32_t y) : row(x), column(y) { } bool operator==(const Coords &rhs) { if(this->row == rhs.row && this->column == rhs.column) { return true; } else { return false; } } bool operator!=(const Coords &rhs) { return !((*this) == rhs); } Coords operator+(const Coords &rhs) { Coords newxy; newxy.row = this->row + rhs.row; newxy.column = this->column + rhs.column; return newxy; } }; uint32_t findIndexOfMinimum(std::vector<float> vec) { uint32_t indexMinimum = 0; float minValue = vec[0]; for(uint32_t i = 1; i < vec.size(); ++i) { if(vec[i] < minValue) { indexMinimum = i; minValue = vec[i]; } } return indexMinimum; } float euclideanDistance(Coords from, Coords to) { return sqrt((from.row - to.row) * (from.row - to.row) + (from.column - to.column) * (from.column - to.column)); } Coords findCoordswithMinimumDistance(Coords position, Coords targetPosition, int16_t deltaRow, int16_t deltaColumn) { std::vector<float> distances; std::vector<Coords> coordinates; coordinates.push_back(position + Coords(0, deltaColumn)); coordinates.push_back(position + Coords(deltaRow, deltaColumn)); coordinates.push_back(position + Coords(deltaRow, 0)); distances.push_back(euclideanDistance(coordinates[0], targetPosition)); distances.push_back(euclideanDistance(coordinates[1], targetPosition)); distances.push_back(euclideanDistance(coordinates[2], targetPosition)); return coordinates[findIndexOfMinimum(distances)]; } uint64_t calculateResult(std::vector<Coords> finalPositions) { uint64_t result = 0; for(auto it = finalPositions.cbegin(); it != finalPositions.cend(); ++it) { result += (*it).row * (*it).column; } return result; } void roar(std::vector<Coords> *bearsPositions, uint32_t roaringBearIndex, uint32_t kamilIndex) { if(roaringBearIndex != kamilIndex) { Coords targetCoords = (*bearsPositions)[roaringBearIndex]; for(uint32_t i = 0; i < bearsPositions->size(); ++i) { if(i != roaringBearIndex && i != kamilIndex && (*bearsPositions)[roaringBearIndex] != (*bearsPositions)[i]) { int32_t rowDiff = targetCoords.row - (*bearsPositions)[i].row; int32_t columnDiff = targetCoords.column - (*bearsPositions)[i].column; if(rowDiff != 0 || columnDiff != 0) { if(rowDiff >= 0 && columnDiff >= 0) { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, 1, 1); } else if(rowDiff < 0 && columnDiff >= 0) { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, -1, 1); } else if(rowDiff >= 0 && columnDiff < 0) { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, 1, -1); } else { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, -1, -1); } } } } } } void roarLoop(std::vector<Coords> bearsPositions, uint32_t kamilIndex) { for(uint32_t i = 0; i < bearsPositions.size(); ++i) { roar(&bearsPositions, i, kamilIndex); } cout << calculateResult(bearsPositions) << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); uint32_t bearsQuantity; cin >> bearsQuantity; std::vector<Coords> bearsPositions(bearsQuantity); for(uint32_t i = 0; i < bearsQuantity; ++i) { uint32_t x, y; cin >> x >> y; bearsPositions[i].row = y; bearsPositions[i].column = x; } for(uint32_t kamil = 0; kamil < bearsQuantity; ++kamil) { //send copied vector roarLoop(bearsPositions, kamil); } 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 | #include <iostream> #include <vector> #include <cmath> using namespace std; struct Coords { int32_t row; int32_t column; Coords() { } Coords(int32_t x, int32_t y) : row(x), column(y) { } bool operator==(const Coords &rhs) { if(this->row == rhs.row && this->column == rhs.column) { return true; } else { return false; } } bool operator!=(const Coords &rhs) { return !((*this) == rhs); } Coords operator+(const Coords &rhs) { Coords newxy; newxy.row = this->row + rhs.row; newxy.column = this->column + rhs.column; return newxy; } }; uint32_t findIndexOfMinimum(std::vector<float> vec) { uint32_t indexMinimum = 0; float minValue = vec[0]; for(uint32_t i = 1; i < vec.size(); ++i) { if(vec[i] < minValue) { indexMinimum = i; minValue = vec[i]; } } return indexMinimum; } float euclideanDistance(Coords from, Coords to) { return sqrt((from.row - to.row) * (from.row - to.row) + (from.column - to.column) * (from.column - to.column)); } Coords findCoordswithMinimumDistance(Coords position, Coords targetPosition, int16_t deltaRow, int16_t deltaColumn) { std::vector<float> distances; std::vector<Coords> coordinates; coordinates.push_back(position + Coords(0, deltaColumn)); coordinates.push_back(position + Coords(deltaRow, deltaColumn)); coordinates.push_back(position + Coords(deltaRow, 0)); distances.push_back(euclideanDistance(coordinates[0], targetPosition)); distances.push_back(euclideanDistance(coordinates[1], targetPosition)); distances.push_back(euclideanDistance(coordinates[2], targetPosition)); return coordinates[findIndexOfMinimum(distances)]; } uint64_t calculateResult(std::vector<Coords> finalPositions) { uint64_t result = 0; for(auto it = finalPositions.cbegin(); it != finalPositions.cend(); ++it) { result += (*it).row * (*it).column; } return result; } void roar(std::vector<Coords> *bearsPositions, uint32_t roaringBearIndex, uint32_t kamilIndex) { if(roaringBearIndex != kamilIndex) { Coords targetCoords = (*bearsPositions)[roaringBearIndex]; for(uint32_t i = 0; i < bearsPositions->size(); ++i) { if(i != roaringBearIndex && i != kamilIndex && (*bearsPositions)[roaringBearIndex] != (*bearsPositions)[i]) { int32_t rowDiff = targetCoords.row - (*bearsPositions)[i].row; int32_t columnDiff = targetCoords.column - (*bearsPositions)[i].column; if(rowDiff != 0 || columnDiff != 0) { if(rowDiff >= 0 && columnDiff >= 0) { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, 1, 1); } else if(rowDiff < 0 && columnDiff >= 0) { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, -1, 1); } else if(rowDiff >= 0 && columnDiff < 0) { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, 1, -1); } else { (*bearsPositions)[i] = findCoordswithMinimumDistance((*bearsPositions)[i], targetCoords, -1, -1); } } } } } } void roarLoop(std::vector<Coords> bearsPositions, uint32_t kamilIndex) { for(uint32_t i = 0; i < bearsPositions.size(); ++i) { roar(&bearsPositions, i, kamilIndex); } cout << calculateResult(bearsPositions) << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); uint32_t bearsQuantity; cin >> bearsQuantity; std::vector<Coords> bearsPositions(bearsQuantity); for(uint32_t i = 0; i < bearsQuantity; ++i) { uint32_t x, y; cin >> x >> y; bearsPositions[i].row = y; bearsPositions[i].column = x; } for(uint32_t kamil = 0; kamil < bearsQuantity; ++kamil) { //send copied vector roarLoop(bearsPositions, kamil); } return 0; } |