接水问题 二

https://www.51nod.com/contest/Problem.html#!problemId=2200&contestId=54 

n个人一起排队接水,第i个人的重要性是a[i],需要b[i]的时间来接水。

1 <= n <= 100000 

0 <= b[i] <= 1000

0 <= a[i] <= 1000

同时只能有一个人接水,正在接水的人和没有接水的人都需要等待。

完成接水的人会立刻消失,不会继续等待。

你可以决定所有人接水的顺序,并希望最小化所有人等待时间乘以自己的重要性a[i]的总和。

Input

第一行一个整数n。
以下n行,每行两个整数a[i]和b[i]。

Output

一行一个整数表示答案。

Input示例

4
1 4
2 3
3 2
4 1

Output示例

35

设第一个人重要性a[x], 时间b[x], 同理第二个人a[y], b[y]
那么如果第一个人在前,则b[x] * a[x] + a[y] * (b[x] + b[y])
那么如果第二个人在前,则b[y] * a[y] + a[x] * (b[x] + b[y])
化简之后可得a[y] * b[x] 与 a[x] * b[y] 

#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
pair<ll,ll>a[maxn];
bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
    return a.second*b.first<a.first*b.second;
}
int main()
{
   int n;
   cin>>n;
   for(int i=1;i<=n;i++)
   {
       cin>>a[i].first>>a[i].second;
       if(a[i].first==0||a[i].second==0) i--,n--;
   }
   sort(a+1,a+n+1,cmp);
   ll ans=0,time=0;
   for(int i=1;i<=n;i++)
   {
       time+=a[i].second;
       ans+=a[i].first*time;
   }
   cout<<ans<<endl;
   return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_38924883/article/details/81986918