zoukankan      html  css  js  c++  java
  • POJ 3262 Protecting the flowers

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

    这道题和蝎子那道题是相同贪心思路

    http://www.cnblogs.com/oscar-cnblogs/p/6329703.html

    //贪心方式一:列出函数关系式比较复杂
    //方式二:像蝎子那道题 先讨论两只 奶牛怎样送是最优的
    /*
    a1 b1
    a2 b2
    先送1 ans = 2*a1*b2
    先送2 ans = 2*b1*a2
    那么如果先送1 更优说明 2*a1*b2 < 2*b1*a2

    --->> a1 / b1 < a2 / b2这样就得出比较方式了
    */

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 
     7 int n;
     8 struct Cow
     9 {
    10     double t, d;
    11     bool operator < (Cow a) const
    12     {
    13         return t / d < a.t / a.d;
    14     }
    15 }cow[100007];
    16 int main()
    17 {
    18     freopen("in.txt", "r", stdin);
    19     while(~scanf("%d", &n))
    20     {
    21         long long ans = 0, sum = 0;
    22         for (int i = 0; i < n; i++)
    23         {
    24             scanf("%lf%lf", &cow[i].t, &cow[i].d);
    25             sum += cow[i].d;
    26         }
    27         sort(cow, cow+n);
    28         for (int i = 0; i < n; i++)
    29         {
    30             sum -= cow[i].d;
    31             ans += sum*cow[i].t*2;
    32             //printf("%.0lf %.0lf
    ", cow[i].t, cow[i].d);
    33         }
    34         printf("%lld
    ", ans);
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    buuctf—web—高明的黑客
    buuctf—web—Easy Calc
    buuctf刷题之旅—web—EasySQL
    buuctf刷题之旅—web—随便注
    buuctf刷题之旅—web—WarmUp
    Dao
    Spring AOP配置
    分布式
    tomcat配置
    JVM知识
  • 原文地址:https://www.cnblogs.com/oscar-cnblogs/p/6376273.html
Copyright © 2011-2022 走看看