$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的更多相关文章

  1. Codeforces 677D Vanya and Treasure 暴力+BFS

    链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...

  2. CodeForces 677D. Vanya and Treasure 枚举行列

    677D. Vanya and Treasure 题意: 给定一张n*m的图,图上每个点标有1~p的值,你初始在(1,1)点,你必须按照V:1,2,3...p的顺序走图上的点,问你如何走时间最少. 思 ...

  3. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  4. codeforces 677D D. Vanya and Treasure(二维线段树)

    题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...

  5. 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 ...

  6. codeforces 492E. Vanya and Field(exgcd求逆元)

    题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...

  7. codeforces 677D(分层图dp)

    Codeforces 677D 传送门:https://codeforces.com/contest/677/problem/D 题意: 给你一个n*m的方格图,每个点有一个权值val,现在要求你从坐 ...

  8. 题解-CF677D Vanya and Treasure

    CF677D Vanya and Treasure 有一个 \(n\times m\) 的矩阵 \(a(1\le a_{i,j}\le p)\),求从起点 \((1,1)\) 出发依次遍历值为 \(1 ...

  9. 【12.78%】【codeforces 677D】Vanya and Treasure

    time limit per test1.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...

随机推荐

  1. 在 Ubuntu 12.04 上通过 Tomcat 部署 Solr 4

    http://www.oschina.net/question/12_71342 可行

  2. CSS盒模型和定位的类型

    此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...

  3. java判断字符串是否为乱码

    项目中有一个功能 在IE中GET方式提交会产生乱码 但有两个入口都会走这同一段代码 固不能直接转码,所以要进行判断传过来的该值是不是乱码 可用以下方式验证: java.nio.charset.Char ...

  4. RTB撕开黑盒子 Part 4: Shady Bidding

    在这篇文章中,我将告诉你"真实的出价"比你想的微妙,并且你可以使用基于ROI的pacing策略,不需要构建一个期望扣费的模型,你就可以得到完美的期望扣费模型. Same Same ...

  5. RCP:如何把Preferences中的项从一个类别移动到另一个类别 2013-08-23 18:59 by Binhua Liu,

    RCP:如何把Preferences中的项从一个类别移动到另一个类别 前言 很久没写文章了,准备写一系列关于Eclipse RCP /Plugin的文章. 这些文章都是trouble shooting ...

  6. LigerUI权限系统之组织结构

    先上图,再看代码.组织结构界面 组织结构添加: 组织结构修改: 组织结构删除: 我在做这个页面的时候treegrid 的远程数据加载让我很头痛,从LigerUI官网提供的Demo来看,它是根据json ...

  7. 一步一步深入spring(3)--spring的依赖注入方式

    对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...

  8. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

  9. Bitmap的一个简单实现

    一.Bitmap简介 Bitmap是一种常用的数据结构,其实就是一个连续的数组,主要是用于映射关系,如映射整数,一位代表一个数,即这里假设Bitmap有100Bytes * 8 这么多的位,那么这里可 ...

  10. Java8:使用Lambda表达式增强版Comparator排序

    学习路上的自我记录-------路好长,就问你慌不慌,大声港,不慌.----jstarseven. 实体类: package com.server.model; /** * Created by js ...