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 ...
随机推荐
- 2013.5.A
题1 高低位交换 [问题描述] 给出一个小于2^32的正整数.这个数可以用一个32位的二进制数表示(不足32位用0补足).我们称这个二进制数的前16位为“高位”,后16位为“低位”.将它的高低位交换, ...
- PHP gbk转换成utf8
/** * GBK ASCII 转换成utf8 */ public function to_utf8($str){ $detect = array('ASCII', 'GBK', 'UTF-8'); ...
- [转]ARM64 Function Calling Conventions
from apple In general, iOS adheres to the generic ABI specified by ARM for the ARM64 architecture. H ...
- django 创建一个通用视图
创建一个通用视图 抽取出我们代码中共性的东西是一个很好的编程习惯. 比如,像以下的两个Python函数: def say_hello(person_name): print 'Hello, ...
- springMVC3学习(八)--全局的异常处理
在springMVC的配置文件中: <bean id="exceptionResolver" class="org.springframework.web.serv ...
- 最近对Memcache的一些学习
首先,Memcache是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度,再特别强调下:M ...
- SpringMVC入门笔记一
SpringMVC优势 性能比struts2好 简单 便捷 易学 和Spring无缝集成(使用spring ioc aop) 约定优于配置 能够简单进行Junit测试 ...
- enode框架step by step之事件驱动架构(EDA)思想的在框架中如何体现
enode框架step by step之事件驱动架构(EDA)思想的在框架中如何体现 上一篇文章,我给大家分享了我的一个基于DDD以及EDA架构的框架enode,但是只是介绍了一个大概.接下来我准备用 ...
- rabbitmq在mac上安装
1.安装brew 打开http://bash.sh 执行 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ ...
- 【Oracle】-【体系结构】-【DBWR】-DBWR进程相关理解
对DBWR的一些理解 首先从名称上,DBWR全称是Database Writer Process,属于Oracle后台进程的一种,有的地方也叫DBWn,我想这里是出于DBWR进程个数的原因,DBWR进 ...