[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吓人... ...

posted @ 2018-07-31 19:31  JZYshuraK_彧  阅读(183)  评论(0编辑  收藏  举报