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. python语法root=Tkinter.Tk()

    1. Tkinter 是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口.Tkinter不是唯一的python图形编程接口,但是是 其中比较流行的一个.最大的特点是 ...

  2. CC3200在AP模式的TCP sock作为客户端连接时返回SL_ECONNREFUSED(-111) Connection refused

    1. CC3200处于AP模式(电脑无线连接CC3200的WIFI信号),开启一个TCP socket,这个socket作为TCP客户端去连接TCP服务器端 struct sockaddr_in ad ...

  3. Windows Phone 图片扩展类

    using System.IO; using System.Text; using System.Net; using System.Threading.Tasks; using System.Win ...

  4. http协议--留

    1.http消息结构 *http客户端,即web浏览器,链接到服务器,向服务器发送一个http请求的目的 *http服务器,即web服务,接受请求,并向客户端发送http响应数据 http统一资源标识 ...

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

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

  6. Android记事本08

    昨天: Anr问题异常的原因和解决方案. 今天: Activity数据传递之通用方式. Activity数据传递之静态变量. Activity数据传递之全局变量. 遇到的问题: 无.

  7. pytorch:EDSR 生成训练数据的方法

    Pytorch:EDSR 生成训练数据的方法 引言 Winter is coming 正文 pytorch提供的DataLoader 是用来包装你的数据的工具. 所以你要将自己的 (numpy arr ...

  8. PAT 1050 螺旋矩阵

    https://pintia.cn/problem-sets/994805260223102976/problems/994805275146436608 本题要求将给定的 N 个正整数按非递增的顺序 ...

  9. Java进行身份证格式强校验(准)

    最近做了一个系统,涉及到对用户输入的身份证号进行校验,减少脏数据传入后台处理并降低企业验证成本,因此在接入层便对输入信息做格式强校验. 直接附上代码,可直接使用. package hope.ident ...

  10. [bzoj4361] isn [树状数组+dp+容斥原理]

    题面 传送门 思路 首先,本题目的核心元素是非降子序列,而显然这个题目中的子序列只和序列的长度.位置,以及互相之间的包含关系,这些东西相关 所以我们可以依据这些先"猜"(实际上是估 ...