$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. Day3:T3DP T4堆排序

    QAQ因为T3的题解说的不是很清楚,但是blogs又不能按照自己想要的顺序排+略有一点强迫症 所以先挖个坑放着,过几天再补上

  2. ASP.NET开发大杂烩

    ASP.NET开发大杂烩 正巧今天遇到一个获取动态生成table中的一个动态生成的TextBox的值的时候总是findcontrol不到.后来经过我们的徐总,瞬间解决,但是我觉得对于一个页面的声明周期 ...

  3. c语言:将二进制数按位输出

    问题: 1.输入int 20,其二进制为10100,按位输出10100; 2.或者将1转化为“+”,0转化为“-”,输出就是” + - + - - “; int biTofh(int bi,int l ...

  4. CNN for Visual Recognition (assignment1_Q1)

    参考:http://cs231n.github.io/assignment1/ Q1: k-Nearest Neighbor classifier (30 points) import numpy a ...

  5. UML九种图-包图、类图

    UML九种图-包图.类图 一.包 (一)相关概念: 1.包: 一个包=一层=一个命名空间=一个文件夹 2.包的命名: 简单名:王老二 路径名:中国.河北省.廊坊市.廊坊师范学院.信息技术提高班.九期班 ...

  6. 分享自制的C#和VB Code互转工具

    分享自制的C#和VB Code互转工具 作为.NET程序员,往往习惯使用一种语言(据我观察,2006年后的程序员习惯用C#,之前的喜欢VB).而对于另一种语言虽然能读懂但是写起来总是比较费事.尤其面对 ...

  7. Nginx之旅系列 - Nginx的configuration

    题记:Nginx之旅系列是用来记录Nginx从使用到源码学习的点点滴滴,分享学习Nginx的快乐 Nginx 首页: http://nginx.org/ Nginx的configuration 今天对 ...

  8. Big Data Ingestion and streaming product introduction

    Flume Flume isdistributed system for collecting log data from many sources, aggregating it,and writi ...

  9. 小C的树(Treap节点删除)[70/100]

    用Treap暴力模拟,调常数~ 好了,接下来说一下Treap中的删除操作 Step 1:先找到这个结点. Step 2:如果它的儿子≤1,用它儿子替它 否则 把之后做根节点的儿子旋转到根 回到Step ...

  10. (转)Tomcat 配置成https协议

    Tomcat 配置成https协议 TomcatXMLServlet  在命令提示符窗口,进入Tomcat目录,执行以下命令: keytool -genkey -alias tomcat -keyal ...