【贪心】Protecting the Flowers

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

题意:
农夫有n头牛在花丛里破坏花,他要把牛给带回家,这样花就能不被破坏了,
但是一次只能带一头牛,每头牛带到家里要花 ti 分钟(当然,从花丛到家再到花丛就是要花 2 * t i分钟了),每头牛每分钟要破坏 di 朵花,问农夫怎么带牛能使总的破坏的花总数最少,当然,农夫一开始在花丛。

输入:
第一行输入n。
随后的n行输入每头牛的 ti 和 di

输出:
输出最小的破坏总数

在这里插入图片描述

ll t,l,n,m,p,q,sum=0,ans=0;
string s;
struct node{
	int t,d;
}a[maxn];
bool cmp(node x,node y){
	return x.t*1.0/x.d < y.t*1.0/y.d;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].t>>a[i].d;
		sum+=a[i].d;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++){
		sum-=a[i].d;
		ans+=2*a[i].t*sum;
	}
	cout<<ans<<endl;

	return 0;
}

这题需要稍微考虑下了。

如果先带第i头牛,那就是 2 * ti * dj
如果先带第j头牛,那就是 2 * tj * di

假设先带第i头牛的方案更优,
那就是 ti* dj <tj *di
即 ti / di < tj / dj 注意乘上个1.0 这样才严谨

然后按照这样的cmp排序,
每次算ans+=2a[i].tsum;
sum是剩下的牛总共每分钟能吃多少

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
阅读下面材料,在空格处填入适当的内容(1个单词)或使用括号中单词的正确形式。 If including inter-state sections, those not protecting the northern border of China, the oldest _____56____(exist) section of the Great Wall of China was the Qi State "Great Wall". It stretches for over 500 kilometers (300 miles) from the Yellow River at Jinan eastwards to the East China Sea, almost ___57____(divide) Shandong Province in half. The "Great Wall" of the Qi State was ___58____(initial) built around 650 BC, and expended during the Warring States Period (475–221 BC). Before the Qi State Wall was built, natural barriers, i.e. rivers and mountain ranges, formed ____59_____ only defensible boundaries between territories as barriers ___60____ enemies. The State of Qi built its Great-Wall-esque military barrier along its southern border to prevent attacks from the State of Lu and the State of Chu. However, rapid development and ____61____(construct) have brought many new problems and challenges in protecting the wall. ____62____ is necessary to provide a solid legal guarantee for its conservation. To tackle the challenges, Shandong Province has passed a regulation protecting the structure____63____will take effect on Jan. 1. This year, Shandong has added 860 patrol posts in seven cities along the Qi wall, mainly recruiting farmers living nearby. Guo Jialian has been patrolling the section of the wall in Guangli village for three months. "I need to check ____64______ there is any damage to the wall that is caused by people digging earth from it. Awareness of protecting the Qi wall _____65_____ (enhance) in recent years. We all know the wall is a cultural relic," says Guo, adding that he frequently sees tourists coming to visit the ancient structure.
最新发布
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值