码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3262 Protecting the Flowers 贪心

时间:2016-04-27 08:18:18      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草

        求把所有奶牛送回牛棚内,所消耗草的最小值

 

思路:贪心,假设奶牛a和奶牛b所处位置为,

    交换前 ....(ta, da) (tb, db)....

    交换后 ....(tb, db) (ta, da)....

 

设此前已消耗的时间为x,那么交换前消耗的草:x*da + (x+ta)*db

            交换后消耗的草:x*db + (x+tb)*da

除非交换后的消耗相比交换前的小才交换,即ta*db > tb*da

 

AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100005;
const int INF = 0X3f3f3f3f;
int n,t[N],d[N],sum,vis[N];
struct node
{
	int t,d;
}w[N];
int cmp(node n1, node n2)
{
	return n1.t*n2.d < n1.d * n2.t;
}
void solve()
{
	sort(w,w+n,cmp);
	long long ans = 0;
	for(int i = 0; i < n; i++)
	{
		sum -= w[i].d;
		ans += sum*w[i].t*2;
	}
	cout<<ans<<endl;
}
int main()
{
	while(~scanf("%d", &n))
	{
		sum = 0;
		for(int i = 0; i < n; i++)
			scanf("%d %d", &w[i].t, &w[i].d),sum+=w[i].d;
		solve();
	}
	return 0;
}

  

poj 3262 Protecting the Flowers 贪心

标签:

原文地址:http://www.cnblogs.com/sevenun/p/5437416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!