经典的贪心模型,常规思路:将M和B排序即可

看到没有人用优先队列,于是我的showtime到了

说下思路:

  • 读入时将数加入啊a,b堆中,不用处理(二叉堆本来就有有序的性质)

  • 读完后逐个判断,照题目模拟即可

  • 总时间复杂度:O(nlogn) 其实就是堆排序的时间复杂度

楼下的那位用了sort还说是O(2n)的,大家不要犯这种低级错误

贴代码:

#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#pragma GCC optimize(2)//STL必带
using namespace std;
#define ll long long
const int maxn=0x7f;
const int INF=0x7fffffff;
inline void read(int&x){//quick input
    int data=0,w=1;
    char ch=getchar();
    while(ch!='-'&&!isdigit(ch))
        ch=getchar();
    if(ch=='-')
        w=-1,ch=getchar();
    while(isdigit(ch))
        data=10*data+ch-'0',ch=getchar();
    x=data*w;
}
void write(int x){//quick output
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar('0'+x%10);
}
priority_queue<int> a,b;
int main()
{
//    freopen(".in","r",stdin);
//    freopen(".out","w",stdout);
    int n,x,y,t;
    read(n);read(x);read(y);
    for(int i=1;i<=n;++i){//use variable "t" to improve memory and time
        read(t);
        a.push(t);
        read(t);
        b.push(t);
    }
    int ans=0,u,v;
    while(!a.empty()){//slower than "sort", easier to understand though
        u=a.top();
        a.pop();
        v=b.top();
        b.pop();
        if(u<v)//do as  what the problem said
            ans+=(v-u)*x;
        else if(u>v)
            ans+=(u-v)*y;
    }
    write(ans);
//    fclose(stdin);
//    fclose(stdout);
    return 0;
}

LG2945 【[USACO09MAR]沙堡Sand Castle】的更多相关文章

  1. 洛谷 P2945 [USACO09MAR]沙堡Sand Castle 题解

    题目传送门 大概思路就是把这两个数组排序.在扫描一次,判断大小,累加ans. #include<bits/stdc++.h> using namespace std; int x,y,z; ...

  2. 洛谷 P2945 [USACO09MAR]沙堡Sand Castle

    传送门 题目大意: ai,ai+1,ai+2... 变成 bi,bi+1,bi+2.. 不计顺序,增加和减少a数组均有代价. 题解:贪心+排序 小的对应小的 代码: #include<iostr ...

  3. 洛谷 - P2945 - 沙堡Sand Castle - 排序

    https://www.luogu.org/problemnew/show/P2945 好像猜一猜就觉得排序之后是最优的,懒得证明了.每个城墙向他最接近的城墙靠近,绝对是最优的.

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

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

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

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

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

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

  7. [USACO09MAR]Sand Castle

    嘟嘟嘟 太水了,大佬们就绕道吧…… 就是m, b数组分别排个序,然后更改对应位置的m[i]和b[i],就行了. 因为如果m[i]不改为b[i]而是b[i + 1]的话,那么必定要将m[j] (j &g ...

  8. 随手练—— 洛谷-P2945 Sand Castle(贪心)

    题目链接:https://www.luogu.org/problemnew/show/P2945 (原题 USACO) 要求钱最少,就是试着让M和B的离散程度最小(我自己脑补的,就是总体更接近,我不知 ...

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

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

随机推荐

  1. 解决Word 2013, Word 2016的保存太慢的问题

    尝试下面步骤: 方法 一:文件〉选项〉高级〉,保存,关闭“允许后台保存”选项. 提示:禁止该项功能可能存在风险, 请及时保存文件. 方法二:禁用 Office  中的硬件加速 1.启动任一 Offic ...

  2. YII之yiic创建YII应用

    yii提供了强大的命令行工具来快速的创建相关组件和应用.下面就来讲解用yiic工具快速创建yii应用我的web目录在 d:\www下 yiiframework在 D:\www\yii\framewor ...

  3. 敏感性、特异性、假阳性、假阴性(sensitivity and specificity)

    医学.机器学习等等,在统计结果时时长会用到这两个指标来说明数据的特性. 定义 敏感性:在金标准判断有病(阳性)人群中,检测出阳性的几率.真阳性.(检测出确实有病的能力) 特异性:在金标准判断无病(阴性 ...

  4. WPF:改变ListBoxItem和ListViewItem的颜色

    目录 1. 改变ListBoxItem颜色 2. ListViewItem的颜色设置 注意: 本文仅讨论默认ListBoxItem和ListViewItem的鼠标指向和被选择后的前景和背景颜色设置.如 ...

  5. 新概念 Lesson 1 Excuse me!

    xu言: 从哪里跌倒,就从哪里爬起来.希望这次真的能够坚持下去... standard  ['stændəd]    pronunciation [prə,nʌnsɪ'eɪʃ(ə)n] basic   ...

  6. 启动Eclipse时发生An internal error occurred during: "Initializing Java Tooling"错误

    详细提示如下: An internal error occurred during: "Initializing Java Tooling". Illegal exception ...

  7. 简话Angular 07 Angular config-run-service-factory-provider-constant-value

    一句话: 它们Angular框架声明周期的各个阶段,常规约定各专注于特定功能,经过处理也可以互相替换 1.功能细分简解 config Angular module模块的加载阶段-应用在此时还没有启动 ...

  8. close_on_exec标志位的作用

    转自:http://blog.csdn.net/sunlylorn/article/details/6363727 close_on_exec 是一个进程所有文件描述符(文件句柄)的位图标志,每个比特 ...

  9. spring boot 学习(七)小工具篇:表单重复提交

    注解 + 拦截器:解决表单重复提交 前言 学习 Spring Boot 中,我想将我在项目中添加几个我在 SpringMVC 框架中常用的工具类(主要都是涉及到 Spring AOP 部分知识).比如 ...

  10. hadoop hadoop install (1)

    vmuser@vmuser-VirtualBox:~$ sudo useradd -m hadoop -s /bin/bash[sudo] vmuser 的密码: vmuser@vmuser-Virt ...