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

题目链接: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;
}

矩阵拿宝物--Codeforces 1201D - Treasure Hunting Codeforces Round #577 (Div. 2)的更多相关文章

  1. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  2. Codeforces 1201D. Treasure Hunting

    传送门 看一眼感觉就是 $dp$,但是似乎状态太多了 考虑推推性质 首先每到一行都要把所有宝藏都走到,那么一定会走到最左边的和最右边的宝藏 注意到一旦走完所有宝藏时肯定是在最左边或者最右边的宝藏位置 ...

  3. Codeforces Round #577 (Div. 2) D. Treasure Hunting

    Codeforces Round #577 (Div. 2)  D. Treasure Hunting 这个一场div2 前面三题特别简单,这个D题的dp还是比较难的,不过题目告诉你了只能往上走,所以 ...

  4. Codeforces Round #577 (Div 2)

    A. Important Exam 水题 #include<iostream> #include<string.h> #include<algorithm> #in ...

  5. Codeforces Round #577 (Div. 2) 题解

    比赛链接:https://codeforc.es/contest/1201 A. Important Exam 题意:有\(n\)个人,每个人给出\(m\)个答案,每个答案都有一个分值\(a_i\), ...

  6. Codeforces Round #577 (Div. 2) C. Maximum Median (模拟,中位数)

    题意:给你一个长度为奇数\(n\)的序列.你可以对任意元素加上\(k\)次\(1\),求操作后的中位数最大. 题解:先对序列进行排序,然后对中位数相加,如果中位数和后面的元素相等,就对后面所有和当前中 ...

  7. Codeforces Round #577 (Div. 2) C. Maximum Median

    题意:就是给一n(奇数)个元素数组,可以对它的元素执行k次+1操作,递增排序,求中位数最大是多少. 那我们在排完序之后,中位数前的元素可以不管它,只要对中位数后的操作就行,我们要判断和中位数相等的元素 ...

  8. [Codeforces 1214D]Treasure Island(dfs)

    [Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. FOI冬令营 Day 3

    目录 T1.签到题(sort) 传送门 Code T2.送分题(queue) 传送门 Code T3.简单题(game) 传送门 Code 咕咕咕 T1.签到题(sort) 传送门 原题:LOJ 27 ...

  2. ArcGIS数据建模 (模型构建器modelbuilder) 培训视频 5章28小节587分钟视频 51GIS网站上线

    网址:http://www.51gis.com.cn/kecheng.html?id=358

  3. C#中 Dictionary<>的使用及注意事项

    1,如果在主体代码中使用,直接在初始化中生成就行 2如果在其他层,比如逻辑层,要注意在事件内部定义,在外部的话,重复调用就会提示“”“已经定义了相同的KEY”,见例子 (例子是转的) Dictiona ...

  4. C# 怎么生成DLL文件(转)

    有两种方法:     但是一般这个使用     打开VS2008,依次点击:菜单->文件->新建项目->项目类型visual C#(这里假设为该项目所取的名字是DllBuild)-& ...

  5. 打印变量地址-0x%08x

    地址是8个16进制数. 1.8个16进制数:相当于32个二进制数.4G内存刚好可以用32位的二进制表示出来.2.因为变量或函数等等在运行时都是存储在内存中的,所以你用取地址符当然是取出计算机内存中的地 ...

  6. CentOS7下搭建zabbix监控(二)——Zabbix被监控端配置

    Zabbix监控端配置请查看:CentOS7下搭建zabbix监控(一)——Zabbix监控端配置 (1).在CentOS7(被监控端)上部署Zabbix Agent 主机名:youxi2 IP地址: ...

  7. 123457123456#0#-----com.threeapp.ErTongPaint01-----儿童宝宝绘画涂鸦游戏02

    com.threeapp.ErTongPaint01-----儿童宝宝绘画涂鸦游戏02

  8. Oracle SQL 脚本跟踪

    NC Oracle SQL 脚本跟踪 脚本: select * from v$sqlarea a and a.LAST_ACTIVE_TIME >= to_date( '2013-02-21 1 ...

  9. 使用 Sublime + PlantUML 高效地画图

    转自 http://www.jianshu.com/p/e92a52770832

  10. java、ruby、python、php等如何生成excel文档?

    excel在我们日常工作生活中会经常用到,通常我们都是用office软件去编写文档.但是对于格式一致的excel文档,如果还是使用人工完成,那绝不是我们软件工程师的姿态了~ 下面我就介绍一种方法,不需 ...