【题解】Protecting the Flowers
Protecting the Flowers
https://ac.nowcoder.com/acm/problem/25043
题面:有n只牛,把牛都放到郊外吃花,每次带回一头牛,来回时长是2*ti,每头牛吃花的速度是di,问怎么带才能吃最少的花。
思路:像这种题一般都往贪心去想,贪心有一个这样的理解就是,两个对比来贪。
假如有两头牛,一个是a,一个是b,假如把a带回去,花费是 ,把b带回去就是
因为排序可以通过比较函数排,我们知道两头牛之间怎么去算花费,所以在比较函数里面把花费算出来,把小的排在前面就好了。
代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#include<map>
#define fs first
#define se second
#define pb push_back
#define cppio ios::sync_with_stdio(false);cin.tie(0)
#define eps 1e-7
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
const int maxn=1e5+5;
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;
struct Node{
int t,d;
}node[maxn];
int cmp(Node a,Node b){
return a.t*a.d<b.t*b.d;
}
int main(){
int n;
scanf("%d",&n);
ll sum=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&node[i].t,&node[i].d);
sum+=node[i].d;
}
sort(node+1,node+1+n,cmp);
ll ans=0;
for(int i=1;i<=n;i++){
sum-=node[i].d;
ans+=node[i].t*2*sum;
}
printf("%lld",ans);
return 0;
}
老板电器公司氛围 197人发布
