poj 3262 Protecting the Flowers 【贪心】

来源:互联网 发布:淘宝刀创 编辑:程序博客网 时间:2024/05/16 17:55

这道题跟hdoj4882 ZCC Loves Codefires 几乎一模一样,只是改了一下下。

解题策略 :参考hdoj4882的解法 http://blog.csdn.net/shengweisong/article/details/38316699

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

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef struct{double t, d;double r;}str;str s[100005]; int cmp(str a, str b){return a.r<b.r;}int main(){int n, i, j;while(scanf("%d", &n) == 1){for(i = 0; i < n; i ++){scanf("%lf%lf", &s[i].t, &s[i].d);s[i].r = s[i].t/s[i].d;}sort(s, s+n, cmp);__int64 sum =0, time = 2*s[0].t; //和hdoj4882 这处有点小小的改变for(i = 1; i < n; i ++){sum += time *s[i].d;time += 2*s[i].t;}printf("%I64d\n", sum);}}


0 0
原创粉丝点击