计算几何+贪心
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct City { int id; double J1;//距离第一条 double J2;//距离第二条 double X,Y; double C; int be;//属于哪一条 double xuqiu; } node[210]; int n,c; int shu1,shu2; bool cmpid(const City&a,const City&b) //按照城市编号排序 { return a.id<b.id; } bool cmpbe(const City&a,const City&b) //按照归属分层 { return a.be<b.be; } bool cmpC(const City&a,const City&b) //按照差值排序 { return a.C<b.C; } int main() { int i; while(~scanf("%d%d",&n,&c)) { shu1=0,shu2=0; double x1,y1,x2,y2; double x3,y3,x4,y4; double A1,B1,C1; double A2,B2,C2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4); A1=y2-y1; A2=y4-y3; B1=x1-x2; B2=x3-x4; C1=x2*y1-x1*y2; C2=x4*y3-x3*y4; for(i=0; i<n; i++) scanf("%lf%lf%lf",&node[i].X,&node[i].Y,&node[i].xuqiu); for(i=0; i<n; i++) { node[i].J1=fabs(A1*node[i].X+B1*node[i].Y+C1)/sqrt(A1*A1+B1*B1); node[i].J2=fabs(A2*node[i].X+B2*node[i].Y+C2)/sqrt(A2*A2+B2*B2); if(node[i].J1<=node[i].J2) node[i].be=1,shu1++; else node[i].be=2,shu2++; node[i].C=fabs(node[i].J1-node[i].J2)*node[i].xuqiu; node[i].id=i; } if(abs(shu1-shu2)<=c) { for(i=0; i<n; i++) { if(i<n-1) printf("%d ",node[i].be); else printf("%d ",node[i].be); } } else { int fenjie=-1; sort(node,node+n,cmpbe); for(i=0; i<n; i++) if(node[i].be==2) break; fenjie=i-1; if(shu1>shu2) { sort(node,node+fenjie+1,cmpC); int yidong=0; for(i=0; i<=fenjie; i++) { node[i].be=2; shu1--; shu2++; if(abs(shu1-shu2)<=c) break; } sort(node,node+n,cmpid); for(i=0; i<n; i++) { if(i<n-1) printf("%d ",node[i].be); else printf("%d ",node[i].be); } } else { sort(node+fenjie+1,node+n,cmpC); int yidong=0; for(i=fenjie+1; i<=n-1; i++) { node[i].be=1; shu1++; shu2--; if(abs(shu2-shu1)<=c) break; } sort(node,node+n,cmpid); for(i=0; i<n; i++) { if(i<n-1) printf("%d ",node[i].be); else printf("%d ",node[i].be); } } } } return 0; }