链接: https://codeforces.com/contest/1201/problem/D
题面:
You are on the island which can be represented as a n×m table. The rows are numbered from 1 to n and the columns are numbered from 1 to m. There are k treasures on the island, the i-th of them is located at the position (ri,ci).

Initially you stand at the lower left corner of the island, at the position (1,1). If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from (r,c) to (r+1,c)), left (from (r,c) to (r,c−1)), or right (from position (r,c) to (r,c+1)). Because of the traps, you can’t move down.

However, moving up is also risky. You can move up only if you are in a safe column. There are q safe columns: b1,b2,…,bq. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.

Input
The first line contains integers n, m, k and q (2≤n,m,k,q≤2e5, q≤m) — the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.

Each of the next k lines contains two integers ri,ci, (1≤ri≤n, 1≤ci≤m) — the coordinates of the cell with a treasure. All treasures are located in distinct cells.

The last line contains q distinct integers b1,b2,…,bq (1≤bi≤m) — the indices of safe columns.

Output
Print the minimum number of moves required to collect all the treasures.
题意: 给你一个矩形,起始点在(1,1),在一些格子里有宝物,你需要将整个图中的宝物全部收集起来,而且你不能向下走,而且只有在所给出的列上你才能向上走,问最少要走多少格
思路: 由于数据过大,不可能将整个图开出来
所以我们可以预处理每一行的所在宝物的最左边与最右边,然后进行由下往上dp,上行的点由下行的所在宝物的最左边与最右边出发得到

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=2e5+5;
ll n,m,k,q,x,y,a[N][2],dp[N][2],b[N];
ll sl(int j,int u,int i,int v)
{ll sum=INF,sum1=INF;ll p=lower_bound(b+1,b+1+q,a[j][u])-b;if(p<=q)sum=abs(a[j][u]-b[p])+abs(a[i][v]-a[i][v^1])+abs(b[p]-a[i][v^1]);p=upper_bound(b+1,b+1+q,a[j][u])-b-1;if(p)sum1=abs(a[j][u]-b[p])+abs(a[i][v]-a[i][v^1])+abs(b[p]-a[i][v^1]);return min(sum1,sum);
}
int main()
{scanf("%lld%lld%lld%lld",&n,&m,&k,&q);for(int i=1;i<=n;i++){a[i][0]=INF;a[i][1]=-INF;dp[i][0]=dp[i][1]=(1ll<<60);}for(int i=1;i<=k;i++){scanf("%d%d",&x,&y);a[x][0]=min(y,a[x][0]);a[x][1]=max(y,a[x][1]);}a[1][0]=1,a[1][1]=max(a[1][1],1ll);for(int i=1;i<=q;i++)scanf("%lld",&b[i]);sort(b+1,b+1+q);int j=1;dp[1][0]=abs(a[1][1]-1)+abs(a[1][1]-a[1][0]),dp[1][1]=abs(a[1][1]-1);for(int i=2;i<=n;i++){if(a[i][0]==INF)continue;for(int v=0;v<2;v++){for(int u=0;u<2;u++){dp[i][v]=min(dp[i][v],dp[j][u]+sl(j,u,i,v)+i-j);}}j=i;}printf("%lld\n",min(dp[j][0],dp[j][1]));return 0;
}

Treasure Hunting Codeforces Round #577 (Div. 2)相关推荐

  1. Codeforces Round #577 (Div. 2)--B. Zero Array

    菜鸡我认为的好题 题意:这里有n个数,两两相互减1,执行n次,问有没有可能这n个数全都变为0; 做法:首先和是奇数是不可能的,然后和为偶数的时候:比如数据 2 2 6 那么结果是不可以的,变 -> ...

  2. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  3. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  4. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  5. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  6. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  7. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  8. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

  9. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

最新文章

  1. 图片按钮ImageButton
  2. vue图片点击超链接_vue使用v-for实现hover点击效果
  3. 未来的计算机作文2000字,未来的模样2000字作文
  4. boost::asio::ip::tcp用法的测试程序
  5. iOS socket 套接字编程
  6. 开源WPF控件库MaterialDesignInXAML推荐
  7. ASP.NET CORE 微服务(简化版)实战系列-没有比这性价比再高的实战课程了
  8. 没错,继事理图谱后,我们又搞事情了:数地工场自然语言处理语义开放平台正式对外发布!
  9. 英语四六级听力考试选项技巧
  10. Nginx启动报错误unlink() “nginx.pid” failed (2: No such file or directory)
  11. Q106:Linux系统下安装编译PBRT-V3
  12. 软件基本功:数组赋值,一定要对齐
  13. c++builder excel 插入分页符
  14. 9.STC15W408AS单片机EEPROM
  15. == 人工智能和机器学习 AIML ==
  16. 独孤求败-小滴云架构大课十八式-xdclass2022
  17. mib文件在服务器的什么位置,MIB文件简单分析
  18. 《计算机网络教程》(微课版 第五版)第五章 运输层 课后习题及答案
  19. java重复执行方法_重复java方法
  20. github之处理“忒修斯之船”问题

热门文章

  1. 赛门铁克10月份智能安全分析报告
  2. 棒棒糖球球机器人_球球大作战刷棒棒糖
  3. ajax 实验报告,AJAX实验报告 (4500字).doc
  4. python解决数据框中添加一行或者一列(DataFrame的行列处理)
  5. 冰冻三尺,非一日之寒。数据解析——正则解析(1)
  6. 你无法让一个瞎子理解颜色的多彩
  7. python 在linux中把doc转换为docx格式文件(支持word97和word2003)
  8. 2020年秋招联发科小米等20家公司面经总结
  9. POI导出数据至Excel,cpu飙升 cpu占用很高,原因排查
  10. python读取excel【二】,循环行与列对应数据