滑雪(ski)
滑雪(ski)
题目描述
Michael喜欢滑雪。这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道在一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。
输入
第1行为表示区域的二维数组的行数R和列数C(1≤R,C≤l00)。下面是R行,每行有C个数,代表高度。
输出
输出区域中最长滑坡的长度。
样例输入
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
样例输出
25
提示
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可行的滑坡为24-17-16-1(从24开始,在l结束)。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。
分析:可以按权值从小到大遍历点(贪心),另一种思路是直接记忆化,貌似记忆化更快。。。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e2+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,a[maxn][maxn],ma,vis[maxn][maxn];
set<pair<int,pii> >q;
int main()
{
int i,j,k,t;
scanf("%d%d",&n,&m);
rep(i,,n-)rep(j,,m-)scanf("%d",&a[i][j]),q.insert(mp(a[i][j],mp(i,j)));
for(auto x:q)
{ int val=x.fi,l=x.se.fi,r=x.se.se;
vis[l][r]=;
for(int i=;i<;i++)
{
int xx=l+dis[i][],yy=r+dis[i][];
if(xx>=&&xx<n&&yy>=&&yy<m&&vis[xx][yy]&&a[xx][yy]<a[l][r])
{
vis[l][r]=max(vis[l][r],vis[xx][yy]+);
}
}
ma=max(ma,vis[l][r]);
}
printf("%d\n",ma);
//system ("pause");
return ;
}
记忆化:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include<functional>
#include <set>
#include <map>
#include <climits>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 30005
#define maxn 100005
typedef pair<int,int> PII;
typedef long long LL; int n,m;
int dp[][];
int a[][];
int dir[][]={{,},{-,},{,},{,-}};
int dfs(int x,int y){
if(dp[x][y]!=-)return dp[x][y];
for(int i=;i<;++i){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx<||xx>n||yy<||yy>m)continue;
if(a[xx][yy]<a[x][y]){
dp[x][y]=max(dp[x][y],+dfs(xx,yy));
}
}
return dp[x][y]=max(dp[x][y],);
} int main(){
//freopen("in.txt","r",stdin);
int i,j;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=;i<=n;++i)
for(j=;j<=m;++j)
scanf("%d",&a[i][j]);
mst(dp,-);
for(i=;i<=n;++i)for(j=;j<=m;++j)if(dp[i][j]==-)dp[i][j]=dfs(i,j);
int ans=-;
for(i=;i<=n;++i)for(j=;j<=m;++j)ans=max(ans,dp[i][j]);
printf("%d\n",ans);
}
return ;
}
滑雪(ski)的更多相关文章
- ImageNet2017文件下载
ImageNet2017文件下载 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PASCAL ...
- ImageNet2017文件介绍及使用
ImageNet2017文件介绍及使用 文件说明 imagenet_object_localization.tar.gz包含训练集和验证集的图像数据和地面实况,以及测试集的图像数据. 图像注释以PAS ...
- [USACO09OPEN]滑雪课Ski Lessons
题目描述 Farmer John wants to take Bessie skiing in Colorado. Sadly, Bessie is not really a very good sk ...
- 【USACO2009 Open】滑雪课程ski
[USACO2009 Open]滑雪课程 Ski Lessons Time Limit: 1000 ms Memory Limit: 131072 KBytes Description 约翰请贝西去科 ...
- 译:滑雪租赁问题(ski rental problem)
本文翻译自维基百科词条:http://en.wikipedia.org/wiki/Ski_rental_problem 滑雪租赁问题(ski rental problem)是一类问题的总称, ...
- 洛谷 P3650 [USACO1.3]滑雪课程设计Ski Course Design
P3650 [USACO1.3]滑雪课程设计Ski Course Design 题目描述 农民约翰的农场里有N座山峰(1<=N<=1000),每座山都有一个在0到100之间的整数的海拔高度 ...
- [USACO2009 OPEN] 滑雪课 Ski Lessons
洛谷P2948 看到题目就觉得这是动规但一直没想到如何状态转移……看了别人的题解之后才有一些想法 f[i][j]:前i单位时间能力值为j可以滑的最多次数 lessons[i][j]:结束时间为i,获得 ...
- LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating
LGOJ3101 [USACO14JAN]滑雪等级Ski Course Rating [问题描述] The cross-country skiing course at the winter Mool ...
- BZOJ 1571: [Usaco2009 Open]滑雪课Ski
Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里,每天会提供S(0<=S& ...
随机推荐
- 如何对Site Settings页面进行定制化 添加一个setting 链接
下面在Site Settings页 >Site Administration里添加一个Ruby Setting 超链接,点击进入到rubySetting.aspx 1.在SharePoint p ...
- Chapter 2 Open Book——12
I called him in when dinner was ready, and he sniffed appreciatively as he walked into the room. 当晚饭 ...
- data Mining with Weka: Trailer More Data Mining with Weka 用weka 进行数据挖掘 Weka 用weka 进行更多数据挖掘
https://www.youtube.com/user/WekaMOOC 大学公开课 视频教程 weka 入门教程 data Mining with Weka: Trailer More Dat ...
- JavaScript高级程序设计:第五章
引用类型 一.object类型: 创建object实例的方式有两种.第一种是使用new操作符后跟Object构造函数,如下所示: var person = new Object(): person ...
- iOS 上架提示ipad需要显示四个方位,而我们只能竖屏的时候的解决办法
勾选requires sull screen
- 常见html标签
1.flash嵌入标签 <object> <embed allowscriptaccess="always" allowfullscreen="true ...
- 百度,google的地理编码
1.百度的地理编码:(不支持中国以外的其它城市) http://api.map.baidu.com/geocoder/v2/?ak=E974997f80db18330f8f5c61d084a677&a ...
- 使用Core Animation对象来实现动画
转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...
- Loadrunner之脚本的调试和保存(六)
一.调试脚本 脚本录制完毕后,按F5键或单击菜单上的RUN按钮,可以运行脚本. 在VIRTUAL USER GENERATOR中运行脚本的作用,主要是查看录制的脚本能否正常通过,如果有问题 ...
- oracle10g遇到ORA-00257归档程序错误,在释放之前仅限于内部连接
一.简要介绍 首先数据库日志文件有两种: 联机日志文件和归档日志文件,联机日志文件会将之前的覆盖,不会做备份. 而归档日志文件会做备份,这样就造成了归档日志空间已满,解决方法: 如果真的是归档日志空间 ...