P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144
一道非常有趣的游戏,不,题目。
当数据水时,可以这样表示状态。
f[i][j]表示合并[i,j]区间所能得到的最大值,有点floyed的小味道。
if(f[i][k]==f[k+1][j])
f[i][j]=max(f[i][k]+1,f[i][j]);
不断更新答案。
这样需要枚举左右端点和中间点,O(n^3)
300以内的数据可以水过。
状态的设定直接把dp拉到暴力里去了。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.26
using namespace std;
int n;
int x;
int f[][];
int ans;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
}
int main()
{
in(n);
For(i,,n)
{
in(x);
f[i][i]=x;
}
For(j,,n)
for(int i=j;i>=;i--)
For(k,i,j-)
if(f[i][k]==f[k+][j])
{
f[i][j]=max(f[i][k]+,f[i][j]);
ans=max(ans,f[i][j]);
}
o(ans);
return ;
}
O.O
优化状态:
有点倍增的小味道。
i表示数值,j表示区间的左端点,f[i][j]表示右端点,如果存在这样的右端点,就把ans更新为i。怎么转移呢?
if(!f[i][j]&&f[i-1][j])
f[i][j]=f[i-1][f[i-1][j]+1];
画个图就明白了。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.26
using namespace std;
int n;
int x;
int f[][];//18M
int ans;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
}
int main()
{
in(n);
For(i,,n)
{
in(x);
f[x][i]=i;
}
For(i,,)
For(j,,n)
if(!f[i][j]&&f[i-][j]&&f[i-][f[i-][j]+])
{
f[i][j]=f[i-][f[i-][j]+];
ans=i;
}
o(ans);
return ;
}
P3147 [USACO16OPEN]262144的更多相关文章
- 洛谷P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- 洛谷 P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- P3146 [USACO16OPEN]248 & P3147 [USACO16OPEN]262144
注:两道题目题意是一样的,但是数据范围不同,一个为弱化版,另一个为强化版. P3146传送门(弱化版) 思路: 区间动规,设 f [ i ][ j ] 表示在区间 i ~ j 中获得的最大值,与普通区 ...
- 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144
https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...
- luogu P3147 [USACO16OPEN]262144
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- 洛谷 P3147 [USACO16OPEN]262144 P
链接: P3147 P3146双倍经验 前言: 今天发现的一道很有意思的DP题 分析: 第一眼以为是区间DP,于是设f[i][j]为从第i个数到第j个数可以合出的最大值,但思考后发现并不能简单合并,并 ...
- P3147 [USACO16OPEN]262144 (贪心)
题目描述 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-262,144),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. 这道题的思路: ...
- [USACO16OPEN]262144
传送门啦 其实大家可以先看一下这个题 [USACO16OPEN]248 分析: 数据范围很奇特:n特别,a[i]特别——如果O(N^3)能接受就直接区间DP水过了,但是不行,于是考虑设计一个状态囊括a ...
- 【[USACO16OPEN]262144】
发现这个数列的范围特别大但是值域的范围特别小 于是可以大胆猜测这道题值域肯定需要开到状态里去 又发现\(262144=2^{18}\)这个暗示非常明显啊,暗示这道题跟二进制有关系 其实也没什么关系 设 ...
随机推荐
- hdp3: regionserver running as process 3170. Stop it first.
1 启动hbase报错 hdp3: regionserver running as process 3170. Stop it first. 2 始终没有发现错误日志 3 jps没有发现hbase的r ...
- [ZJOI2012]波浪弱化版(带技巧的DP)
题面 \(solution:\) 这道确实挺难的,情况特别多,而且考场上都没想到如何设置状态.感觉怎么设状态不能很好的表示当前情况并转移,考后发现是对全排列的构造方式不熟而导致的,而这一题的状态也是根 ...
- 微信小程序的页面渲染(if/for)
下面,粗略的介绍一下微信小程序的条件渲染.列表渲染.数据绑定等,详细的内容大家可以去看微信小程序的API,在此只做简单描述,希望能帮助到大家 条件渲染 <!--wxml--> <vi ...
- Android NetworkInterface 的 name
user@android:/$ ls /sys/class/net/ dummy0 lo p2p0 rev_rmnet0 rev_rmnet1 rev_rmnet2 rev_rmnet3 rmnet0 ...
- jQuery——Js与jQuery的相互转换
$()与jQuery() jQuery中$函数,根据传入参数的不同,进行不同的调用,实现不同的功能.返回的是jQuery对象 jQuery这个js库,除了$之外,还提供了另外一个函数:jQuery j ...
- SpringMVC——SpringMVC简介
Spring web mvc 和Struts2 都属于表现层的框架,它是Spring 框架的一部分,我们可以从Spring 的整体结构中看得出来:
- Check Box、Radio Button、Combo Box控件使用
Check Box.Radio Button.Combo Box控件使用 使用控件的方法 1.拖动控件到对话框 2. 定义控件对应的变量(值变量或者控件变量) 3.响应控件各种消息 Check Box ...
- Linux系统无线网卡的安装【转】
转自:http://www.linuxidc.com/Linux/2013-03/81473.htm 现在的很多的可移动无线网卡都是usb接口的,把这种网卡应用到windows操作系统上是很容易就能起 ...
- xpath与nodejs解析xml
测试xpath的工具 http://www.freeformatter.com/xpath-tester.html#ad-output http://www.xpathtester.com/test ...
- rsync同步(winxdows到linux/linux到linxu同步)
1.什么是rsync? -rsync是类unix系统下的数据镜像备份工具——remote sync.一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH.rsync主 ...