1276. 护花 (Standard IO)

Description

FJ出去砍木材去了,把N(2<=N<=100,000)头牛留在家中吃草,当他回来的时候,发现奶牛们都跑到花园里吃花去了,为了减少损失,FJ打算把牛移到牛棚中去。
每头牛的位置离牛棚需要Ti分钟(1<=Ti<=2,000,000),而且在等待被移走的过程中,每分钟破坏Di(1<=Di<=100)朵花,无论多么努力FJ一次只能移动一只奶牛,移动一只奶牛到牛棚需要2×Ti分钟(来回各一次)。
写一个程序安排移动顺序使得损失的花最少。

Input

第1行输入一个整数N
第2到N+1行每行包含两个整数Ti和Di

Output

输出一个整数表示最少损失的花的数量

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

思路

O(nlogn)
用s保存每头奶牛的性价比,在对s进行快排。
var
  n:longint;
  t,d:array[1..100000] of longint;
  s:array[1..100000] of real;
procedure px(x,y:longint);
var
  l,h,k:longint;
  mid,f:real;
begin
  l:=x;h:=y;
  mid:=s[(l+h) div 2];
  repeat
    while mid>s[l] do inc(l);
    while mid<s[h] do dec(h);
    if l<=h then
      begin
        k:=t[l];t[l]:=t[h];t[h]:=k;
        k:=d[l];d[l]:=d[h];d[h]:=k;
        f:=s[l];s[l]:=s[h];s[h]:=f;
        inc(l);dec(h);
      end;
  until l>h;
  if x<h then px(x,h);
  if y>l then px(l,y);
end;
var
  i,j,l,x:longint;
  ans:int64;
begin
  readln(n);
  for i:=1 to n do
    begin
      readln(t[i],d[i]);
      s[i]:=t[i]/d[i];
      l:=l+d[i];
    end;
  px(1,n);
  for i:=1 to n do
    begin
      l:=l-d[i];
      ans:=ans+l*2*t[i];
    end;
  writeln(ans);
end.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值