网上题解比较少,自己比较弱研究了半天(已经过了),希望对找题解的人有帮助

题目链接:https://codeforc.es/contest/1201/problem/D

题意: 给你一个矩形,起始点在(1,1),在给定坐标有宝物,你要将整个图中的宝物全部拿到,而且你不能向下走(左右随意),而且只有在所给出的列上你才能向上走,问最少要走多少格

#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>
#include <bitset>
//#include <map>
//#include<unordered_map> https://codeforc.es/contest/1201/problem/D
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
const long long INF=(1LL<<);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)2e5+; int dis(int a,int b)
{
return abss(a-b);
}
int L[N],R[N],LV[N],RV[N];
struct node
{
int x,y;
friend bool operator<(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
}_[N];
int is[N]; int main()
{
int n,m,k,q,n_=;
sc("%d%d%d%d",&n,&m,&k,&q);
fo(i,,k)sc("%d%d",&_[i].y,&_[i].x);//题目中取完一行只可能停留在头和尾
sort(_+,_++k); //下一行进行转移就行了
fo(i,,k)
{
int tx=_[i].x,ty=_[i].y;
n_=maxx(n_,ty);
if(!LV[ty])LV[ty]=tx; //预处理
else LV[ty]=minn(tx,LV[ty]);//宝藏的最左
if(!RV[ty])RV[ty]=tx;
else RV[ty]=maxx(tx,RV[ty]);//宝藏的最右
}
fo(i,,q)sc("%d",&is[i]),L[is[i]]=R[is[i]]=is[i];
sort(is+,is++q); //预处理
fo(i,,m)if(!L[i])L[i]=L[i-];//左邻近的安全列
fr(i,m,)if(!R[i])R[i]=R[i+];//右邻近的安全列
int posl=,posr=;
long long ans=,preresl=,preresr=;
fo(i,,n_)
{
if(i==)
{
if(RV[i])
ans=dis(posl,RV[i]),posl=posr=RV[i];
preresl=preresr=ans;
}
else
{
preresl++;preresr++;//除了第一行其他只要上升了,用于转移的左右停留状态都要+1;
if(LV[i]==&&RV[i]==)
continue;
long long resl=INF,resr=INF;
if(L[posl])resl=min(resl,preresl+dis(posl,L[posl])+dis(L[posl],RV[i])+dis(RV[i],LV[i]));
if(R[posl])resl=min(resl,preresl+dis(posl,R[posl])+dis(R[posl],RV[i])+dis(RV[i],LV[i]));
if(L[posr])resl=min(resl,preresr+dis(posr,L[posr])+dis(L[posr],RV[i])+dis(RV[i],LV[i]));
if(R[posr])resl=min(resl,preresr+dis(posr,R[posr])+dis(R[posr],RV[i])+dis(RV[i],LV[i]));
//进行转移;
if(L[posl])resr=min(resr,preresl+dis(posl,L[posl])+dis(L[posl],LV[i])+dis(RV[i],LV[i]));
if(R[posl])resr=min(resr,preresl+dis(posl,R[posl])+dis(R[posl],LV[i])+dis(RV[i],LV[i]));
if(L[posr])resr=min(resr,preresr+dis(posr,L[posr])+dis(L[posr],LV[i])+dis(RV[i],LV[i]));
if(R[posr])resr=min(resr,preresr+dis(posr,R[posr])+dis(R[posr],LV[i])+dis(RV[i],LV[i]));
ans=min(resl,resr);
preresl=resl;
preresr=resr;
posl=LV[i];
posr=RV[i];
}
}
pr("%lld\n",ans);
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

最新文章

  1. Android Touch事件分发机制学习
  2. (转)对《30个提高Web程序执行效率的好经验》的理解
  3. Sublime Text 3前端开发常用优秀插件介绍
  4. 重装Windows系统后,Linux系统启动引导失败
  5. Item 表单页面的 Select2 相关业务逻辑
  6. MySQL table_id原理及风险分析
  7. mysq优化
  8. C#语言入门详解(002)
  9. iOS之 LLDB调试常用命令
  10. 还在使用SimpleDateFormat?你的项目崩没?
  11. [Luogu4916]魔力环[Burnside引理、组合计数、容斥]
  12. Tcp Udp发送包的大小限制问题
  13. IDEA破解教程
  14. 小程序flex容器
  15. SQL去除空格、截取数据的方法:trim、substring
  16. Pairs Forming LCM LightOJ - 1236 (算术基本定理)
  17. Study 5 —— CSS概述
  18. 分析SignalTap的仿真结果
  19. 时间序列分析工具箱——sweep
  20. (5keras自带的模型之间的关系)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

热门文章

  1. 当前主流的小型嵌入式 GUI
  2. Oracle数据库-primary key/foreign key和references关系
  3. Matlab基础:关于图像的基本操作
  4. Android:状态栏禁用时蓝牙多文件传输弹窗及进度显示
  5. QML渐变色
  6. Qt编写自定义控件38-高亮按钮
  7. WebDriver介绍
  8. python 中的一些基础算法:递归/冒泡/选择/插入
  9. DN创建
  10. python调用shell命令