CodeForces 677D Vanya and Treasure
$dp$,树状数组。
很明显这是一个$DAG$上的$dp$,由于边太多,暴力$dp$会超时,需要优化。
例如计算$dp[x][y]$,可以将区域分成四块,$dp[x][y]$取四块中的最小值,每一块用一个二维树状数组维护最小值即可。
每次扩展一层需要一个新的树状数组,因为每次初始化树状数组会超时,所以可以额外开一个数组记录一下每一个点是第几次更新的。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int INF=0x7FFFFFFF;
const int maxn=;
int n,m,p,a[maxn][maxn],c[][maxn][maxn],d[][maxn][maxn],dp[maxn][maxn];
vector<int>v[maxn*maxn]; int lowbit(int x){return x&(-x);} int get(int op,int h,int x,int y)
{
int res=INF;
for(int i=x;i>;i=i-lowbit(i))
for(int j=y;j>;j=j-lowbit(j))
if(d[op][i][j]==h) res=min(res,c[op][i][j]);
return res;
} void update(int op,int h,int x,int y,int v)
{
for(int i=x;i<=n;i=i+lowbit(i))
for(int j=y;j<=m;j=j+lowbit(j))
{
if(d[op][i][j]!=h) c[op][i][j]=INF;
d[op][i][j]=h; c[op][i][j]=min(c[op][i][j],v);
}
} int main()
{
scanf("%d%d%d",&n,&m,&p);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
scanf("%d",&a[i][j]);
v[a[i][j]].push_back(i*m+j);
} for(int k=;k<;k++)
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
c[k][i][j]=INF,d[k][i][k]=-; for(int i=;i<v[].size();i++)
dp[v[][i]/m+][(v[][i]%m)+]=v[][i]/m+v[][i]%m; for(int i=;i<=p;i++)
{
for(int j=;j<v[i-].size();j++)
{
int r=v[i-][j]/m,c=v[i-][j]%m; r++; c++;
update(,i-,r,c,dp[r][c]-r-c);
update(,i-,r,m-c+,dp[r][c]-r+c);
update(,i-,n-r+,c,dp[r][c]+r-c);
update(,i-,n-r+,m-c+,dp[r][c]+r+c);
} for(int j=;j<v[i].size();j++)
{
int r=v[i][j]/m,c=v[i][j]%m; r++; c++; dp[r][c]=INF;
if(get(,i-,r,c)!=INF) dp[r][c]=min(dp[r][c],get(,i-,r,c)+r+c);
if(get(,i-,r,m-c+)!=INF) dp[r][c]=min(dp[r][c],get(,i-,r,m-c+)+r-c);
if(get(,i-,n-r+,c)!=INF) dp[r][c]=min(dp[r][c],get(,i-,n-r+,c)-r+c);
if(get(,i-,n-r+,m-c+)!=INF) dp[r][c]=min(dp[r][c],get(,i-,n-r+,m-c+)-r-c);
}
} int ans=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(a[i][j]==p) ans=dp[i+][j+];
printf("%d\n",ans);
return ;
}
CodeForces 677D Vanya and Treasure的更多相关文章
- Codeforces 677D Vanya and Treasure 暴力+BFS
链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...
- CodeForces 677D. Vanya and Treasure 枚举行列
677D. Vanya and Treasure 题意: 给定一张n*m的图,图上每个点标有1~p的值,你初始在(1,1)点,你必须按照V:1,2,3...p的顺序走图上的点,问你如何走时间最少. 思 ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- codeforces 677D D. Vanya and Treasure(二维线段树)
题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力
D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...
- codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- codeforces 677D(分层图dp)
Codeforces 677D 传送门:https://codeforces.com/contest/677/problem/D 题意: 给你一个n*m的方格图,每个点有一个权值val,现在要求你从坐 ...
- 题解-CF677D Vanya and Treasure
CF677D Vanya and Treasure 有一个 \(n\times m\) 的矩阵 \(a(1\le a_{i,j}\le p)\),求从起点 \((1,1)\) 出发依次遍历值为 \(1 ...
- 【12.78%】【codeforces 677D】Vanya and Treasure
time limit per test1.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...
随机推荐
- Controller的激活
Controller的激活 概述 在此系列开篇的时候介绍了MVC的生命周期 , 对于请求的处理,都是将相应的类的方法注册到HttpApplication事件中,通过事件的依次执行从而完成对请求的处理. ...
- [转]影响Cache的几个HTTP头信息
原文地址:http://hi.baidu.com/feilala_fly/item/f79eca08fbf389026c9048a7 Http的Cache机制总共有4个组成部分: Cache-Cont ...
- StreamWriter(ms, new UTF8Encoding(false))可以达到不输出BOM的需求。
winform 通过webservice向服务器提交图片需要注意的地方 最近一个winform项目中需要通过拍照或者上传本地文件或者截图的方式把产品图片上传到服务器,最后选择了服务器部署webserv ...
- jQuery实现table隔行换色和鼠标经过变色
一.隔行换色 $("tr:odd").css("background-color","#eeeeee"); $("tr:even& ...
- ASP.NET MVC页面UI之多级数据选择UI(行业信息、专业信息、职位信息的选择)
多级数据选择操作在开发中是常见的操作,比如选择行业信息时,一般有个大类,每个大类下边又包含很多小类,本文简单实现了弹出窗口一级一级选择功能. 本文博客出处:http://www.kwstu.com/A ...
- django admin.py settings 操作
dango, 怎么说呢,什么东西都内置了,什么东西都是自己的东西.用过flask, cherrypy, web.py, pyramid 等等python 框架后,再选用dango 觉得,理念有很大的区 ...
- ThinkPHP中连接mysql数据库的四种实用和通用的连接方法
ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库适配器来处理.目前的数 ...
- 12503 - Robot Instructions
Robot Instructions You have a robot standing on the origin of x axis. The robot will be given som ...
- 解决hexo神烦的DTraceProviderBindings MODULE_NOT_FOUND
原文:http://kikoroc.com/2016/05/04/resolve-hexo-DTraceProviderBindings-MODULE-NOT-FOUND.html 今晚折腾hexo的 ...
- mongodb两次被黑后......
先说说事情的经过...... 2017年1月8号星期天,在家翻头条无意中看到一条新闻说很多用户的mongodb被黑了,数据都被删了.当时想着公司的爬虫用的也是mongodb做存储,应该不会被黑吧,不可 ...