EcustOJ P109跳一跳(离散化+dp)
题目链接
感觉这道题我看了很多天,胡思乱想啊,一开始觉得记忆化搜索会可能T啊,,可能出题人的数据卡的好就稳T了的感觉。。后来想了想,好像离散化一下,记一下位置之后再记忆化搜索就应该稳了吧。。(好像直接搜也不会T的,,??)
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <bitset>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
const int maxn=1e5+1;
int dp[maxn][3],ans;
int a[maxn],b[maxn],pos[maxn],n;
int dfs(int now,int sta){
if(now==n)return dp[now][sta]=1;
if(dp[now][sta]!=-1)return dp[now][sta];
if(sta==0){
int cnt=0;
while(pos[a[now]+cnt]<=now&&a[now]+cnt+1<=n)cnt++;
if(pos[a[now]+cnt]<=now)return dp[now][sta]=0;
return dp[now][sta]=dfs(pos[a[now]+cnt],1);
}else {
int cnt=0;
while(pos[a[now]-cnt]<=now&&a[now]-cnt-1>=1)cnt++;
if(pos[a[now]-cnt]<=now)return dp[now][sta]=0;
return dp[now][sta]=dfs(pos[a[now]-cnt],0);
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],b[i]=a[i];
sort(b+1,b+1+n);
int cnt=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+1+cnt,a[i])-b,pos[a[i]]=i;
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;i++)if(dfs(i,0))ans++;
cout<<ans<<endl;
return 0;
}
第二天 ,写了一个非递归的版本。。。快一点,大致都差不多的。
dp[i][0]=dp[pos][1],(a[i]<a[pos],且a[pos]>a[p] {p!=pos且p>i且a[p]<a[i]}。
dp[i][1]=dp[pos][0],(a[i]>a[pos],且a[pos]<a[p] {p!=pos且p>i且a[p]>a[i]}。
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <bitset>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
const int maxn=1e5+1;
int dp[maxn][3],ans;
int a[maxn],b[maxn],pos[maxn],n;
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],b[i]=a[i];
sort(b+1,b+1+n);
int cnt=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+1+cnt,a[i])-b,pos[a[i]]=i;
dp[n][0]=dp[n][1]=1;
for(int i=n-1;i>=1;i--){
int cnt=0;
while(pos[a[i]+cnt]<=i&&a[i]+cnt+1<=n)cnt++;
if(pos[a[i]+cnt]<=i)dp[i][1]=0;
else dp[i][1]=dp[pos[a[i]+cnt]][0];
cnt=0;
while(pos[a[i]-cnt]<=i&&a[i]-cnt-1>=1)cnt++;
if(pos[a[i]-cnt]<=i)dp[i][0]=0;
else dp[i][0]=dp[pos[a[i]-cnt]][1];
}
for(int i=1;i<=n;i++)if(dp[i][1]||i==n)ans++;
cout<<ans<<endl;
return 0;
}
EcustOJ P109跳一跳(离散化+dp)的更多相关文章
- 【loj6342】跳一跳 期望dp
题目描述 一个人从 $1$ 开始向 $n$ 跳,在 $i$ 时会等概率跳到 $i,i+1,...,n$ 之一.求从 $1$ 跳到 $n$ 的期望步数. $n\le 10^7$ . 题解 期望dp傻逼题 ...
- loj #6342. 跳一跳 期望dp
令 $f[i]$ 表示已经到达 $i$ 点,为了到大 $n$ 点还期望需要的时间,随便转移一下就行. 由于本题卡空间,要记得开滚动数组. #include <bits/stdc++.h> ...
- 挑战App Store,微信通过“跳一跳”秀了一下“小程序”的肌肉
2017年即将结束的时候,微信放了一个大招.随着最新的微信v6.6.1版本更新,基于小程序的"小游戏"板块正式上线.微信上首发的这款"小游戏"叫"跳一 ...
- .NET开发一个微信跳一跳辅助程序
昨天微信更新了,出现了一个小游戏"跳一跳",玩了一下 赶紧还蛮有意思的 但纯粹是拼手感的,玩了好久,终于搞了个135分拿了个第一名,没想到过一会就被朋友刷下去了,最高的也就200来 ...
- C#又能出来装个B了。一步一步微信跳一跳自动外挂
PS:语言只是载体.思维逻辑才是王道 前天看见了个python的脚本.于是装python.配置环境变量.装pip.折腾了一上午,最终装逼失败. 于是进入博客园,顶部有篇文章吸引了我 .NET开发一个微 ...
- 用C#实现微信“跳一跳”小游戏的自动跳跃助手
一.前言: 前段时间微信更新了新版本后,带来的一款H5小游戏“跳一跳”在各朋友圈里又火了起来,类似以前的“打飞机”游戏,这游戏玩法简单,但加上了积分排名功能后,却成了“装逼”的地方,于是很多人花钱花时 ...
- Adb+.net 实现微信跳一跳自动化
第一次用adb,一开始只是想试试看能不能解析出,没有看网上的现有解析方式. 需要安卓机开启usb 调试+电脑运行.打开跳一跳的界面 点击程序 [开始]按钮即可开始,别的按钮都是调试用的 主要流程是用a ...
- 微信跳一跳Python
微信最新的小程序里面出了个叫“跳一跳”的小游戏,大神们也通过Python实现了自动玩游戏具体代码 如下: Github地址: https://github.com/wangshub/wechat_ju ...
- 微信跳一跳辅助自动跳Python
一.说明 此代码借鉴github一位大神所写,已经做了简化合并处理,如果能成功连上手机并运行,可以实现程序自动玩游戏,刷个1000+的分数轻轻松松 github源码地址 https://github. ...
随机推荐
- bootstrap 栅栏系统
媒体查询 /* 超小屏幕(手机,小于 768px) */ /* 没有任何媒体查询相关的代码,因为这在 Bootstrap 中是默认的(还记得 Bootstrap 是移动设备优先的吗?) */ /* 小 ...
- N球M盒
N球,M盒,由于球是否相同,盒是否相同,盒是否可以为空,共2^3=8种: 1.球同,盒同,盒不可以为空Pm(N)--这符号表示部分数为m的N-分拆的个数,m是P的下标,为了好看我将大写的M弄成小写 2 ...
- 【POJ2182】Lost Cows 树状数组+二分
题中给出了第 i 头牛前面有多少比它矮,如果正着分析比较难找到规律.因此,采用倒着分析的方法(最后一头牛的rank可以直接得出),对于第 i 头牛来说,它的rank值为没有被占用的rank集合中的第A ...
- Django 路由报错友好提示
这个方法要在设置路由文件内使用也就是urls.py内. """mysite URL Configuration The `urlpatterns` list routes ...
- 原生js实现each方法
首先我们了解一下什么是callback函数 CALLBACK,即回调函数,是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就 ...
- 真机控件获取 app-inspector
1.安装app-inspector:npm install app-inspector -g 若是要卸载原有的:npm uninstall app-inspector -g np ...
- github在网页编写readme之后的操作
study from : https://jingyan.baidu.com/article/f3e34a12a25bc8f5ea65354a.html
- RAND_MAX
RAND_MAX 指的是 C 语言标准库 <stdlib.h> 中定义的一个宏.经预编译阶段处理后, 它展开为一个整数类型的常量表达式.RAND_MAX 是 <stdlib.h> ...
- http协议中的请求方式
get:获取url传的查询字符串(action=show)表单和连接的url中传的值.容量2K左右. post:以post方式提交,获取表单和连接的url中传的值.容量8M左右. delete:删除某 ...
- bzoj2957 奥妙重重的线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=2957 线段树的query和update竟然还可以结合起来用! 题意:小A的楼房外有一大片施工工地, ...