Central Avenue Road+csuoj+水题
来源:互联网 发布:免费域名跳转 编辑:程序博客网 时间:2024/05/16 03:26
Central Avenue Road
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 30 Solved: 10
[Submit][Status][Web Board]
Description
There are a lot of trees in the park of Jilin University. We want to open up a road in this forest in a straight line from one tree to another. In order to view more scenery, the difference of both sides of the road is minimum (zero or one). If other tree is just on the road, it is not counted on both sides. Of course, we have a lot of ways to build this road. The best way is that the distance of two reference trees is shortest.
Input
The first line of each case is the number of trees N (2 <= N <=100). The next N lines have two integers that is the x and y coordinates of the i-th tree. The input file is terminated by N=0.
Output
For each case, output the distance of the two reference trees that the road obeys the above rules and the distance is shortest. Keep three digits after decimal point.
Sample Input
40 03 410 00 100
Sample Output
5.000
解决方案:求建一条两颗树之间的路,要求左边的树的数目和右边的数目之差最多不能超过1。暴力即可,但要特判斜率为0和为无穷的情况。
code:#include <iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;const int maxn=105;int x[maxn],y[maxn];int n;double dist(int i,int j){ return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+0.000001);}bool judge(int i,int j){ int a=y[j]-y[i]; int b=x[i]-x[j]; int c=x[j]*y[i]-x[i]*y[j]; int u=x[i]-x[j]; int d=y[i]-y[j]; int lf=0,rt=0; if(u==0&&d!=0) { for(int k=0; k<n; k++) { if(x[k]<x[i]) { lf++; } if(x[k]>x[i]) { rt++; } } } else if(d==0&&u!=0) { for(int k=0; k<n; k++) { if(y[k]<y[i]) { lf++; } if(y[k]>y[i]) { rt++; } } } else { for(int k=0; k<n; k++) { if(a*x[k]+b*y[k]+c>0) { lf++; } else if(a*x[k]+b*y[k]+c<0) { rt++; } } } if(fabs(rt-lf)<=1) return true; else return false;}int main(){ while(~scanf("%d",&n)&&n) { for(int i=0; i<n; i++) { scanf("%d%d",&x[i],&y[i]); } double Max=0x3f3f3f3f3f; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(judge(i,j)){ double temp=dist(i,j); if(temp<Max) Max=temp; } } } printf("%.3lf\n",Max); } return 0;}
- Central Avenue Road+csuoj+水题
- avenue technology
- CSUOJ
- CSUOJ
- road
- Road
- Road
- Road
- Road
- road
- csuoj--1307
- csuoj Lexicography
- csuoj X
- 1989CSUOJ
- CSUOJ优先队列
- CSUOJ 1165 枚举
- CSUOJ 1063 Sequence
- CSUOJ 1258 维护序列
- Servlet中注入Spring的bean
- ORA-600 Lookup Error Categories [ID 175982.1]
- C++学习资料和工具链接
- 在工具栏上加控件
- Acdream 1129 Beloved Sons 二分图匹配
- Central Avenue Road+csuoj+水题
- 伙伴们快去学习《代码大全》吧
- 读“程序员生存定律”笔记
- 使用VirtualBox安装Minix3
- Java设计模式菜鸟系列(七)命令模式建模与实现
- 数字电路设计之恢复余数除法器的verilog实现
- 基于树的排序,降序输出
- SDUTOJ 2116 数据结构实验之链表一:顺序建立链表
- JDBC和ODBC