• Source
    1. #include <bits/stdc++.h>
    2. using namespace std;
    3.  
    4. #define pr pair<int,int>
    5. #define f first
    6. #define s second
    7.  
    8. bool compare(pr a, pr b){
    9. return a.s<b.s;
    10. }
    11.  
    12. int solver(vector<int> x ,vector<int> y, int k){
    13. int n = x.size();
    14. int minArea = INT_MAX;
    15. // generate points
    16. vector<pr> xsort, ysort;
    17.  
    18. for (int i = 0; i < n; ++i) {
    19. xsort.push_back({x[i],y[i]});
    20. ysort.push_back({x[i],y[i]});
    21. }
    22.  
    23. // sort points by X
    24. sort(xsort.begin(), xsort.end());
    25.  
    26. // sort points by Y
    27. sort(ysort.begin(), ysort.end(), compare);
    28.  
    29. for (int i = 0; i < n - k + 1; ++i) {
    30. for (int j = 0; j < n - k + 1; ++j) {
    31. pr lp = xsort[i];
    32. pr bp = ysort[j];
    33.  
    34. if (lp.f > bp.f || lp.s < bp.s) {
    35. continue;
    36. }
    37.  
    38. int leftEdge = lp.f - 1;
    39. int bottomEdge = bp.s - 1;
    40.  
    41. set<pr> tempPoints, acceptablePoints;
    42. for(int m =i; m<n; m++)
    43. tempPoints.insert(xsort[m]);
    44.  
    45. for(int m=j; m<n; m++){
    46. if(tempPoints.count(ysort[m])){
    47. acceptablePoints.insert(ysort[m]);
    48. }
    49. }
    50.  
    51. if (acceptablePoints.size() < k) {
    52. continue; // not enough points
    53. }
    54.  
    55. // calculate and sort areas
    56. vector<int> areas;
    57.  
    58. for (auto point : acceptablePoints) {
    59. int sqside = max(point.f + 1 - leftEdge, point.s + 1 - bottomEdge);
    60. areas.push_back(sqside * sqside);
    61. }
    62.  
    63. sort(areas.begin(), areas.end());
    64.  
    65. int currentArea = areas[k - 1];
    66. if (minArea > currentArea) {
    67. minArea = currentArea;
    68. }
    69. }
    70. }
    71. return minArea ;
    72. }
    73.  
    74. int main() {
    75. int t; cin>>t;
    76. while(t--){
    77. int N;
    78. int K;
    79. cin>>N>>K;
    80. vector<int> x(N), y(N);
    81.  
    82. for (int i = 0; i < N; ++i) {
    83. cin>>x[i]>>y[i];
    84. }
    85.  
    86. cout<<solver(x,y,K)<<endl;
    87. }
    88. return 0;
    89. }