UVALive 6092 Catching Shade in Flatland --枚举+几何计算
题意:x=[-200,200],y=[-200,200]的平面,一天中太阳从不同角度射到长椅(原点(0,0))上,有一些树(用圆表示),问哪个时刻(分钟为单位)太阳光线与这些圆所交的弦长总和最长。太阳距离原点总是500m。(这些圆不会互相相交,每个圆都不包括原点或者不经过原点)
解法:直接暴力24*60分钟,找出此时的角度,然后求出直线方程,再枚举每个圆,求出弦长。注意这里每个圆都不包括原点,所以直线与圆的交点一定在同一侧,所以。。我当时想多了,没看清题目。把他当成可以包含原点了,代码超长,幸好过了。
代码:
没想多应该这样就可以了:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#define INint 2147483647
#define pi acos(-1.0)
#define eps 1e-4
using namespace std;
#define N 100102
#define M 22 typedef struct point
{
double x,y;
point(double x=,double y=):x(x),y(y){}
}Vector; double DegtoRad(double deg)
{
return deg/180.0*pi;
} int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
}
Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (point A,point B){return Vector(A.x-B.x,A.y-B.y); }
Vector operator * (Vector A,double p){return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p){return Vector(A.x/p,A.y/p);}
bool operator == (const point& a,const point& b){return dcmp(a.x-b.x)==&&dcmp(a.y-b.y)==;}
bool operator < (const point& a,const point& b){return a.x<b.x ||(a.x==b.x&&a.y<b.y);}
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;} //叉积 ,大于零说明B在A的左边。小于零说明B在A的右边
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;} //点积
double length(Vector A){return sqrt(Dot(A,A));} //向量长度 double DistanceToSegment(point P,point A,point B)
{
if(A==B) return length(P-A);
Vector v1=B-A,v2=P-A,v3=P-B;
if(dcmp(Dot(v1,v2))<) return length(v2);
else if(dcmp(Dot(v1,v3))>) return length(v3);
else return fabs(Cross(v1,v2))/length(v1);
} point p[];
double ra[]; int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF && n)
{
for(i=;i<n;i++)
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&ra[i]);
double maxi = 0.0;
int S = *;
for(i=;i<S;i++)
{
point A,B,C;
A = point(0.0,0.0);
double rad = DegtoRad(i/4.0);
B = point(*sin(rad),*cos(rad));
double sum = 0.0;
for(j=;j<n;j++)
{
C = p[j];
double dis = DistanceToSegment(C,A,B);
if(dis >= ra[j])
continue;
sum += 2.0*sqrt(ra[j]*ra[j]-dis*dis);
}
maxi = max(maxi,sum);
}
printf("%.3lf\n",maxi);
}
return ;
}
当时的代码(考虑了可能包含原点):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#define pi acos(-1.0)
using namespace std;
#define N 100102
#define M 22 struct node
{
double x,y,r;
}p[]; int getPlane(double nx,double ny)
{
if(nx > && ny > )
return ;
else if(nx > && ny < )
return ;
else if(nx < && ny < )
return ;
else if(nx < && ny > )
return ;
else
return ;
} double dis(int nx,int ny)
{
return sqrt(nx*nx + ny*ny);
} int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF && n)
{
for(i=;i<n;i++)
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].r);
int S = *;
double Si = 24.0*60.0;
int Plane;
double maxi = 0.0;
for(i=;i<S;i++)
{
if(i == || i == || i == || i == )
continue;
double A = tan(*pi*(double)i/Si);
double B = -1.0;
double k = A;
double di = sqrt(A*A+B*B);
if(i > && i < )
Plane = ;
else if(i > && i < )
Plane = ;
else if(i > && i < )
Plane = ;
else
Plane = ;
double sum = 0.0;
for(j=;j<n;j++)
{
double x = p[j].x;
double y = p[j].y;
double r = p[j].r;
double PtoL = fabs(A*x-y)/di;
if(PtoL > r)
continue;
double AA = k*k+1.0;
double BB = -(2.0*x+2.0*k*y);
double CC = x*x + y*y - r*r;
if(BB*BB-4.0*AA*CC <= 0.0)
continue;
double jie1x = (-BB+sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie1y = k*jie1x;
double jie2x = (-BB-sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie2y = k*jie2x;
int P1 = getPlane(jie1x,jie1y);
int P2 = getPlane(jie2x,jie2y);
if(P1 == Plane && P2 == Plane)
sum += 2.0*sqrt(r*r-PtoL*PtoL);
else if(P1 == Plane)
sum += dis(jie1x,jie1y);
else if(P2 == Plane)
sum += dis(jie2x,jie2y);
}
maxi = max(maxi,sum);
}
//up
double sum = 0.0;
for(j=;j<n;j++)
{
double x = p[j].x;
double y = p[j].y;
double r = p[j].r;
double PtoL = x;
if(PtoL > r)
continue;
double AA = 1.0;
double BB = -2.0*y;
double CC = x*x + y*y - r*r;
double jie1x = 0.0;
double jie1y = (-BB+sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie2x = 0.0;
double jie2y = (-BB-sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
if(jie1y > && jie2y > )
sum += fabs(jie1y-jie2y);
else if(jie1y > )
sum += jie1y;
else if(jie2y > )
sum += jie2y;
}
maxi = max(maxi,sum);
//down
sum = 0.0;
for(j=;j<n;j++)
{
double x = p[j].x;
double y = p[j].y;
double r = p[j].r;
double PtoL = x;
if(PtoL > r)
continue;
double AA = 1.0;
double BB = -2.0*y;
double CC = x*x + y*y - r*r;
double jie1x = 0.0;
double jie1y = (-BB+sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie2x = 0.0;
double jie2y = (-BB-sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
if(jie1y < && jie2y < )
sum += fabs(jie1y-jie2y);
else if(jie1y < )
sum += -jie1y;
else if(jie2y < )
sum += -jie2y;
}
maxi = max(maxi,sum);
//right
sum = 0.0;
for(j=;j<n;j++)
{
double x = p[j].x;
double y = p[j].y;
double r = p[j].r;
double PtoL = y;
if(PtoL > r)
continue;
double AA = 1.0;
double BB = -2.0*x;
double CC = x*x + y*y - r*r;
double jie1x = (-BB+sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie1y = 0.0;
double jie2x = (-BB-sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie2y = 0.0;
if(jie1x > && jie2x > )
sum += fabs(jie1x-jie2x);
else if(jie1x > )
sum += jie1x;
else if(jie2x > )
sum += jie2x;
}
maxi = max(maxi,sum);
//left
sum = 0.0;
for(j=;j<n;j++)
{
double x = p[j].x;
double y = p[j].y;
double r = p[j].r;
double PtoL = y;
if(PtoL > r)
continue;
double AA = 1.0;
double BB = -2.0*x;
double CC = x*x + y*y - r*r;
double jie1x = (-BB+sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie1y = 0.0;
double jie2x = (-BB-sqrt(BB*BB-4.0*AA*CC))/(2.0*AA);
double jie2y = 0.0;
if(jie1x < && jie2x < )
sum += fabs(jie1x-jie2x);
else if(jie1x < )
sum += -jie1x;
else if(jie2x < )
sum += -jie2x;
}
maxi = max(maxi,sum);
printf("%.3lf\n",maxi);
}
return ;
}
UVALive 6092 Catching Shade in Flatland --枚举+几何计算的更多相关文章
- UVALive - 3263 That Nice Euler Circuit (几何)
UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址: UVALive - 3263 That Nice Euler Circuit 题意: 给 ...
- 1549: Navigition Problem (几何计算+模拟 细节较多)
1549: Navigition Problem Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Su ...
- Jack Straws POJ - 1127 (几何计算)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5428 Accepted: 2461 Descr ...
- UVALive 6885 Flowery Trails 最短路枚举
题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...
- fzu 2035 Axial symmetry(枚举+几何)
题目链接:fzu 2035 Axial symmetry 题目大意:给出n个点,表示n边形的n个顶点,判断该n边形是否为轴对称图形.(给出点按照图形的顺时针或逆时针给出. 解题思路:将相邻两个点的中点 ...
- UVALive 6692 Lucky Number (思路 + 枚举)
题意:给你n 个数字,某一个数的幸运数是这个数前面比他小 离他最远的位置之差,求出最大幸运值. 析:先按从大到小排序,然后去维护那个最大的id,一直比较,更新最大值就好. 代码如下: #pragma ...
- Direct2D 几何计算和几何变幻
D2D不仅可以绘制,还可以对多个几何图形对象进行空间运算.这功能应该在GIS界比较吃香. 这些计算包括: 合并几何对象,可以设置求交还是求并,CombineWithGeometry 边界,加宽边界,查 ...
- Codeforces 552E Vanya and Brackets(枚举 + 表达式计算)
题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左 ...
- Jack Straws POJ - 1127 (简单几何计算 + 并查集)
In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...
随机推荐
- OWIN概述
关于OWIN OWIN defines a standard interface between .NET web servers and web applications. The goal of ...
- DOM笔记整理及应用实例
一.前言 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model).HTML DOM 模型被构造为对象的树 通过可编程的对象模型,JavaScript 获得了足够的 ...
- play framework学习笔记之 模板引擎
模板语法 ${client.name} ${client?.name} 不能确定client是否存在的时候? #{extends /} #{doLayout /}#{get} #{set} 比如 #{ ...
- 实践GTD三周年后的体会
从2011年7月开始实践GTD到现在已经3年多了,按照年初设定的目标,应该写点GTD的体会,但提起笔来的时候却发现并没有什么太新的体会,大部分体会都已经在以前的文章中写到了,甚至有些流程已经简化了,看 ...
- iOS 获取UIView所在的VIewController
写程序的时候我们经常要封装代码,当你封装了一个UIView的子类里需要调用所在ViewController的方法用如下代码调取所在的VIewController #pragma mark - 获取所在 ...
- iOS 新窗口在最上层
有的时候需要弹出一个UIView 在整个视图的最上方,使用 [self.view addsubview : view] 一般就可以了,但是这样不严谨,因为这是一个tableview,那么这个view ...
- 【读书笔记】iOS-NSDate
+dateWithTimeIntervalSinceNow:接受一个NSTimeInterval参数,该参数是一个双精度值,表示以秒为单位的时间间隔.通过该参数可以指定时间偏移的方式:对于将来的时间, ...
- SQL语句的简单使用
首先要先引入libsqlite3.0.tbd框架 DataBaseHandle.h #import <Foundation/Foundation.h> @interface DataBas ...
- GCD中的dispatch_apply的用法及作用
GCD中的dispatch_apply的用法及作用 (一)dispatch_apply的基本用法 dispatch_apply函数是dispatch_sync函数和Dispatch Group的关联A ...
- iOS开发笔记13:顶部标签式导航栏及下拉分类菜单
当内容及分类较多时,往往采用顶部标签式导航栏,例如网易新闻客户端的顶部分类导航,最近刚好有这样的应用场景,参考网络上一些demo,实现了这种导航效果,记录一些要点. 效果图(由于视频转GIF掉帧,滑动 ...