蓝桥杯搬运冰块(贪心)

贪心: 2个的个例的优先级 决定了整个数列的优先级 喵喵喵

试题 算法提高 搬运冰块
     
资源限制
时间限制:1.0s   内存限制:256.0MB
问题描述
  丑枫接到了一份奇葩的工作:往冰库里搬运冰块.冰库外放着N箱冰块,由于室外温度高,冰块会很快融化,且每箱冰块的融化速度不同.因为每箱冰块的体积,质量不等,把每箱冰块搬运进冰块花费的时间也不同.因此需要合理安排搬运顺序,才能使总的冰块融化量最小.丑枫请你帮忙计算最少的总融化量是多少,以便汇报上司.
输入格式
  第一行输入整数N
  接下来N行,每行两个整数,分别表示每箱冰块的搬运耗时Ti及融化速度Di.
输出格式
  输出最少的总融化量
样例输入
6
6 1
4 5
4 3
6 2
8 1
2 6
样例输出
86
数据规模和约定
  2<=N<=100000,1<=Ti<=4000000,1<=Di<=100
样例说明
  按照6、2341、5的顺序搬运
View problem
#include <bits/stdc++.h>
using namespace std;
#define ri register int
#define M 100005

template <class G> void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=(ch=='-');ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
}
struct dian{
    int t,d;
}p[M];

bool cmp(dian a,dian b)
{
    return a.t*b.d<b.t*a.d;
}
int n,m;
int main(){
    
    read(n);
    long long mx=0;
    for(ri i=1;i<=n;i++)
    {
        read(p[i].t);read(p[i].d);
        mx+=p[i].d;
    }
    sort(p+1,p+1+n,cmp);
    long long ans=0;
    for(ri i=1;i<=n;i++)
    {   
        mx-=p[i].d;
        ans+=(mx*p[i].t);
        
    }
    printf("%lld",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Lamboofhome/p/15690243.html