2338: [HNOI2011]数矩形

因为已经看了一眼题解,知道是算中点和长度,相同时构成一个矩形,所以就把所有的线段算出来,然后排序,相同的就更新答案

为了避免误差,我们都用整数存,中点直接相加就行了,没必要除2,长度也只要平方就行了,不用开根,算面积就用叉积算,这样做就完全没有误差了

虽然复杂度本来是不行的但是出题人没想卡咱,就过了

  1 const
  2     maxn=1500;
  3 type
  4     point=record
  5       x,y:longint;
  6     end;
  7     segment=record
  8       aa,bb:point;
  9       x,y:longint;
 10       l:int64;
 11     end;
 12 var
 13     a:array[0..maxn]of point;
 14     seg:array[0..maxn*maxn]of segment;
 15     n,tot:longint;
 16     ans:int64;
 17 
 18 procedure swap(var x,y:segment);
 19 var
 20     t:segment;
 21 begin
 22     t:=x;x:=y;y:=t;
 23 end;
 24 
 25 procedure sort(l,r:longint);
 26 var
 27     i,j:longint;
 28     x:segment;
 29 begin
 30     i:=l;
 31     j:=r;
 32     x:=seg[(l+r)>>1];
 33     repeat
 34       while (x.x<seg[i].x) or ((x.x=seg[i].x) and (x.y<seg[i].y)) or ((x.x=seg[i].x) and (x.y=seg[i].y) and (x.l<seg[i].l)) do
 35         inc(i);
 36       while (x.x>seg[j].x) or ((x.x=seg[j].x) and (x.y>seg[j].y)) or ((x.x=seg[j].x) and (x.y=seg[j].y) and (x.l>seg[j].l)) do
 37         dec(j);
 38       if i<=j then
 39       begin
 40         swap(seg[i],seg[j]);
 41         inc(i);
 42         dec(j);
 43       end;
 44     until i>j;
 45     if i<r then sort(i,r);
 46     if j>l then sort(l,j);
 47 end;
 48 
 49 procedure init;
 50 var
 51     i,j:longint;
 52 begin
 53     read(n);
 54     for i:=1 to n do
 55       with a[i] do
 56       read(x,y);
 57     for i:=1 to n-1 do
 58       for j:=i+1 to n do
 59         begin
 60           inc(tot);
 61           with seg[tot] do
 62             begin
 63               aa:=a[i];
 64               bb:=a[j];
 65               x:=a[i].x+a[j].x;
 66               y:=a[i].y+a[j].y;
 67               l:=sqr(int64(a[i].x-a[j].x))+sqr(int64(a[i].y-a[j].y));
 68             end;
 69         end;
 70     sort(1,tot);
 71 end;
 72 
 73 function cj(a,b,c:point):int64;
 74 begin
 75     exit(abs(int64(a.x-b.x)*int64(c.y-b.y)-int64(c.x-b.x)*int64(a.y-b.y)));
 76 end;
 77 
 78 procedure up(var x:int64;y:int64);
 79 begin
 80     if x<y then x:=y;
 81 end;
 82 
 83 procedure work;
 84 var
 85     l,r,i,j:longint;
 86 begin
 87     l:=1;
 88     r:=1;
 89     while l<=tot do
 90       begin
 91         while (r<tot) and (seg[r+1].x=seg[l].x) and (seg[r+1].y=seg[l].y) and (seg[r+1].l=seg[l].l) do
 92           inc(r);
 93         for i:=l to r-1 do
 94           for j:=i+1 to r do
 95             up(ans,cj(seg[i].aa,seg[i].bb,seg[j].aa));
 96         l:=r+1;
 97         r:=l;
 98       end;
 99     write(ans);
100 end;
101 
102 begin
103     init;
104     work;
105 end.
View Code
原文地址:https://www.cnblogs.com/Randolph87/p/3653724.html