# BZOJ1597: [Usaco2008 Mar]土地购买

## 1597: [Usaco2008 Mar]土地购买

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1448  Solved: 504
[Submit][Status]

## Input

* 第1行: 一个数: N

* 第2..N+1行: 第i+1行包含两个数,分别为第i块土地的长和宽

* 第一行: 最小的可行费用.

4
100 1
15 15
20 5
1 100

500

## HINT

FJ分3组买这些土地: 第一组:100x1, 第二组1x100, 第三组20x5 和 15x15 plot. 每组的价格分别为100,100,300, 总共500.

## Source

 1 const maxn=50000+100;
2 type node=record
3      x,y:int64;
4      end;
5 var a,b:array[0..maxn] of node;
6     q,sta:array[0..maxn] of longint;
7     i,n,top,l,r:longint;
8     f:array[0..maxn] of int64;
9 function cmp(a,b:node):boolean;
10  begin
11  if a.x=b.x then exit(a.y<b.y) else exit(a.x<b.x);
12  end;
13 procedure sort(l,r:longint);
14  var i,j:longint;x,y:node;
15  begin
16  i:=l;j:=r;x:=a[(i+j)>>1];
17  repeat
18   while cmp(a[i],x) do inc(i);
19   while cmp(x,a[j]) do dec(j);
20   if i<=j then
21    begin
22    y:=a[i];a[i]:=a[j];a[j]:=y;
23    inc(i);dec(j);
24    end;
25  until i>j;
26  if i<r then sort(i,r);
27  if j>l then sort(l,j);
28  end;
29 procedure init;
30  begin
32  for i:=1 to n do readln(a[i].x,a[i].y);
33  sort(1,n);
34  for i:=1 to n do
35   begin
36   while (top>0) and (a[i].y>=a[sta[top]].y) do dec(top);
37   inc(top);sta[top]:=i;
38   end;
39  for i:=1 to top do b[i]:=a[sta[i]];n:=top;
40  end;
41 function k(i,j:longint):double;
42  begin
43  k:=(f[i]-f[j])/(b[j+1].y-b[i+1].y);
44  end;
45 function w(i,j:longint):int64;
46  begin
47  w:=f[j]+b[j+1].y*b[i].x
48  end;
49 procedure main;
50  begin
51  l:=0;r:=0;
52  for i:=1 to n do
53   begin
54   while (l<r) and (k(q[l+1],q[l])<b[i].x) do inc(l);
55   f[i]:=w(i,q[l]);
56   while (l<r) and (k(i,q[r])<k(q[r],q[r-1])) do dec(r);
57   inc(r);q[r]:=i;
58   end;
59  writeln(f[n]);
60  end;
61 begin
62   assign(input,'input.txt');assign(output,'output.txt');
63   reset(input);rewrite(output);
64   init;
65   main;
66   close(input);close(output);
67 end. 
View Code

posted @ 2014-08-12 15:46  ZYF-ZYF  Views(163)  Comments(0Edit  收藏  举报