2338: [HNOI2011]数矩形
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 877 Solved: 358
[ Submit][ Status]
Description
这道题其实还是比较简单的,也是做的计算几何的第一道题,主要思路就是取任意两个点为顶点来建边,然后按边的长度排序,由矩形的性质可知当长度相等且中点相同的两条边能构成以这两条边为对角线的矩形,再利用叉积算出面积即可。。。不知道为什么在bzoj上A了但在cogs上T两个点。
T的程序:
/**************************************************************
Problem: 2338
User: _vampire_
Language: C++
Result: Accepted
Time:12284 ms
Memory:80404 kb
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
int n,tot=0;
struct point{
LL x,y;
}p[1501];
struct line{
LL x1,y1,x2,y2,x,y,len,xx,yy;
}l[1125001];
inline int my_cmp(line a,line b){
if(a.len<b.len) return 1;
if(a.len==b.len&&a.x<b.x) return 1;
if(a.len==b.len&&a.x==b.x&&a.y<b.y) return 1;
return 0;
}
int main()
{
int i,j;
LL ans=0,area=0;
scanf("%d",&n);
for(i=1;i<=n;++i){
cin>>p[i].x>>p[i].y;
}
for(i=1;i<=n-1;++i){
for(j=i+1;j<=n;++j){
tot+=1;
l[tot].x1=p[i].x;l[tot].y1=p[i].y;
l[tot].x2=p[j].x;l[tot].y2=p[j].y;
l[tot].x=p[i].x+p[j].x;
l[tot].y=p[i].y+p[j].y;
l[tot].len=pow(p[i].x-p[j].x,2)+pow(p[i].y-p[j].y,2);
l[tot].xx=p[j].x-p[i].x;l[tot].yy=p[j].y-p[i].y;
}
}
sort(l+1,l+tot+1,my_cmp);
for(i=2;i<=tot;++i){
j=i-1;
while(l[i].len==l[j].len&&l[i].x==l[j].x&&l[i].y==l[j].y){
area=l[i].xx*l[j].yy-l[i].yy*l[j].xx;
ans=max(ans,abs(area));
j-=1;
}
}
cout<<ans/(LL)2<<endl;
}
终于改对了。。。可能是因为我没有用比较正统的计算几何的方法才导致T的,看着课本上的计算几何基础写了遍,快了好多233
code:
/**************************************************************
Problem: 2338
User: _vampire_
Language: C++
Result: Accepted
Time:2096 ms
Memory:36460 kb
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL long long
struct point{
LL x,y;
point(){}
point(LL _x,LL _y):x(_x),y(_y){}
}p[1501];
struct line{
int a,b;
LL len;
point mid;
line(){}
line(int _a,int _b,LL _len,point _mid):a(_a),b(_b),len(_len),mid(_mid){}
}l[1125000];
point operator + (point a,point b) {return point(a.x+b.x,a.y+b.y);};
point operator - (point a,point b) {return point(a.x-b.x,a.y-b.y);};
LL operator * (point a,point b) {return a.x*b.y-a.y*b.x;};
bool operator < (point a,point b) {return a.x==b.x ? a.y<b.y : a.x<b.x;};
bool operator > (point a,point b) {return a.x==b.x ? a.y>b.y : a.x>b.x;};
bool operator == (point a,point b) {return a.x==b.x&&a.y==b.y;};
int n,tot=0;
LL ans=0;
inline LL dis(point a,point b) {return (LL)pow(a.x-b.x,2)+pow(a.y-b.y,2);};
bool operator == (line a,line b) {return a.mid==b.mid&&a.len==b.len;};
bool operator < (line a,line b) {return a.len==b.len ? a.mid<b.mid : a.len<b.len;};
int main()
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%lld%lld",&p[i].x,&p[i].y);
for(i=1;i<n;++i){
for(j=i+1;j<=n;++j){
tot+=1;
l[tot]=line(i,j,dis(p[i],p[j]),p[i]+p[j]);
}
}
sort(l+1,l+tot+1);
for(i=2;i<=tot;++i){
for(j=i-1;j>=1&&l[i]==l[j];--j){
ans=max(ans,abs((p[l[i].a]-p[l[j].a])*(p[l[i].a]-p[l[j].b])));
}
}
printf("%lld",ans);
}