P2878 [USACO07JAN]Protecting the Flowers S(奶牛吃花花)C++

在这里插入图片描述
在这里插入图片描述
输入输出样例:
输入:

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

输出:

86

思路:就是贪心,
我们把这两头牛的时间分别设为x1,x2

把他们的每分钟的食草速度设成y1,y2

那么如果先运奶牛1: 造成的损耗是:x.d * y.t

如果先运奶牛2: 造成的损耗是:y.d * x.t

优先运送损耗大的奶牛即可。

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;

struct cow{
    
    
	long long time;
	long long eat;
};

bool cmp(cow a, cow b)
{
    
    
	return a.eat*b.time > b.eat*a.time ;
}

int main()
{
    
    
	long long n,sum=0,time=0;
	cin >> n ;
	cow a[n+10];
	for(int i=0;i<n;i++)
	{
    
    
		cin >> a[i].time >> a[i].eat ;
	}
	sort(a,a+n,cmp);
	for(int i=0;i<n;i++)
	{
    
    
		sum+=a[i].eat*time;
		time+=a[i].time*2;
	}
	cout << sum << endl ;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wangyurenwls/article/details/121483225
今日推荐