문제 설명
데카르트 평면에서 N개의 점이 주어집니다. 우리의 임무는 축의 한 면에 남아 있는 점을 얻기 위해 제거해야 하는 최소 점 수를 찾는 것입니다.
주어진 입력이 {(10, 5), (-2, -5), (13, 8), (-14, 7)}이면 (-2, -5)를 제거하면 나머지 모든 점이 X 위에 있습니다. -축.
따라서 답은 1입니다.
알고리즘
1. Finds the number of points on all sides of the X-axis and Y-axis 2. Return minimum from both of them
예
#include <iostream>
#include <algorithm>
#define SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
using namespace std;
struct point{
int x, y;
};
int minPointsToBeRemoved(point arr[], int n){
int a = 0, b = 0, c = 0, d = 0;
for (int i = 0; i < n; i++){
if (arr[i].x >= 0)
b++;
else if (arr[i].x <= 0)
a++;
if (arr[i].y <= 0)
d++;
else if (arr[i].y >= 0)
c++;
}
return min({a, d, c, b});
}
int main(){
point arr[] = {{10, 5}, {-2, -5}, {13, 8}, {-14, 7}};
cout << "Minimum points to be removed = " <<
minPointsToBeRemoved(arr, SIZE(arr)) << endl;
return 0;
} 출력
위의 프로그램을 컴파일하고 실행할 때. 다음 출력을 생성합니다 -
Minimum points to be removed = 1