Advertisement
1WaKa_WaKa1

Task_E

Mar 13th, 2022
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <climits>
  4.  
  5. using namespace std;
  6. int main() {
  7.     int cowsAmount;
  8.     int stallsAmount;
  9.     vector<int> coordinates;
  10.     //Ввод данных
  11.     cin >> stallsAmount;
  12.     cin >> cowsAmount;
  13.     int k = 0;
  14.     while (k != stallsAmount){
  15.         int temp;
  16.         cin >> temp;
  17.         coordinates.push_back(temp);
  18.         k++;
  19.     }
  20.  
  21.     //Тело программы
  22.     int l1 = 0;
  23.     int r1 = stallsAmount-1;
  24.     cowsAmount-=2;
  25.     int minDist = INT_MAX;
  26.     while (cowsAmount){
  27.         int m = (coordinates[l1]+coordinates[r1])/2;
  28.         int tempInd;
  29.         int tempMin = coordinates[r1];
  30.         for (int i = l1; i < r1/2+2; i++){
  31.             if (abs(m-coordinates[i]) < tempMin){
  32.                 tempMin = abs(m-coordinates[i]);
  33.                 tempInd = i;
  34.             }
  35.         }
  36.         if (abs(coordinates[l1] - coordinates[tempInd]) < abs(coordinates[tempInd] - coordinates[r1])){
  37.             r1 = tempInd;
  38.         } else l1 = tempInd;
  39.         cowsAmount--;
  40.     }
  41.     minDist = coordinates[r1] - coordinates[l1];
  42.     cout << minDist;
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement