安装服务器
(server.pas/c/cpp)
【问题描述】
政府计划建立一个大型的服务器中心,为各个城市提供网络服务。每个城市对网络的需求量是不一样的,而需求量越大,对线路的要求也就越高,线路的成本也就越高。因此需要选择合适的地点修建。每个城市用一个二维整数坐标表示,两个点之间的距离定义为水平距离+垂直距离,即a,b两点间距离为D(a,b)=|Xa-Xb|+|Ya-Yb|。对于每个城市,线路的费用为:费用=距离×人口×城市的网络需求程度。总的费用为各个城市的费用的总和。请你找出最适合安装服务器(即总费用最小)的整数坐标(不一定要在城市上)。
【输入文件】
输入文件server.in第一行有一个正整数N(N ≤ 100000),表示城市的数量。后面的n行每行描述一个城市,每行有四个整数x,y,p,k分别表示城市的坐标,人口数,以及网络需求程度。(0 < x, y < 2^31;p≤600, k ≤30)
【输出文件】
输出文件server.out包含一行。在这一行中,应当包含两个整数x,y表示最优解的坐标,如果有多个最优解,那么输出x最小的,如果有x相同,那么输出y最小的。
【样例输入】
5
2 3 5 3
2 1 100 30
2 2 1 1
3 2 7 6
1 1 4 30
【样例输出】
2 1
【数据规模】
对于30%的数据,保证有N <= 500,x, y <= 100;
对于50%的数据,保证有N <= 5000;
对于全部的数据,保证有N <= 100000。
=====================================
带权中位数
----------------------------------
这道题公式
p*[abs(x-x1)+abs(x-x2)+......]+p*[abs(y-y1)+abs(y-y2)]
先按距离排序
在累加,直到>=tot/2;
=========================================
type
node=record
x,y,v:longint;
end;
var
n:longint;
map:array[1..100000]of node;
procedure init;
begin
assign(input,'server.in');
assign(output,'server.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
procedure qsort1(s,t:longint);
var
i,j:longint;
x:longint;
tem:node;
begin
i:=s; j:=t;
x:=map[(s+t)shr 1].x;
repeat
while x<map[j].x do dec(j);
while map[i].x<x do inc(i);
if i<=j then
begin
tem:=map[i];
map[i]:=map[j];
map[j]:=tem;
inc(i); dec(j);
end;
until i>j;
if i<t then qsort1(i,t);
if s<j then qsort1(s,j);
end;
procedure qsort2(s,t:longint);
var
i,j:longint;
x:longint;
tem:node;
begin
i:=s; j:=t;
x:=map[(s+t)shr 1].y;
repeat
while x<map[j].y do dec(j);
while map[i].y<x do inc(i);
if i<=j then
begin
tem:=map[i];
map[i]:=map[j];
map[j]:=tem;
inc(i); dec(j);
end;
until i>j;
if i<t then qsort2(i,t);
if s<j then qsort2(s,j);
end;
procedure main;
var
i:longint;
p,k,t1:longint;
tot:longint;
begin
readln(n);
tot:=0;
for i:=1 to n do
begin
readln(map[i].x,map[i].y,p,k);
map[i].v:=p*k;
tot:=tot+map[i].v;
end;
tot:=tot shr 1;
qsort1(1,n);
t1:=0;
for i:=1 to n do
begin
t1:=t1+map[i].v;
if t1>=tot then
begin
write(map[i].x,' ');
break;
//terminate;
end;
end;
qsort2(1,n);
t1:=0;
for i:=1 to n do
begin
t1:=t1+map[i].v;
if t1>=tot then
begin
writeln(map[i].y);
terminate;
end;
end;
end;
begin
init;
main;
terminate;
end.