[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节 ...
随机推荐
- mt7620 wifi driver
<*> Ralink RT2860 802.11n AP support [*] LED Support [*] WSC (WiFi Simple Config) [*] WSC 2.0( ...
- nfs 挂载错误
[ 147.080000] svc: failed to register lockdv1 RPC service (errno 146). [ 147.090000] lockd_up: makes ...
- 2016/05/25 抽象类与API(接口)差别
简单来说, 接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的, 另外,实现接口的一定要实现接口里定义的所有方法,而实现抽象类可以有选择地重写需要用到的 ...
- 新拿到的app跑的时候出现问题
连接器链接失败,是因为对应类build后的中间产物.o文件没有生成,对应架构下,用模拟器跑的,很可能是因为无法编译产出x86,或i386架构的中间产物,所以linker链接转换机器码时候找不到对应的中 ...
- org.apache.flume.EventDeliveryException: NettyAvroRpcClient { host: hadoop1, port: 41414 }: Failed to send event
org.apache.flume.EventDeliveryException: NettyAvroRpcClient { host: hadoop1, port: 41414 }: Failed t ...
- An O(ND) Difference Algorithm and Its Variations (1986)
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 The problems of finding a longest com ...
- dedecms获取顶级栏目名称、二级栏目名称实现方法
织梦DEDECMS文章.栏目页获取当前页面顶级栏目名称的方法 在用织梦做一些项目时,时常会碰到需要在当前页面调用顶级栏目名称的时候,织梦默认{dede:field name='typename' /} ...
- 一步一步学Silverlight 2系列(20):如何在Silverlight中与HTML DOM交互(下)
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- hiho Mission Impossible 6(模拟 未提交验证。。)
题意:模拟文本操作 思路:模拟 #include<iostream> #include<stdio.h> #include<string.h> using name ...
- position属性中的绝对定位和相对定位
absolute(绝对定位):1.如果没有父级DIV,则会根据浏览器原始点去定位,而且跟他相邻的DIV会忽略它,定位后则可用TRBL(top,right,bottom,left)去布局.注意:TRBL ...