Problem G. Birthday Cake
| Problem G. Birthday Cake |
Background
Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100.

There are 2N (N is a integer, 1<=N<=50) cherries on the cake. Mother wants to cut the cake into two halves with a knife (of course a beeline). The twins would like to be treated fairly, that means, the shape of the two halves must be the same (that means the beeline must go through the center of the cake) , and each half must have N cherrie(s). Can you help her?
Note: the coordinate of a cherry (x , y) are two integers. You must give the line as form two integers A,B(stands for Ax+By=0), each number in the range [-500,500]. Cherries are not allowed lying on the beeline. For each dataset there is at least one solution.
Input
The input file contains several scenarios. Each of them consists of 2 parts: The first part consists of a line with a number N, the second part consists of 2N lines, each line has two number, meaning (x,y) .There is only one space between two border numbers. The input file is ended with N=0.Output
For each scenario, print a line containing two numbers A and B. There should be a space between them. If there are many solutions, you can only print one of them.Sample Input
2 -20 20 -30 20 -10 -50 10 -5 0
Sample Output
0 1
题意:给你一2*n个点,让你找出一条直线AX+BY=0,使直线把这个蛋糕分为两个部分,每一边都有n个点。
由于A,B的范围在-500~500之间,所以直接枚举就可以了。
#include <stdio.h>
const int N = 110;
struct Node{
int x;
int y;
}node[N];
void solve(int n) {
int less,more;
for(int i = -500; i <= 500; i++) {
for(int j = -500; j <= 500; j++) {
less = more = 0;
for(int k = 0; k < 2*n; k++) {
if( i*node[k].x + j*node[k].y < 0) {
less++;
}
if( i*node[k].x + j*node[k].y > 0) {
more++;
}
if( i*node[k].x + j*node[k].y == 0) {
break;
}
if( less == n && more == n) {
printf("%d %d\n",i,j);
return;
}
}
}
}
}
int main() {
int n;
while( scanf("%d",&n) != EOF && n) {
for(int i = 0; i < 2*n; i++) {
scanf("%d%d",&node[i].x,&node[i].y);
}
solve( n );
}
return 0;
}
本文介绍了一个有趣的算法问题——如何通过一条直线将蛋糕上的2N颗樱桃等分为两组,每组N颗,且直线不能经过任何樱桃。文章提供了一种通过枚举直线系数来解决问题的方法,并附带了完整的C语言实现代码。
1875

被折叠的 条评论
为什么被折叠?



