我是想用对横坐标排序的方法,然后再分别枚举点,但是没有考虑到矩形相交的情况,想了半天,发现矩形如果相交貌似只能判断结点,一个结点在一个矩形的上面,另一个结点在一个矩形的下面,这样的话就是相交。
最一开始的想法理解错了,因此现在觉得好像坐标排序没什么用……
算法:搜索
1.先进行双关键字排序,加快程序的速度。
最一开始的想法理解错了,因此现在觉得好像坐标排序没什么用……
算法:搜索
1.先进行双关键字排序,加快程序的速度。
2.在确定一个点的情况下枚举另一个点,保证到最后一位且矩形数达到才可以更新。
program VJ1126;
const
maxn=50;
type
atp=record
x,y:longint;
end;
var
n,m,ans,check:longint;
a:array [0..maxn] of atp;
procedure init;
var
i:longint;
begin
ans:=maxlongint;
readln(n,m);
for i:=1 to n do readln(a[i].y,a[i].x);
end;
procedure qsort(l,r:longint);
var
i,j:longint;
tt,t:atp;
begin
i:=l;
j:=r;
tt:=a[(l+r) shr 1];
repeat
while (a[i].x<tt.x) or ((a[i].x=tt.x) and (a[i].y<tt.y)) do inc(i);
while (a[j].x>tt.x) or ((a[j].x=tt.x) and (a[j].y>tt.y)) do dec(j);
if i<=j then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
inc(i);
dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
procedure dfs(x,sum,tot:longint);
var
i,upx,dwx,lfy,rgy:longint;
begin
{只有当扫描到最后一位且分割矩形数=n才可以进行更新。}
if tot=m then
begin
if (sum<ans) and (check=n) then ans:=sum;
exit;
end;
upx:=a[x].x;
dwx:=a[x].x;
lfy:=a[x].y;
rgy:=a[x].y;
for i:=x to n do
begin
inc(check,i-x+1);
dwx:=a[i].x;
if a[i].y<lfy then lfy:=a[i].y;
if a[i].y>rgy then rgy:=a[i].y;
dfs(i+1,sum+(dwx-upx)*(rgy-lfy),tot+1);
dec(check,i-x+1);
end;
end;
begin
assign(input,'VJ1126.in'); reset(input);
assign(output,'VJ1126.out'); rewrite(output);
init;
qsort(1,n);
dfs(1,0,0);
writeln(ans);
close(input); close(output);
end.