• poj -3262 Protecting the Flowers (贪心)


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

    开始一直是理解错题意了!!导致不停wa。

    这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛需要的时间和牛在花园每分钟吃多少花朵,问你怎么安排让损失最小。

    这题单独按time和eat排序都不行,得按它们的比率来排,如果是选择eat/time  则从大到小排,time/eat则从小到大排,但是不会严格证明。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <algorithm>
     8 #include <string>
     9 #include <set>
    10 #include <functional>
    11 #include <numeric>
    12 #include <sstream>
    13 #include <stack>
    14 #include <map>
    15 #include <queue>
    16 
    17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    18 
    19 #define ll long long
    20 #define inf 0x7f7f7f7f
    21 #define lc l,m,rt<<1
    22 #define rc m + 1,r,rt<<1|1
    23 #define pi acos(-1.0)
    24 
    25 #define L(x)    (x) << 1
    26 #define R(x)    (x) << 1 | 1
    27 #define MID(l, r)   (l + r) >> 1
    28 #define Min(x, y)   (x) < (y) ? (x) : (y)
    29 #define Max(x, y)   (x) < (y) ? (y) : (x)
    30 #define E(x)        (1 << (x))
    31 #define iabs(x)     (x) < 0 ? -(x) : (x)
    32 #define OUT(x)  printf("%I64d
    ", x)
    33 #define lowbit(x)   (x)&(-x)
    34 #define Read()  freopen("a.txt", "r", stdin)
    35 #define Write() freopen("dout.txt", "w", stdout);
    36 #define N 100005
    37 using namespace std;
    38 
    39 struct point
    40 {
    41     int x,y;
    42     double d;
    43     bool operator <(const point &a) const
    44     {
    45         return d>a.d;
    46     }
    47 }p[100010];
    48 int main()
    49 {
    50    //Read();
    51    int n;
    52    ll sum,t;
    53    while(~scanf("%d",&n))
    54    {
    55        sum=t=0;
    56        for(int i=0;i<n;i++)
    57        {
    58            scanf("%d%d",&p[i].x,&p[i].y);
    59            p[i].d=p[i].y*1.0/p[i].x;
    60            t+=p[i].y;  //这样处理 方便计算
    61        }
    62        sort(p,p+n);
    63        //for(int i=0;i<n;i++) printf("%d %d
    ",p[i].x,p[i].y);
    64        for(int i=0;i<n;i++)
    65        {
    66            t-=p[i].y;
    67            sum+=p[i].x*t*2;
    68        }
    69        printf("%lld
    ",sum);
    70    }
    71     return 0;
    72 }
  • 相关阅读:
    flink 使用processFunction函数的sideOutPut实现filter操作(java版)
    flink 状态编程之RichFlatMapFunction用法-java
    java flink之eventTime和window
    java spark sql 计算各个省份广告点击数的top3
    java spark 计算各个省份广告点击数的top3
    java streaming转换算子window,countByWindow
    java spark转换算子sortByKey
    java spark转换算子join、leftOuterJoin、rightOuterJoin、fullOuterjoin
    java spark转换算子groupByKey、reduceByKey、aggregateByKey
    java spark转换算子union、intersection、subtract
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4419896.html
Copyright © 2020-2023  润新知