797F - Mice and Holes

思路:

  XXYXX

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 5005
#define ll long long struct HoleType {
ll x,c;
};
struct HoleType hole[maxn]; ll n,m,mice[maxn],s[maxn],sum,dp[maxn][maxn],que[maxn]; inline void in(ll &now)
{
ll if_z=;now=;
char Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} bool cmp(HoleType aa,HoleType bb)
{
return aa.x<bb.x;
} int main()
{
in(n),in(m);
for(ll i=;i<=n;i++) in(mice[i]);
for(ll i=;i<=m;i++) in(hole[i].x),in(hole[i].c),sum+=hole[i].c;
if(sum<n)
{
printf("-1\n");
return ;
}
sort(mice+,mice+n+),sort(hole+,hole+m+,cmp);
memset(dp,0x7f,sizeof(dp)),dp[][]=;
for(ll i=;i<=m;i++)
{
ll head=,tail=-;
for(ll j=;j<=n;j++) s[j]=s[j-]+abs(hole[i].x-mice[j]);
for(ll j=;j<=n;j++)
{
while(head<=tail&&que[head]<j-hole[i].c) head++;
while(tail>=head&&dp[i-][j]-s[j]<=dp[i][que[tail]]-s[que[tail]]) tail--;
que[++tail]=j,dp[i][j]=dp[i-][que[head]]-s[que[head]]+s[j];
}
}
cout<<dp[m][n];
return ;
}

AC日记——Mice and Holes codeforces 797f的更多相关文章

  1. Mice and Holes CodeForces - 797F

    Mice and Holes CodeForces - 797F 题意:有n只老鼠和m个洞,都在一个数轴上,老鼠坐标为x[1],...,x[n],洞的坐标为p[1],...,p[m],每个洞能容纳的老 ...

  2. AC日记——Dynamic Problem Scoring codeforces 807d

    Dynamic Problem Scoring 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <io ...

  3. AC日记——Sign on Fence Codeforces 484e

    E. Sign on Fence time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...

  4. AC日记——Sagheer, the Hausmeister codeforces 812b

    812B - Sagheer, the Hausmeister 思路: 搜索: 代码: #include <cstdio> #include <cstring> #includ ...

  5. AC日记——Sagheer and Crossroads codeforces 812a

    812A - Sagheer and Crossroads 思路: 模拟: 代码: #include <cstdio> #include <cstring> #include ...

  6. AC日记——Is it rated? codeforces 807a

    Is it rated? 思路: 水题: 代码: #include <cstdio> #include <cstring> using namespace std; ],b[] ...

  7. Ac日记——Distances to Zero codeforces 803b

    803B - Distances to Zero 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <i ...

  8. AC日记——Roma and Poker codeforces 803e

    803E - Roma and Poker 思路: 赢或输或者平的序列: 赢和平的差的绝对值不得超过k: 结束时差的绝对值必须为k: 当“?”时可以自己决定为什么状态: 输出最终序列或者NO: dp( ...

  9. AC日记——Periodic RMQ Problem codeforces 803G

    G - Periodic RMQ Problem 思路: 题目给一段序列,然后序列复制很多次: 维护序列很多次后的性质: 线段树动态开点: 来,上代码: #include <cstdio> ...

随机推荐

  1. 《Cracking the Coding Interview》——第7章:数学和概率论——题目1

    2014-03-20 01:57 题目:玩篮球投篮,有两种玩法:要么1投1中,要么3投两中.你单次投篮中的概率是p,那么对于不同的p,哪种玩法胜率更高? 解法:第一种总是胜率更高,可以列不等式算算,结 ...

  2. canda 常用命令

    1.获取版本 conda -V conda --version 2.获取帮助 conda -h conda --help 查看某一命令的帮助 conda install -h conda instal ...

  3. jenkins构建定时任务

    jenkins构建定时任务的主要两种形式 1.Build periodically:周期性构建项目,无论有没有代码更新到时间都会构建项目 2.Poll SCM:定时检查源码变更(根据SCM软件的版本号 ...

  4. 【转载】Unity3D研究院之IOS&Andoird使用Sharesdk遇到的坑

    这两天研究了一下ShareSDK,说实话挺好用的,但是还是有点坑的地方.那么雨松MOMO写下博文记录一下来我遇到的坑,嘿嘿. 大部分内容它的文档上已经说的很清楚了. http://wiki.share ...

  5. leetcode 149. 直线上最多的点数 解题报告

    给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +------- ...

  6. 椭圆曲线加密和rsa对比

    最近在导师的要求下接手了基于欧洲标准的车联网项目中的安全层,需要学习密码学,以及网络安全的相关内容,这里做一个总结 引用的大部分内容为一个西安的大佬(哈哈我老家也是西安的),大佬主页:https:// ...

  7. Python-map、filter、reduce方法

    介绍 1.map()函数,会让列表中每一个元素都执行一某个函数(传递1个参数), 并且将执行函数返回的结果(无论是什么结果)放在结果列表中 2.filter()函数,会让列表中的每一个元素都执行一次某 ...

  8. JavaWeb笔记(四)Cookie&Session

    Cookie 客户端会话技术,客户端保存,用于存储少量不太敏感的数据,在不登陆的情况下完成服务器对客户端的身份识别 简单使用步骤 创建Cookie对象,绑定数据 new Cookie(String n ...

  9. Dijkstra算法_最短路径_L2-001. 紧急救援

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  10. J2EE的十三个技术——EJB之概述

    含义: 企业级的JavaBeans(Enterprise JavaBean),其设计目标是部署分布式应用程序. EJB是J2EE的一部分,称为Java企业Bean,它把使用Java开发的服务器组件的部 ...