P3146 [USACO16OPEN]248 & P3147 [USACO16OPEN]262144
注:两道题目题意是一样的,但是数据范围不同,一个为弱化版,另一个为强化版。
P3146传送门(弱化版)
思路:
区间动规,设 f [ i ][ j ] 表示在区间 i ~ j 中获得的最大值,与普通区间动规最大的不同在于:只有左区间的最大值等于右区间的最大值时才能够进行转移。
AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define maxn 257
int n;
int maxx=-0x3f;//maxx记录合并后的最大值
int f[maxn][maxn];//f[i][j]表示 i~j 区间内合并后的最大值
inline int read()
{
char kr=;
char ls;
for(;ls>''||ls<'';kr=ls,ls=getchar());
int xs=;
for(;ls>=''&&ls<='';ls=getchar())
{
xs=xs*+ls-;
}
if(kr=='-') xs=-xs;
return xs;
}
int main()
{
n=read();
for(int i=;i<=n;i++)
{
f[i][i]=read();
maxx=max(f[i][i],maxx);
}
for(int i=;i<=n;i++)// i 枚举区间长度
{
for(int j=;j+i-<=n;j++)//枚举左端点
{
int r=i+j-;//计算出右端点
for(int k=j;k<r;k++)//枚举断点
{
if(f[j][k]==f[k+][r])//如果断点的左右两边最大值相等,转移
{
f[j][r]=max(f[j][r],f[j][k]+);
maxx=max(maxx,f[j][r]);//记录最大值
}
}
}
}
printf("%d\n",maxx);//输出
return ;
}
P3147传送门(强化版)
思路:
在数据范围 2~262144 的情况下,使用区间动规在空间和时间上就有点吃不消了。这是我们考虑更加优化的动规,可以设 f [ i ][ j ] 表示从 j 开始合并到 i 这个数字序列的末尾的下标是什么。那么因为合并的总是一段连续的区间,就有 f [ i ][ j ] = f [ i-1 ][ f [ i-1 ][ j ] ];
AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define maxn 300000
int n,a;
int ans=-0x3f;
int f[][maxn];
inline int read()
{
char kr=;
char ls;
for(;ls>''||ls<'';kr=ls,ls=getchar());
int xs=;
for(;ls>=''&&ls<='';ls=getchar())
{
xs=(xs<<)+(xs<<)+ls-;
}
if(kr=='-') xs=-xs;
return xs;
}
int main()
{
n=read();
for(int i=;i<=n;i++)
{
a=read();
f[a][i]=i+;
}
for(int i=;i<=;i++)
{
for(int j=;j<=n;j++)
{
if(!f[i][j]) f[i][j]=f[i-][f[i-][j]];
if(f[i][j]) ans=i;
}
}
printf("%d\n",ans);
return ;
}
P3146 [USACO16OPEN]248 & P3147 [USACO16OPEN]262144的更多相关文章
- P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144一道非常有趣的游戏,不,题目.当数据水时,可以这样表示状态.f[i][j]表示合并[i,j]区间所能得到的最大值,有点floyed的小味道.if(f[ ...
- 洛谷P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- 洛谷P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 洛谷 P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- 洛谷 P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- P3146 [USACO16OPEN]248
P3146 [USACO16OPEN]248 题解 第一道自己码出的区间DP快庆祝一哈 2048 每次可以合并任意相邻的两个数字,得到的不是翻倍而是+1 dp[L][R] 区间 L~R 合并结果 然后 ...
- [USACO16OPEN]248 G——区间dp
[USACO16OPEN]248 G 题目描述 Bessie likes downloading games to play on her cell phone, even though she do ...
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
- 一道另类的区间dp题 -- P3147 [USACO16OPEN]262144
https://www.luogu.org/problemnew/show/P3147 此题与上一题完全一样,唯一不一样的就是数据范围; 上一题是248,而这一题是262144; 普通的区间dp表示状 ...
随机推荐
- LIS最长上升子序列三种方法 (模板)
O(n^)的方法: #include <iostream> #include <stdio.h> #include <cstring> #include <a ...
- bzoj4448 情报传递
题目链接 离线+树上主席树,主席树维护时间标记 注意查询时如果c<0要把c赋为0: #include<iostream> #include<cstdio> #includ ...
- [转载]谈谈document.ready和window.onload的区别
在Jquery里面,我们可以看到两种写法:$(function(){}) 和$(document).ready(function(){}) 这两个方法的效果都是一样的,都是在dom文档树加载完之后执行 ...
- JQuery ajax请求返回(parsererror)异常处理
目前在学习一个Java应用的框架,反编译后在执行时一直报错,界面上显示”parsererror”,经过JavaScript调试后发现更详细的错误提示信息是 Unexpected token ' in ...
- c#测试执行时间的方法
获取当前实例测量出来的总的运行时间 Stopwatch sp = new Stopwatch(); sp.Start(); //要测试的代码块 sp.Stop(); Console.WriteLine ...
- eclipse 的版本及下载地址
eclipse 的各个版本号: 版本号 代号 代号名 发布日期 Eclipse 3.1 IO 木卫一,伊奥 2005 Eclipse 3.2 Callisto 木卫四,卡里斯托 2006 Eclips ...
- e3.7.2-MyEclipse-10.7安装SVN插件
MyEclipse 10.7的版本是:e3.7.2,要求是匹配该插件eclipse_svn_site-1.10.1的版本,否则无效 将eclipse_svn_site-1.10.1插件文件夹直接拷贝到 ...
- linux下can调试工具canutils安装过程记录
https://www.cnblogs.com/chenfulin5/p/6797756.html 一.下载源码 下载canutils和libsocketcan libsocketcan地址:http ...
- GMIS 2017 大会陈雨强演讲:机器学习模型,宽与深的大战
https://blog.csdn.net/starzhou/article/details/72819374 2017-05-27 19:15:36 GMIS 2017 10 0 5 ...
- Java基础再复习(继承、多态、方法内部类**、HashMap用法**、参数传递**)
###继承: package com.shiyan; public class Animal { public int legNum; //动物四肢的数量 //类方法 public void bark ...