[NewTrain 10][poj 2329]Nearest Number - 2
题面:
http://poj.org/problem?id=2329
题解:
这题有很多做法
1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过
2. 对于每一个格子,我们枚举每一行,找到每一行离他最近的格子
当前格子向右移动时,每一行离他最近的格子不可能向左移动,所以复杂度$O(n^3)$
3. dp 一共四个方向 左上到右下 左下到右上 右上到左下 右下到左上
然后分别dp 找到这个方向离他最近的格子 以左上到右下为例 $f[i][j]$可由$f[i-1][j],f[i][j-1],f[i-1][j-1]$转移得到
复杂度$O(n^2)$
Code:
1. 搜索
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std; int n;
int a[][];
const int dx[]={,,-,},dy[]={,,,-},cx[]={-,,,-},cy[]={-,-,,}; int bfs(int x,int y,int k){
if(a[x][y]) return a[x][y];
if(k>=*n-) return ;
int cnt=;
int ok=;
for(int d=;d<;d++){
int nwx=x+k*dx[d],nwy=y+k*dy[d];
for(int i=;i<=k;i++){
if(nwx>= && nwx<=n && nwy>= && nwy<=n && a[nwx][nwy]){
if(ok==){
ok=a[nwx][nwy];
}
else{
cnt=;
}
}
nwx=nwx+cx[d];
nwy=nwy+cy[d];
}
}
if(cnt==) return ;
else if(ok) return ok;
else return bfs(x,y,k+);
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++)
printf("%d ",bfs(i,j,));
puts("");
}
return ;
}
2.
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long,long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i,j,k) for(register int i=(int)(j);i<=(int)(k);i++)
#define rrep(i,j,k) for(register int i=(int)(j);i>=(int)(k);i--) ll read(){
ll x=,f=;char c=getchar();
while(c<'' || c>''){if(c=='-')f=-;c=getchar();}
while(c>='' && c<=''){x=x*+c-'';c=getchar();}
return x*f;
} const int maxn=;
int n;
int a[maxn][maxn];
int col[maxn];
int b[maxn][maxn],num[maxn]; int main(){
#ifdef LZT
freopen("in","r",stdin);
#endif
n=read();
rep(i,,n)
rep(j,,n){
a[i][j]=read();
if(a[i][j]){
num[i]++;
b[i][num[i]]=j;
}
}
rep(i,,n){
rep(j,,n) col[j]=;
rep(j,,n){
if(a[i][j]==){
int mndis=1e9,mn=-;
rep(k,,n){
int nw=1e9,xx=-;
if(col[k]<=num[k] && nw>abs(b[k][col[k]]-j)+abs(k-i)){
nw=abs(b[k][col[k]]-j)+abs(k-i);
xx=a[k][b[k][col[k]]];
}
if(col[k]<num[k]){
int nwdis=abs(b[k][col[k]+]-j)+abs(k-i);
if(nw>nwdis){
nw=nwdis;
xx=a[k][b[k][col[k]+]];
}
else if(nw==nwdis){
xx=-;
}
}
if(mndis>nw){
mndis=nw;mn=xx;
}
else if(mndis==nw){
mn=-;
}
//cout<<i<<' '<<j<<' '<<k<<' '<<col[k]<<' '<<xx<<endl;
}
if(mn!=-) a[i][j]=mn;
}
rep(k,,n){
if(col[k]<num[k] && abs(b[k][col[k]]-(j+))>abs(b[k][col[k]+]-(j+))) col[k]++;
}
}
} rep(i,,n){
rep(j,,n)
printf("%d ",a[i][j]);
puts("");
}
return ;
}
3.
没写过
不过应该很好写
Review:
水题
数据也很水
比如第一个代码 在所有格子均为0的时候复杂度是严格$O(n^4)$的 但是竟然跑得比第二个代码快300ms。。。
每种做法都挺好想
[NewTrain 10][poj 2329]Nearest Number - 2的更多相关文章
- [POJ 2329] Nearest number-2
Link: POJ 2329 传送门 Solution: 比较明显的$dp$,但爆搜好像也能过 用多个方向$dp$来解决此题,最后汇总答案即可 一开始我写了4个,但后来发现只要相反的2个方向即可,同时 ...
- 【POJ】2329 Nearest number - 2(搜索)
题目 传送门:QWQ 分析 在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索. 看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的? poj挂了,没在poj交, ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ 2329 (暴力+搜索bfs)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 De ...
- POJ-2329 Nearest number - 2(BFS)
Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1275 De ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
随机推荐
- AptitudeSystem 2.0
AptitudeSystem 2.0(2017-03-07) 描写叙述:Windows内核研究辅助工具 支持的系统:Windows 7.Windows 8.Windows 8.1.Windows 10 ...
- Gym - 101164C - Castle KMP
题目链接:传送门 题解: 利用KMP的fail失配数组,快速找到当前后缀与前缀的公共前缀点 #include<bits/stdc++.h> using namespace std; #pr ...
- 使用Apache Ant合并多个jar
Apache Ant下载地址 下载解压后进入bin目录,并在此目录打开cmd 在cmd中运行ant,运行结果为: Buildfile: build.xml does not exist! Build ...
- Uva 3902 Network
题目大意: 在非叶子节点上安装最少的服务器使得,每个叶子节点到服务器的距离不超过k. 贪心+图上的dfs. 先从深度最大的叶子节点开始找.找到父节点后再用这个父节点进行扩充. /* ********* ...
- vue 使用html2canvas将DOM转化为图片
一.前言 我发现将DOM转化为图片是一个非常常见的需求,而自己手动转是非常麻烦的,于是找到了html2canvas这个插件,既是用得比较多的也是维护得比较好的一个插件. 注意:版本比较多,这里介绍最新 ...
- react native 中的ListView
ListView 的运用: 1.首先在react native中引入这个组件: 2.初始化的ListView 的相关属性: constructor(props) { super(props); con ...
- 有关定时器setTimeout()、setInterval()详解
JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成. setTimeout() setTimeout函数用 ...
- MYSQL进阶学习笔记十一:MySQL 表的分析,检查和优化!(视频序号:进阶_28)
知识点十二:MySQL 表的分析,检查和优化(28) 表的分析,检查和优化: 定期分析表: ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, ...
- poj 2923 Relocation 解题报告
题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物 ...
- hdu 1963 Investment 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 题目意思:有 本金 money,还有一些股票的种类,第 i 种股票买入需要 value[i] 这 ...