[bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan

题目大意:n头牛,每头牛有两个参数t和atk。表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵花。求一个弄走牛的顺序,使得这些牛破坏最少的花。

注释:$1\le n \le 10^5$。


想法贪心

两头牛i和j。

只考虑这两头牛的位置。

如果i在j前面,拉走i的时候j会造成$2t_i*atk_j$朵花。反之同理。

比较两者谁大就放在前面。

在cmp中这样写就行了。

最后,附上丑陋的代码... ...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
using namespace std;
typedef long long ll;
struct Node
{
	ll t,d;
}a[N];
ll aft[N];
inline bool cmp(const Node &a,const Node &b)
{
	return a.d*b.t>b.d*a.t;
}
int main()
{
	int n; cin >> n ;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld%lld",&a[i].t,&a[i].d);
		a[i].t*=2;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=n;i>=1;i--)
	{
		aft[i]=aft[i+1]+a[i].d;
	}
	// for(int i=1;i<=n;i++) printf("Fuck %d\n",a[i].d);
	ll ans=0;
	for(int i=1;i<=n-1;i++)
	{
		ans+=a[i].t*aft[i+1];
	}
	printf("%lld\n",ans);
	return 0;
}

小结:贪心真tm吓人... ...

转载于:https://www.cnblogs.com/ShuraK/p/9397829.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值