Advertisement
Andre1314

Untitled

Dec 18th, 2023
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <math.h>
  5.  
  6. #define ll long long
  7. #define pll pair<ll, ll>
  8.  
  9. using namespace std;
  10.  
  11. struct Params {
  12.     ll width;
  13.     ll height;
  14.     ll classIndex;
  15. };
  16.  
  17. ll n, k, m;
  18. ll w, h;
  19. vector<Params> classes;
  20.  
  21. ll len(ll width1, ll height1, ll width2, ll height2) {
  22.     return max(abs(width1 - width2), abs(height1 - height2));
  23. }
  24.  
  25. vector<pll> distance(ll width, ll height) {
  26.     vector<pll> result;
  27.     for (auto cls : classes) {
  28.         ll d = len(cls.width, cls.height, width, height);
  29.         result.emplace_back(d, cls.classIndex);
  30.     }
  31.  
  32.     sort(result.begin(), result.end());
  33.     return result;
  34. }
  35.  
  36. ll classif(vector<pll> dist, ll n) {
  37.     if (k > dist.size()) k = dist.size();
  38.    
  39.     vector<pll> nearest(dist.begin(), dist.begin() + k);
  40.     ll counter = 0;
  41.  
  42.     while (k + counter < dist.size() && nearest.back().first == dist[k + counter].first) {
  43.         nearest.push_back(dist[k + counter]);
  44.         counter++;
  45.     }
  46.  
  47.     vector<ll> counts(n + counter, 0);
  48.     for (auto neighbor : nearest) {
  49.         if (neighbor.second < counts.size()) {
  50.             counts[neighbor.second]++;
  51.         }
  52.     }
  53.  
  54.     return max_element(counts.begin(), counts.end()) - counts.begin();
  55. }
  56.  
  57. int main() {
  58.     cin >> n >> k >> m;
  59.  
  60.     for (ll i = 0; i < n; i++) {
  61.         cin >> w >> h;
  62.         classes.push_back({ w, h, i });
  63.     }
  64.  
  65.     vector<ll> nums;
  66.     for (ll i = 0; i < m; i++) {
  67.         cin >> w >> h;
  68.  
  69.         ll ind = classif(distance(w, h), n + i);
  70.  
  71.         nums.push_back(ind + 1);
  72.  
  73.         classes.push_back({ w, h, ind });
  74.     }
  75.  
  76.     for (ll i = 0; i < nums.size(); i++) {
  77.         cout << nums[i];
  78.         if (i < nums.size() - 1) cout << " ";
  79.     }
  80.  
  81.     return 0;
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement