http://www.lydsy.com/JudgeOnline/problem.php?id=3399

贪心就是将两组排序,然后直接模拟即可。。

如果我们用a去匹配一个绝对值和它差不多的值,那么去匹配的那个位置显然要和它这个位置匹配,同样的高度差。。。

自己理解吧。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=25500;
int a[N], b[N], n, x, y, ans; int main() {
read(n); read(x); read(y);
for1(i, 1, n) read(a[i]), read(b[i]);
sort(a+1, a+1+n); sort(b+1, b+1+n);
for1(i, 1, n) if(a[i]>b[i]) ans+=(a[i]-b[i])*y; else ans+=(b[i]-a[i])*x;
print(ans);
return 0;
}

Description

约 翰用沙子建了一座城堡.正如所有城堡的城墙,这城墙也有许多枪眼,两个相邻枪眼中间那部分叫作“城齿”.    城墙上一共有N(1≤N≤25000)个 城齿,每一个都有一个高度Mi.(1≤尬≤100000).现在约翰想把城齿的高度调成某种顺序下的 Bi,B2,…,BN(I≤Bi≤100000). -个城齿每提高一个单位的高度,约翰需要X(I≤X≤100)元;每降低一个单位的高度,约翰需要 Y(1≤y≤100)元.    问约翰最少可用多少钱达到目的.数据保证答案不超过2^32.

Input

 
    第1行输入3个整数N,X,Y.
    第2到N+1行每行输入两个整数Mi和Bi.

Output

 
    最少花费.

Sample Input

3 6 5
3 1
1 2
1 2

Sample Output

11

HINT

第1个城齿降低1,第2个城齿提高1

Source

【BZOJ】3399: [Usaco2009 Mar]Sand Castle城堡(贪心)的更多相关文章

  1. bzoj 3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MB Description 约翰用沙子建了一座城堡.正 ...

  2. BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡:贪心【最小匹配代价】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3399 题意: 给你一个数列a,和一个可变换顺序的序列b(数列长度≤25000). a增加一 ...

  3. BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡(贪心)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3399 [题目大意] 将一个集合调整成另一个集合中的数,把一个数+1需要消耗x,-1需要 ...

  4. 3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 37  Solved: 32[Sub ...

  5. BZOJ3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 17[Sub ...

  6. DP经典 BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生

    BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 419  Solve ...

  7. BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )

    n <= 105 , 其实是10 ^ 5 ....坑...我一开始写了个模拟结果就 RE 了.. 发现这个后写了个单调栈就 A 了... ---------------------------- ...

  8. BZOJ 3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 动态规划

    3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=34 ...

  9. bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害

    1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 Description Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着 ...

随机推荐

  1. VS 一键调用 SVN Blame

    在Windows上做项目开发的时候,常常需要调用SVN Blame去追溯一个文件的编辑历史,但是这个常见的需求往往需要很繁琐的步骤.首先需要打开文件所在文件夹,然后右键,在一长排上下文菜单中准确地选中 ...

  2. VLC 源代码结构

    x64 sudo apt-get install -y liblua5.2-dev libmad0-dev libavutil-dev libavcodec-dev libavformat-dev l ...

  3. IPhone打包工具脚本

    (后面就是代码了,我就不翻译了.) #!/usr/bin/perl use File::Copy; my $installPath = $ARGV[]; #the name that displays ...

  4. 阻塞与非阻塞、同步与异步、I/O模型

    1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步/异步主要针对C端:  同步: 所谓同步,就是在c端 ...

  5. Introducing MVC

    PS:这本书感觉不怎么样,这么多低频词就倒人胃口... Suppose you'v recently launched a new web site, only to find that it's s ...

  6. maven的profile 目录、变量打包

    <project> <build> <finalName>maven-project</finalName> <resources> < ...

  7. phpexcel表的一些设置

    $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); //set default styles$objPHPE ...

  8. Mac /Ubuntu/Windows 下安装nodejs

    Mac If you're using the excellent homebrew package manager, you can install node with one command: b ...

  9. python-循环while

    while 只要…条件成立,就一直做…. for 循环会在可迭代的序列被穷尽的时候停止,while 则是在条件不成立的时候停止,因此 while 的作用概括成一句话就是:只要…条件成立,就一直做…. ...

  10. Nginx(一):linux下安装nginx与配置

    linux系统为Centos 64位 准备目录 [root@instance-3lm099to ~]# mkdir /usr/local/nginx [root@instance-3lm099to ~ ...