zoukankan      html  css  js  c++  java
  • POJ-3262

    链接:http://poj.org/problem?id=3262

    题意:有n头牛,第i头牛送回牛棚需要Ti分钟,每分钟吃Di朵花。当牛送回时就不会再吃花。求被吃的花最小数。

    思路:贪心。但开始简单以为优先送回吃的多的就行,W了两发幡然醒悟,应该这样排序:对两头牛a、b,若a被送回,b吃的花为b.d*a.t;若b被送回,a吃的花为a.d*b.t,因此对这两个进行比较,优先选出较大值。

    代码:

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 int n;
     5 long long ans,total;
     6 struct cow
     7 {
     8     long long t,d;
     9 }c[110000];
    10 bool cmp(cow a,cow b)
    11 {
    12     long long p1=a.d*b.t,p2=a.t*b.d;
    13     return p1>p2;
    14 }
    15 int main(void)
    16 {
    17     cin>>n;
    18     for(int i=1;i<=n;i++)
    19     {
    20         cin>>c[i].t>>c[i].d;
    21         total+=c[i].d;
    22     }
    23     sort(c+1,c+1+n,cmp);
    24     for(int i=1;i<=n;i++)
    25     {
    26         total-=c[i].d;
    27         ans+=total*2*c[i].t;
    28     }
    29     cout<<ans<<endl;
    30     return 0;
    31 }
  • 相关阅读:
    11月1号笔试题总结
    10月30笔试题总结
    web前端常用单词
    9月13日·碎碎念
    python 匿名函数
    python 二分法查找
    python 递归
    python内置函数
    python 列表生成式
    python 生成器
  • 原文地址:https://www.cnblogs.com/yanying7/p/13341769.html
Copyright © 2011-2022 走看看