题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634

题意:

  约翰留下他的N只奶牛上山采木。可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!

  为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚。

  第i只牛所在的位置距离牛棚t[i](1 <= t[i] <= 2000000)分钟的路程,而在约翰开始送她回牛棚之前,她每分钟会啃食e[i](1 <= e[i] <= 100)朵鲜花。

  无论多么努力,约翰一次只能送一只牛回棚。而运送第第i只牛事实上需要2Ti分钟,因为来回都需要时间。

  写一个程序来决定约翰运送奶牛的顺序,使最终被吞食的花朵数量最小。

题解:

  贪心。

  对于顺序相邻的两只牛a和b,交换a和b的顺序,对于a和b之外的牛是没有影响的。

  将其他的牛看作一只牛c。

  当a排在b之前时,答案为:

    ans1 = t[a]*(e[b]+e[c]) + t[b]*e[c]

  当b排在a之前时,答案为:

    ans2 = t[b]*(e[a]+e[c]) + t[a]*e[c]

  假设a排在b前面的时候答案更优,则有:

    ans1 < ans2

    即:t[a]*(e[b]+e[c]) + t[b]*e[c] < t[b]*(e[a]+e[c]) + t[a]*e[c]

    整理得:t[a]*e[b] < t[b]*e[a]

  所以按照t[a]*e[b] < t[b]*e[a]排序就好了。

AC Code:

 // before: t[a]*(e[b]+e[c]) + t[b]*e[c]
// after: t[b]*(e[a]+e[c]) + t[a]*e[c]
// if a is better:
// t[a]*(e[b]+e[c]) + t[b]*e[c] < t[b]*(e[a]+e[c]) + t[a]*e[c]
// t[a]*e[b] + t[a]*e[c] + t[b]*e[c] < t[b]*e[a] + t[b]*e[c] + t[a]*e[c]
// t[a]*e[b] < t[b]*e[a]
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 100005 using namespace std; struct Cow
{
int eat;
int tim;
Cow(int _eat,int _tim)
{
eat=_eat;
tim=_tim;
}
Cow(){}
friend bool operator < (const Cow &a,const Cow &b)
{
return a.tim*b.eat<b.tim*a.eat;
}
}; int n;
long long ans=;
Cow cow[MAX_N]; void read()
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>cow[i].tim>>cow[i].eat;
cow[i].tim<<=;
}
} void solve()
{
sort(cow,cow+n);
int tot=;
for(int i=;i<n;i++)
{
tot+=cow[i].eat;
}
for(int i=;i<n;i++)
{
tot-=cow[i].eat;
ans+=cow[i].tim*tot;
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}

BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】的更多相关文章

  1. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )

    考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...

  2. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the g ...

  3. bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】

    因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小.按照a.t*b.f排降序,模拟着计算答案 #include<iostream> #include<cstdi ...

  4. 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  So ...

  5. [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 885  So ...

  6. 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1634 贪心.. 我们发现,两个相邻的牛(a和b)哪个先走对其它的牛无影响,但是可以通过 a的破坏花× ...

  7. 【bzoj1634】[Usaco2007 Jan]Protecting the Flowers 护花 贪心

    题目描述 Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, a ...

  8. BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 448  So ...

  9. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

随机推荐

  1. VC6 在使用VC助手(Visual AssistX)在Win7下不能使用↑↓←→及回车键选择的解决的方法

    VC6使用Visual AssistX版本号的问题,换一个版本号.如"Visual Assist X 10.8.2029"就可以解决. http://pan.baidu.com/w ...

  2. 聚合数据Android SDK 12306火车票查询订票演示示例

    1.聚合SDK是聚合数据平台,为移动开发者提供的免费数据接口.使用前请先到聚合平台(http://www.juhe.cn/)注册,申请相关数据. 2.下载聚合数据SDK,将开发包里的juhe_sdk_ ...

  3. 一种调用dll的巧妙方法

    直接上代码,后面说应用场景 新建一个项目,引入需要调用的dll,如下 class Program { [DllImport( "soft.dll" )] static extern ...

  4. python解释器分类

    当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...

  5. Source Insight 4.0 破解和使用

    参考出处: https://blog.csdn.net/u011604775/article/details/81698062 https://blog.csdn.net/user11223344ab ...

  6. 有一个直方图,用一个整数数组表示,其中每列的宽度为1,求所给直方图包含的最大矩形面积。比如,对于直方图[2,7,9,4],它所包含的最大矩形的面积为14(即[7,9]包涵的7x2的矩形)。给定一个直方图A及它的总宽度n,请返回最大矩形面积。保证直方图宽度小于等于500。保证结果在int范围内。

    // ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...

  7. android 在githup中的资源整理(转)

    1.Github开源Android组件资源整理(一) 个性化控件(View) 2.Github开源Android组件资源整理(二)ActionBar和Menu 3. Github开源Android组件 ...

  8. PowerBuilder -- 数字金额大写

    //==================================================================== // 事件: .pub_fc_change_number( ...

  9. leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  10. HDU 5379 Mahjong tree(树的遍历&amp;组合数学)

    本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...