——by A Code Rabbit
Description
两个小女孩生日了,但是小气的妈妈只买一个蛋糕,所以你就悲剧了。
蛋糕上有2N个樱桃,你要把蛋糕一刀切成均等的两半,并且樱桃数也平均分。
蛋糕是圆形的,令蛋糕的中心为坐标轴的原点。
输入樱桃的位置。
输出 A,B 使得 Ax+By=0 为切下去那一刀的轨迹。
Types
Brute Force :: Elementary Skills
Analysis
这题的关键其实在于题目的数据范围[-500, 500],并且为整数。
因为直线方程必过(0, 0),所以直线必然可以通过蛋糕的圆心,把蛋糕均分。
因此不必考虑均分蛋糕的问题。
所以只要在定义域里枚举A、B的值,然后判断是否把樱桃平均分即可。
而判断樱桃与直线的关系的方法是:
把樱桃的坐标带入直线方程的左边,大于0则在直线上方,小于0则在直线下方。
Solution
// UVaOJ 10167
// Birthday Cake
// by A Code Rabbit
#include <cstdio>
const int LIMITS = 10000;
const int RANGE = 500;
int n;
struct Point {
int x, y;
};
Point cherry[LIMITS];
void Enumerate();
bool IsFair(int a, int b);
int main() {
while (scanf("%d", &n), n) {
// Inputs.
for (int i = 0; i < n * 2; ++i) {
scanf("%d%d", &cherry[i].x, &cherry[i].y);
}
// Brute force! And then, outputs.
Enumerate();
}
return 0;
}
void Enumerate() {
for (int i = -RANGE; i < RANGE; ++i) {
for (int j = -RANGE; j < RANGE; ++j) {
if (IsFair(i, j)) {
printf("%d %d\n", i, j);
return;
}
}
}
}
bool IsFair(int a, int b) {
int difference = 0;
bool cut_cherry = false;
for (int i = 0; i < n * 2; ++i) {
if (a * cherry[i].x + b * cherry[i].y > 0) {
++difference;
} else
if (a * cherry[i].x + b * cherry[i].y < 0) {
--difference;
} else {
// If you cut a cherry into half, two children wouldn't be happy.
return false;
}
}
if (difference) {
return false;
} else {
return true;
}
}
参考资料:无