$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. 使用TotoriseSVN的分支/标签功能

    使用TotoriseSVN的分支/标签功能 http://blog.sina.com.cn/s/blog_5383588c0100o1oo.html 原来在新建一个项目文件夹后,再把Trunk.Bra ...

  2. [实验]通过内核Patch去掉iOS-v4.3.3的沙盒特性

    环境: 1.Mac OS X 10.9.2 2.xcode 5.1.1 3.gcc 4.8 4.redsn0w 0.9.15b3 前提: 1.获取 iOS 4.3.3 的kernelcache,并解密 ...

  3. inet_aton()

    两次技术面试都被让c语言实现inet_aton()函数 看来这个函数真的很重要. 我先贴上我自己的实现代码 #include <stdio.h> #include <math.h&g ...

  4. linux挂载分区

    [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda2 36G .3G 32G % / tmpf ...

  5. Apple的通知

    Apple的通知 1. 本地通知 //取消原来的通知,可设可不设 [[UIApplication sharedApplication]cancelAllLocalNotifications]; //初 ...

  6. 开发框架(WinForm)3

    我的开发框架(WinForm)3 今天继续给大家介绍核心库的IOC的使用,在我的框架里,IOC使用的比较简单,主要是用于解除模块间的耦合和实例化接口. 1.接口说明,IocContainer接口比较简 ...

  7. Fast Token Replacement in C#

    http://www.codeproject.com/Articles/298519/Fast-Token-Replacement-in-Csharp Fast Token Replacement i ...

  8. ZOJ 3529 A Game Between Alice and Bob 博弈好题

    A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play t ...

  9. JVM内存模型及垃圾收集策略解析(一)

    JVM内存模型是Java的核心技术之一,之前51CTO曾为大家介绍过JVM分代垃圾回收策略的基础概念,现在很多编程语言都引入了类似Java JVM的内存模型和垃圾收集器的机制,下面我们将主要针对Jav ...

  10. 【终极】Dynamic Web Module 3.0 requires Java 1.6 or newer

    今天在用maven+ssm做项目的时候,右键项目properities设置project facet,勾选dynamic WEB module时一直勾不上,网上找了很多教程都不行, 有说在pom.xm ...