CodeForces 677D. Vanya and Treasure 枚举行列
题意:
给定一张n*m的图,图上每个点标有1~p的值,你初始在(1,1)点,你必须按照V:1,2,3...p的顺序走图上的点,问你如何走时间最少。
思路:
我一开始想的思路感觉很巧妙,但是TLE了。就是把不同值的点放在不同的vector中,然后类似dp的从2更新最小距离到p。因为我这是暴力枚举点的,复杂度不对。后来发现这个思路还需要优化一下,就是把同一行的点放在一起,for一遍这一行属于V的点,就可以更新本行的信息了,再向下把列中属于v+1的更新了。复杂度就降到了O(n^3);
%和学习的是这份代码:huhuhu
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;
///#pragma GCC optimize(3)
///#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; ///priority_queue<int> q;//这是一个大根堆q
///priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
///#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
///priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //
const ll nmos = 0x80000000LL; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //
const int mod = 1e9+; const double PI=acos(-1.0); // #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/ const int maxn = ;
int dp[maxn][maxn], mp[maxn][maxn];
vector<pii>v[maxn*maxn];
vector<int>col[maxn];
bool vis[maxn];
int main(){
int n,m,tp;
scanf("%d%d%d", &n, &m, &tp);
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
int op; scanf("%d", &op);
mp[i][j] = op;
if(op==)dp[i][j] = i+j-;
else dp[i][j] = inf;
v[op].pb(pii(i,j));
}
} for(int i=; i<tp; i++){
for(int j=; j<= max(n,m); j++){
col[j].clear();
vis[j] = false;
} for(pii p : v[i+]){
col[p.se].pb(p.fi);
}
for(pii p: v[i]){
vis[p.fi] = true;
} for(int j=; j<=n; j++) if(vis[j]){
int best = inf;
for(int k = ; k<=m; k++){
best++;
if(mp[j][k] == i)
best = min(best, dp[j][k]);
for(int y : col[k]){
dp[y][k] = min(dp[y][k], best + abs(y-j));
}
} best = inf;
for(int k = m; k>=; k--){
best++;
if(mp[j][k] == i)
best = min(best, dp[j][k]);
for(int y : col[k]){
dp[y][k] = min(dp[y][k], best + abs(y-j));
}
}
}
} for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
if(mp[i][j]==tp){
printf("%d\n", dp[i][j]);
}
}
}
return ;
}
CF-677D
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 - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- CodeForces 677D Vanya and Treasure
$dp$,树状数组. 很明显这是一个$DAG$上的$dp$,由于边太多,暴力$dp$会超时,需要优化. 例如计算$dp[x][y]$,可以将区域分成四块,$dp[x][y]$取四块中的最小值,每一块用 ...
- 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 677E Vanya and Balloons
Vanya and Balloons 枚举中心去更新答案, 数字过大用log去比较, 斜着的旋转一下坐标, 然后我旋出来好多bug.... #include<bits/stdc++.h> ...
- 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 ...
随机推荐
- 【Android】Fresco 初次使用遇到的坑
初次使用开源框架 Fresco,结果遇到了坑,被虐了半下午--暂且记下. 下面的错误 android.view.InflateException: Binary XML file line #** 报 ...
- 【iOS】ARC & MRC
iOS 项目类型,是 ARC 还是 MRC 未完……
- 面试必谈的哈希,.Net 程序员温故而知新
引言: 作为资深老鸟,有事没事,出去面试:找准差距.定位价值. 面试必谈哈希, Q1:什么是哈希? Q2:哈希为什么快? Q3:你是怎么理解哈希算法利用空间换取时间的? Q4:你是怎么解决哈希冲突的? ...
- codeforces 371A K-Periodic Array
很简单,就是找第i位.i+k位.i+2*k位...........i+m*k位有多少个数字,计算出每个数出现的次数,找到出现最多的数,那么K-Periodic的第K位数肯定是这个了.这样的话需要替换的 ...
- Java编程思想之十七 容器深入研究
17.1 完整的容器分类方法 17.2 填充容器 import java.util.*; class StringAddress { private String s; public StringAd ...
- 续集:白菜的内涵,更新nand分区为ubifs,替换overlay
在上一篇真千兆路由的极限之OPENWRT MAKE, 某品牌白菜价QCA9558/QCA9880/QCA8337N纯种组合OS搭建时记中附带了128M nand的空间图示,在ar71xx profil ...
- kylin Retrieving hive dependency...
由于公司环境配置hive默认连接hiveserver2 ,不管hive cli 还是beeline cli都默认使用beeline cli,连接hive需要输入账号密码; 启动kylin 时会Retr ...
- centos开发环境安装
执行 yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel ...
- 逆向破解之160个CrackMe —— 001
CrackMe —— 001 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- RE最全面的正则表达式----终结篇 特殊处理
三.特殊需求表达式 Email地址:^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0- ...