XSY3244 10.31 D

题意:

​ 数轴上有\(N\)只老鼠\(M\)个洞,每个洞有一个容量,求所有老鼠进洞的最小代价。(\(N,M\leq1000000\),时限\(2s\))

题解:

​ 被代爷的前两道题卡得醉生梦死,场上根本没看这题。。。

​ 十万的档显然可以\(dp\),加个线段树什么的就可以了。

​ 对于100%的数据,上面的那个\(dp\)已经没用了,代爷给的做法是正反贪心,让每只老鼠贪心选择左/右最近的洞。对于每只老鼠,这两个洞正好是它最终决策会去的洞。然后把老鼠和每一个可能有老鼠进的洞一起摆在数轴上,做一遍\(dp\)。状态\(f_{i,j}\)表示搞到第\(i\)个点,老鼠比洞多\(j\)个,转移挺简单的。这个\(dp\)相较上面的那个,优点在于它可以直接利用指针跳\(O(1)\)转移。时空复杂度都是线性的。

​ 场上还有人想出了另一种做法。这是我代码的做法。

​ 我们建出一个模型:

  • 对于老鼠和洞,我们分别用两个大根堆\(M,H\)储存,按位置。

  • 将老鼠和洞按位置排序。

  • 如果当前处理的是老鼠,那就取\(H\)堆顶,钦定它进这个洞,这个容量\(-1\),并且在以老鼠为镜面,这个洞反射过去的地方往\(M\)堆里插入一只老鼠,更新答案。

  • 如果当前处理的是洞,那就让\(M\)堆中所有坐标比它大的老鼠滚进这个洞(显然比老鼠在当前洞内更优),但是这不一定最优,所以我们要建立反悔机制:记\(\Delta = 当前洞的坐标-这只老鼠的坐标\),在\(当前洞坐标当前洞坐标+ \Delta\)的地方新建容量为一的洞;更新答案,如果处理完所有之后当前洞还有剩余的容量,就把它扔进堆里。想一想,为什么。

    ​代码:

    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define fo(i,l,r) for(int i=l;i<=r;i++)
    #define of(i,l,r) for(int i=l;i>=r;i--)
    #define fe(i,u) for(int i=head[u];i;i=e[i].next)
    using namespace std;
    typedef long long ll;
    typedef pair<ll,int> pli;
    #define P(a,b) make_pair(a,b)
    inline void open(const char *s)
    {
    #ifndef ONLINE_JUDGE
    char str[20];
    sprintf(str,"in%s.txt",s);
    freopen(str,"r",stdin);
    // sprintf(str,"out%s.txt",s);
    // freopen(str,"w",stdout);
    #endif
    }
    inline ll rd()
    {
    static ll x,f;
    x=0;f=1;
    char ch=getchar();
    for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1ll;
    for(;ch>='0'&&ch<='9';ch=getchar())x=x*10ll+ch-'0';
    return f>0?x:-x;
    }
    const int N=1000010;
    int n,m;
    ll ans=0;
    pli a[N<<1];
    priority_queue<ll>M;
    priority_queue<pli>H; inline void gaoM(pli x)
    {
    ll res=1000000000000000ll;
    if(!H.empty()){
    pli y=H.top();H.pop();
    res=x.first-y.first;
    if(--y.second)H.push(y);
    }
    M.push(x.first+res);
    ans+=res;
    }
    inline void gaoH(pli x)
    {
    while(x.second&&!M.empty()&&M.top()>x.first){
    ll y=M.top();M.pop();
    ll res=x.first-y;x.second--;
    ans+=res;H.push(P(x.first+res,1));
    }
    if(x.second)H.push(x);
    } int main()
    {
    open("c");
    n=rd();m=rd();
    fo(i,1,n)a[i].first=rd(),a[i].second=-1;
    ll s=0;
    fo(i,1,m)a[n+i].first=rd(),a[n+i].second=rd(),s+=a[n+i].second;
    if(s<n)return puts("-1"),0;
    sort(a+1,a+n+m+1);
    fo(i,1,n+m){
    if(a[i].second==-1)gaoM(a[i]);
    else gaoH(a[i]);
    }
    printf("%lld\n",ans);
    return 0;
    }

