bzoj1071

朴素的做法显然是O(n3)的
考虑优化,我们将约束条件变形为A*h+B*v<=A*minh+B*minv+c
右边是一个定值,当右边确定了minh之后,随着minv的增大,原来满足条件的且v>=minv的一定还满足条件
然后我们只要先对A*h+B*v排序,然后穷举minh,然后扫一遍即可
O(n2)的算法很显然,我正好卡着时限过去的……
但这道题的本意肯定是有O(nlogn)的算法,求教导

 type node=record
h,v,s:longint;
end; var f,v:array[..] of longint;
h:array[..] of boolean;
w:array[..] of node;
ans,t,m,x,y,n,i,j,sum,s,a,b,c,k:longint; procedure swap(var a,b:node);
var c:node;
begin
c:=a;
a:=b;
b:=c;
end; procedure sort(l,r: longint);
var i,j,x: longint;
begin
i:=l;
j:=r;
x:=w[(l+r) div ].s;
repeat
while w[i].s<x do inc(i);
while x<w[j].s do dec(j);
if not(i>j) then
begin
swap(w[i],w[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; begin
readln(n,a,b,c);
for i:= to n do
begin
readln(x,y);
h[x]:=true;
if x>m then m:=x;
v[y]:=;
w[i].h:=x;
w[i].v:=y;
w[i].s:=a*x+b*y;
end;
sort(,n);
for i:= to do
if v[i]= then
begin
inc(t);
f[t]:=i; //表示v可能的数字
end; for i:= to m do
begin
if not h[i] then continue; //穷举minh
fillchar(v,sizeof(v),);
sum:=;
k:=;
for j:= to t do
begin
if j<> then
sum:=sum-v[f[j-]]; //对于当前minv,小于的不算
s:=a*i+b*f[j]+c;
while (k<=n) and (w[k].s<=s) do
begin
if (w[k].h>=i) and (w[k].v>=f[j]) then
begin
inc(sum);
inc(v[w[k].v]); //方便随着minv的增大,快速减去小于minv的数目
end;
inc(k);
end;
if sum>ans then ans:=sum;
if k>n then break; //后面一定只减不增
end;
end;
writeln(ans);
end.
上一篇:[javaEE] 开源数据库连接池


下一篇:Windows普通窗口程序