在CCW的基础上需要优化
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Main{
static Point1912[] data;
static Point1912 baseP;//起点,也就是最左下侧的点,可以根据坐标的最小值在数据输入的时候找出来
static int MAX=2087654321;//为了求起点用的
static int N;
public static void main(String[] args) throws Exception{
//System.setIn(new FileInputStream(new File("C:\\Users\\XAGDC\\Desktop\\dwarfs-i&o\\dwarfs\\dwarfs.in8")));
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(reader.readLine());
N=Integer.parseInt(st.nextToken());//房屋的个数
data=new Point1912[N];
double startx=MAX;
double starty=MAX;
int index=0;
for (int i = 0; i <N ; i++) {//接收一下房屋的坐标,有负数和小数
st=new StringTokenizer(reader.readLine());
double x=Double.parseDouble(st.nextToken());
double y=Double.parseDouble(st.nextToken());
data[i]=new Point1912(x,y);
if(y<starty){//按照y最小来进行
starty=y;
startx=x;
index=i;
}else if(y==starty){
if(x<startx){
startx=x;
starty=y;
index=i;
}
}
}
baseP=new Point1912(startx,starty);//这是最左下侧的点,是一个极点
data[index]=data[0];//将最小的点和当前0位置的点交换下,这样就不用排序了
data[0]=baseP;
Arrays.sort(data,1,N,new Comparator<Point1912>(){//从baseP开始,将点按照夹角排序
@Override
public int compare(Point1912 o1, Point1912 o2) {
return CCW(baseP,o1,o2) >0 ? -1:1;
}
});
//点在左侧(CCW<0),V++, 点在右侧,需要回溯V--
int V=1;
Point1912[] res=new Point1912[N+2];
res[0]=data[N-1];
res[1]=baseP;
for (int i = 2; i <N ; i++) {
while(CCW(res[V-1] ,res[V] ,data[i]) <0){
V--;
}
res[++V]=data[i];
}
res[++V]=baseP;
//while( (String str=reader.readLine()).length()>0){
while( (st=new StringTokenizer(reader.readLine())).hasMoreTokens()){
Point1912 startP=new Point1912(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()));
Point1912 endP=new Point1912(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()));
checkCross(res,V,startP,endP);
}
reader.close();
}
public static void checkCross(Point1912[] res,int V,Point1912 startP,Point1912 endP){
int tmp=CCW(startP,endP,res[0]);
boolean cross=false;
for (int i = 1; i <=V ; i++) {
int tmp2=CCW(startP,endP,res[i]);
if(tmp * tmp2 <0){
cross=true;
break;
}
}
if(cross){
System.out.println("BAD");
}else{
System.out.println("GOOD");
}
}
public static int CCW(Point1912 A,Point1912 B,Point1912 C){
return A.x*B.y+B.x*C.y+C.x*A.y-(A.y*B.x+B.y*C.x+C.y*A.x) >0?1:-1;
}
}
class Point1912{
double x;
double y;
public Point1912(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "{" + x +
", " + y +
'}';
}
}