Central Avenue Road+csuoj+水题
2014-10-05 16:56
330 查看
Central Avenue Road
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 30 Solved: 10[Submit][Status][WebBoard]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 isjust 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
4 0 0 3 4 10 0 0 10 0
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;}
相关文章推荐
- hrbust 2147 哈理工oj 经理办公室【水题】
- hrbust 哈理工oj 2113 Count【STL水题】
- Qz’s Maximum All One Square+csuoj+dp
- csuoj1023修路( )
- hrbust/哈理工oj 1677 一个函数【栈】【水题】
- CSUOJ1985: 驱R符
- 自从有了OJ水题嗅探神器,麻麻再也不担心我刷不到题了。。
- CSU 1202 剪刀石头布(水题)
- CSUOJ 1009 抛硬币
- CSUOJ 1808 地铁(用map储存DP数组的DP)
- Nearest Sequence+csuoj+最大公共子串
- csuoj1970LXX数
- hrbust/哈理工oj 1877 区间【水题】
- UVA oj 10300 Ecological Premium(水题)
- csuoj 2015.3.15
- CSUOJ 1021 组合数末尾的零 二进制
- Random Integers+csuoj+dp
- 【杭电oj】1220 - Cube(数学水题)
- 杭电OJ——1196 Lowest Bit(水题)
- csuoj 1339 最后一滴血