zoukankan      html  css  js  c++  java
  • [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

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

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

    注释:$1le 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
    ",a[i].d);
    	ll ans=0;
    	for(int i=1;i<=n-1;i++)
    	{
    		ans+=a[i].t*aft[i+1];
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    

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

  • 相关阅读:
    Server 对象
    Response 对象
    bzoj 5252: [2018多省省队联测]林克卡特树
    bzoj 2167: 公交车站
    bzoj 5315: [Jsoi2018]防御网络
    bzoj 5319: [Jsoi2018]军训列队
    bzoj 4161: Shlw loves matrixI
    bzoj 4942: [Noi2017]整数
    bzoj 2648: SJY摆棋子
    kd-tree 小结
  • 原文地址:https://www.cnblogs.com/ShuraK/p/9397829.html
Copyright © 2011-2022 走看看