Last active
December 29, 2015 08:18
-
-
Save xswang/7641901 to your computer and use it in GitHub Desktop.
UVAoj 10167: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1108 这个是最简单的brute force,不过提交时也又是WA又是TLE的折磨了我好多次。 要判断线两边的cherry个数是否相同,只需要判断:将cherry的坐标带入直线方程后,ax+by>0的个数与ax+by<0的个数是否相同即可。 当时WA的地方在:flag的位置没放对,对每一次的枚举方程来说,都要将两边个数(分别用pos,neg表示)以及找到解的标志重新初始化。 TLE的地方:没有先把每次2n个数据放到内存中,而是读一个计算一个。可能是在IO时花费了不少时间。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int main(){ | |
int n; | |
while(scanf("%d",&n)){ | |
if(n == 0)break; | |
int pos, neg, x, y, tag; | |
vector<int> veca,vecb; | |
veca.clear();vecb.clear(); | |
for(int i = 0; i < 2*n; i++){//这里要先把数据放到内存,不然,竟然会超时... | |
scanf("%d%d",&x,&y); | |
veca.push_back(x); | |
vecb.push_back(y); | |
} | |
for(int a = -500; a <= 500; a++){ | |
for(int b = -500; b <= 500; b++){ | |
pos = 0, neg = 0; tag = 1;//对每一个方程(取定a,b之后就确定了一个方程式),初始化左右两边个数都为0,且找到解的标志为1. | |
for(int i = 0; i < 2*n; i++){//连续输入2n对数据,樱桃的坐标。 | |
//scanf("%d %d",&x,&y);//这是我之前想读一个判断一个的,结果超时了。 | |
if(veca[i]*a +vecb[i]*b == 0)break;//如果cherry在分割线上,还包括了a,b同时为0的情况。均不成立。 | |
else if(veca[i]*a + vecb[i]*b > 0) pos++;//如果樱桃在其中某一侧。其个数加1. | |
else if(veca[i]*a + vecb[i]*b < 0) neg++;//如果樱桃在另一侧,其个数加1 | |
} | |
if(pos == neg && pos + neg == 2*n){//如果樱桃不在分割线上,且线两侧个数相同。而且,总和要等于2n,因为当某个时刻veca[i]*a +vecb[i]*b == 0时,pos和neg也可能相同 | |
printf("%d %d\n",a,b);//输出结果 | |
tag = 0;//置标志位为0.表示已经找到一个解 | |
break; | |
} | |
} | |
if(!tag) break;//如果前面的循环中找到了解,就跳出。继续读入下一堆数。 | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment