[USACO17JAN]Subsequence Reversal
这题刚开始是什么思路也没有,关键是不知道怎么解决序列反转的问题。
然后我就想到如果暴力反转一个序列的话,实际上就是不断交换数组中的两个数ai和aj,同时要满足交换的数不能交叉。
然后又看了一眼(岂止一眼)题解,因为ai <= 50,所以令dp[i][j][L][R]表示区间[i, j],min(ak) >= L, max(ak) <= R时,反转一次的最长不下降子序列。
显然是一个区间dp,那么[i, j]可以从[i + 1, j],[i, j - 1]或是[i + 1, j - 1]转移过来,所以L,R也只可能从ai+1,aj-1转移过来。然后还要考虑交换或者不交换的情况。代码就是dp式了,这里就不写了
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, a[maxn];
int dp[maxn][maxn][maxn][maxn]; int main()
{
n = read();
for(int i = ; i <= n; ++i) a[i] = read(), dp[i][i][a[i]][a[i]] = ;
for(int len = ; len <= n; ++len)
for(int i = ; i + len - <= n; ++i)
{
int j = i + len - ;
for(int l = ; l <= ; ++l)
for(int L = ; L + l - <= ; ++L)
{
int R = L + l - ;
dp[i][j][L][R] = max(dp[i][j][L][R], max(dp[i + ][j][L][R], dp[i][j - ][L][R]));
dp[i][j][L][R] = max(dp[i][j][L][R], max(dp[i][j][L + ][R], dp[i][j][L][R - ]));
dp[i][j][min(L, a[i])][R] = max(dp[i][j][min(L, a[i])][R], dp[i + ][j][L][R] + (a[i] <= L));
dp[i][j][L][max(R, a[j])] = max(dp[i][j][L][max(R, a[j])], dp[i][j - ][L][R] + (a[j] >= R));
dp[i][j][min(L, a[j])][R] = max(dp[i][j][min(L, a[j])][R], dp[i + ][j - ][L][R] + (a[j] <= L)); //一下三行是ai和aj交换
dp[i][j][L][max(R, a[i])] = max(dp[i][j][L][max(R, a[i])], dp[i + ][j - ][L][R] + (a[i] >= R));
dp[i][j][min(L, a[j])][max(R, a[i])] = max(dp[i][j][min(L, a[j])][max(R, a[i])], dp[i + ][j - ][L][R] + (a[i] >= R) + (a[j] <= L));
}
}
write(dp[][n][][]), enter;
return ;
}
[USACO17JAN]Subsequence Reversal的更多相关文章
- [USACO17JAN]Subsequence Reversal序列反转
题目描述 Farmer John is arranging his NN cows in a line to take a photo (1 \leq N \leq 501≤N≤50). The he ...
- [USACO17JAN] Subsequence Reversal序列反转 (dfs+记忆化)
题目大意:给你一个序列,你可以翻转任意一段子序列一次,求最长不下降子序列长度 tips:子序列可以不连续,但不能破坏在原序列中的顺序 观察数据范围,n<=50,很小,考虑dfs *dfs来跑区间 ...
- bzoj4758: [Usaco2017 Jan]Subsequence Reversal(区间dp)
4758: [Usaco2017 Jan]Subsequence Reversal Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 76 Solved ...
- USACO 2017 January Platinum
因为之前忘做了,赶紧补上. T1.Promotion Counting 题目大意:给定一个以1为根的N个节点的树(N<=100,000),每个节点有一个权值,对于每个节点求出权值比它大的子孙的个 ...
- 区间dp提升复习
区间\(dp\)提升复习 不得不说这波题真的不简单... 技巧总结: 1.有时候转移可以利用背包累和 2.如果遇到类似区间添加限制的题可以直接把限制扔在区间上,每次只考虑\([l,r]\)被\([i, ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
随机推荐
- keepalived+lvs简单实现
一,部署实战环节 01,服务架构图: 服务器镜像为centos6.9 02,服务安装: 10.0.0.10: 下载keeplived官方包--> http://www.keepalived. ...
- TOJ 1258 Very Simple Counting
Description Let f(n) be the number of factors of integer n. Your task is to count the number of i(1 ...
- 九度oj题目1518:反转链表
题目1518:反转链表 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2567 解决:948 题目描述: 输入一个链表,反转链表后,输出链表的所有元素.(hint : 请务必使用链表) ...
- LaTex 2
LaTex 入门 此时是否安装成功 如果安装成功了LaTeX, 那么在计算机上会多出来LaTeX的编译器, LaTex Live 安装包在计算机上安装了多个不同的编译器, 有latex, xelate ...
- dojo入门
1.引入dojo.js dojo的发行包里有4个子目录,要引入的文件是名叫"dojo"的子目录里的dojo.js. 假设你是这样的目录结构: project | +--dojo-l ...
- 深入理解JavaScript系列(27):设计模式之建造者模式
介绍 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定. ...
- Win10新建分区
今天忽然觉得将系统分为四个盘,有点无法将分类分开,所以增加了几个分区: 1.windows+X键在弹出的对话框中选择磁盘管理,进入如下界面: 2.如果你想从某个盘分出一些内存建立一个新的分区,就在这个 ...
- Nginx下载安装
系统环境:win7 nginx:1.11.4 1.下载Nginx 下载地址:http://nginx.org/en/download.html 2.将压缩包解压到相应位置 3.启动nginx服务,ng ...
- 跨页面传值之QueryString
跨页面传值常用方法 1.QueryString 2.Form-post控件传递 3.Cookies传递 4.Application传递 5.Session传递(灵活强大) 1.query传值 http ...
- 文件读取工具类读取properties文件
1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...