AC日记——Mice and Holes codeforces 797f
思路:
代码:
#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的更多相关文章
- Mice and Holes CodeForces - 797F
Mice and Holes CodeForces - 797F 题意:有n只老鼠和m个洞,都在一个数轴上,老鼠坐标为x[1],...,x[n],洞的坐标为p[1],...,p[m],每个洞能容纳的老 ...
- AC日记——Dynamic Problem Scoring codeforces 807d
Dynamic Problem Scoring 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <io ...
- 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 ...
- AC日记——Sagheer, the Hausmeister codeforces 812b
812B - Sagheer, the Hausmeister 思路: 搜索: 代码: #include <cstdio> #include <cstring> #includ ...
- AC日记——Sagheer and Crossroads codeforces 812a
812A - Sagheer and Crossroads 思路: 模拟: 代码: #include <cstdio> #include <cstring> #include ...
- AC日记——Is it rated? codeforces 807a
Is it rated? 思路: 水题: 代码: #include <cstdio> #include <cstring> using namespace std; ],b[] ...
- Ac日记——Distances to Zero codeforces 803b
803B - Distances to Zero 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <i ...
- AC日记——Roma and Poker codeforces 803e
803E - Roma and Poker 思路: 赢或输或者平的序列: 赢和平的差的绝对值不得超过k: 结束时差的绝对值必须为k: 当“?”时可以自己决定为什么状态: 输出最终序列或者NO: dp( ...
- AC日记——Periodic RMQ Problem codeforces 803G
G - Periodic RMQ Problem 思路: 题目给一段序列,然后序列复制很多次: 维护序列很多次后的性质: 线段树动态开点: 来,上代码: #include <cstdio> ...
随机推荐
- Eclipse Java 构建路径 ---Eclipse教程第13课
Eclipse Java 构建路径 设置 Java 构建路径 Java构建路径用于在编译Java项目时找到依赖的类,包括以下几项: 源码包 项目相关的 jar 包及类文件 项目引用的的类库 我们可以通 ...
- 不想停机?快使用Windows Server 2016滚动升级
[TechTarget中国原创] Windows Server环境中的关键任务工作量总是密集的.工作量会主要集中在Hyper-V部署集群.硬件故障转移集群或者外来集群的顶部.集群中常用的表格都是基于F ...
- webdriver--设置元素等待
sleep():脚本执行到某一位置时“睡一会”,再继续执行:参数的单位是s:sleep方法由python的time模块提供,有两种引入和使用方式 import time time.sleep(5) f ...
- 获取访问者ip及其所在城市
原本使用新浪Api,然后发现不行了,以下小编重新查找了几个,个人推荐太平洋的接口 1.首先获取真实ip $ip = $_SERVER['REMOTE_ADDR']; 2.要知道的Api接口 几个接口$ ...
- Linux认知之旅【04 进一步了解目录】!
一.目录是什么? 二.不得不提的文件系统! 三.绝对路经,相对路径
- Python全栈工程师(列表、拷贝)
ParisGabriel 感谢 大家的支持 你们的阅读评价就是我最好的更新动力 我会坚持吧排版做的越来越好 每天坚持 一天一篇 点个订阅吧 灰常感谢 当个死粉也阔以 Py ...
- 【多线程学习(1)】创建java多线程
1)java多线程的创建方式有三种: 1.继承Thread类 2.实现Runnable接口 3.实现Callable接口 第一种: //继承Thread类 class ExtendsThread ex ...
- Kd-Tree&Ransac笔记
关于sift资源总结: http://blog.csdn.net/masibuaa/article/details/9191309 两个比较好的资源: https://my.oschina.net/k ...
- WMware给centos6.8虚拟机添加硬盘
背景 用WMware运行系统经常遇见系统磁盘不够用的情况,通常解决这个问题有两种方式: 1) 给现有磁盘扩容: 2) 给虚拟机添加一块虚拟硬盘: 磁盘扩容我还没试验成功,这里我先把给虚拟机添加一块硬盘 ...
- aes加密码
js地址 https://github.com/yves8888/crypto-js 下面src<!DOCTYPE html> <html lang="en"&g ...