XSY3244 10.31 D的更多相关文章

  1. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  2. Contest - 2014 SWJTU ACM 手速测试赛(2014.10.31)

    题目列表: 2146 Problem A [手速]阔绰的Dim 2147 Problem B [手速]颓废的Dim 2148 Problem C [手速]我的滑板鞋 2149 Problem D [手 ...

  3. 17.10.31&11.01

    10.31模拟考试 Prob.1(AC)裸的矩阵幂 Prob.2(WA)(类似括号匹配求合法方案数) 卡特兰数的一个模型运用.可以推出一个式子(推导方法一个erge讲的,一个骚猪讲的) Prob.3( ...

  4. 10.31 正睿停课训练 Day13

    目录 2018.10.31 正睿停课训练 Day13 A Poker(期望) B Label(高斯消元) C Coin(二分图染色 博弈) 考试代码 A(打表) B 2018.10.31 正睿停课训练 ...

  5. [软件工程基础]2017.10.31 第四次 Scrum 会议

    具体事项 项目交接燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #7 掌握 PHP:#6 阅读分析 PhyLab 数据处理相关代码 #10 搭建可用的开发测试环境: ...

  6. Cheatsheet: 2016 10.01 ~ 10.31

    Docker Introduction to Docker Monitoring Database MongoDB: The Good, The Bad, and The Ugly Web 4 Key ...

  7. Cheatsheet: 2015 10.01 ~ 10.31

    .NET Publishing your ASP.NET App to Linux in 5 minutes with Docker Integrating AngularJS with ASP.NE ...

  8. Sixth scrum meeting - 2015/10/31

    概述 今天是周末,我们小组由于之前拖延的比较久,所以今天仍然在努力的开发…… 目前开发已经到了中期阶段,今天遇到了一个问题就是,由于小组的某些同学对git的使用不太熟悉,导致在git push的时候遇 ...

  9. 10.31 afternoon

    巧克力棒(chocolate)Time Limit:1000ms Memory Limit:64MB题目描述LYK 找到了一根巧克力棒,但是这根巧克力棒太长了, LYK 无法一口吞进去.具体地,这根巧 ...

随机推荐

  1. js001 ---- async

    Node.js异步流,详细见https://caolan.github.io/async/docs.html#parallel 1, async 用的比较多的是 waterfall, 瀑布流, 就是每 ...

  2. CSU 1364 Interview RMQ

    题意: 瑶瑶有一家有一家公司,最近他想招m个人.因为他的公司是如此的出名,所以有n个人来参加面试.然而,瑶瑶是如此忙,以至于没有时间来亲自面试他们.所以他准备选择m场面试来测试他们. 瑶瑶决定这样来安 ...

  3. ArcGIS api for javascript——1,2,3综合

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  4. [Poi] Customize Babel to Build a React App with Poi

    Developing React with Poi is as easy as adding the babel-preset-react-appto a .babelrc and installin ...

  5. [Recompose] Stream a React Component from an Ajax Request with RxJS

    Loading data using RxJS is simple using Observable.ajax. This lesson shows you how to take the ajax ...

  6. vue21 slot占位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. android CoordinatorLayout使用

    一.CoordinatorLayout有什么作用 CoordinatorLayout作为“super-powered FrameLayout”基本实现两个功能: 1.作为顶层布局 2.调度协调子布局 ...

  8. legend---七、jquery如何选中select的selected的选择上的自定义属性

    legend---七.jquery如何选中select的selected的选择上的自定义属性 一.总结 一句话总结:用冒号属性选择器 var type=$(this).children('option ...

  9. CentOS下安装C/C++开发工具包的最佳方式

    如果你使用的是 Fedora, Red Hat, CentOS, 或者 Scientific Linux 系统,使用下面的命令安装GNU的C/C++开发包和编译器. # yum groupinstal ...

  10. vmware-images

    https://virtualboxes.org/images/centos/ https://www.osboxes.org/vmware-images/