嘟嘟嘟

这题刚开始是什么思路也没有,关键是不知道怎么解决序列反转的问题。

然后我就想到如果暴力反转一个序列的话,实际上就是不断交换数组中的两个数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的更多相关文章

  1. [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 ...

  2. [USACO17JAN] Subsequence Reversal序列反转 (dfs+记忆化)

    题目大意:给你一个序列,你可以翻转任意一段子序列一次,求最长不下降子序列长度 tips:子序列可以不连续,但不能破坏在原序列中的顺序 观察数据范围,n<=50,很小,考虑dfs *dfs来跑区间 ...

  3. bzoj4758: [Usaco2017 Jan]Subsequence Reversal(区间dp)

    4758: [Usaco2017 Jan]Subsequence Reversal Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 76  Solved ...

  4. USACO 2017 January Platinum

    因为之前忘做了,赶紧补上. T1.Promotion Counting 题目大意:给定一个以1为根的N个节点的树(N<=100,000),每个节点有一个权值,对于每个节点求出权值比它大的子孙的个 ...

  5. 区间dp提升复习

    区间\(dp\)提升复习 不得不说这波题真的不简单... 技巧总结: 1.有时候转移可以利用背包累和 2.如果遇到类似区间添加限制的题可以直接把限制扔在区间上,每次只考虑\([l,r]\)被\([i, ...

  6. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  7. [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 ...

  8. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  9. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

随机推荐

  1. 图解 I帧,B帧以及P帧

    I‑frame (Intra-coded picture): 即完整的一张图片 P‑frame (Predicted picture): 与前面一张图片的区别的区域 B‑frame (Bidirect ...

  2. Python 的 __new__()方法与实例化

    __new__() 是新式类中才有的方法,它执行在构造方法创建实例之前.可以这么理解,在 Python 中类中的构造方法 __init__() 负责将类实例化,而在 __init__() 启动之前,_ ...

  3. ArrayList,LinkList,HashMap

    ArrayList底层实现数组,这是ArrayList get()方法的源码,底层是数组 根据下标返回在数组中对应的位置 ,查询快,插入慢 // Positional Access Operation ...

  4. 机器学习——XGBoost

    基础概念 XGBoost(eXtreme Gradient Boosting)是GradientBoosting算法的一个优化的版本,针对传统GBDT算法做了很多细节改进,包括损失函数.正则化.切分点 ...

  5. [转] CentOS下添加用户并且让用户获得root权限

    http://www.centoscn.com/CentOS/config/2014/0810/3471.html 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser ...

  6. html和css(一)

    简单点来说html和css就是一起连在使用,有了html和css会使网页更加有活力,看起来更加的好看. html是做关于网页标签这一块相当于骨架,更深的还需要另一个来完成,那就是css,相当于向里面加 ...

  7. asp 2.0 ajax triggers 触发更新

  8. html元素固定

    1.position 值 描述 static        默认.位置设置为 static 的元素,它始终会处于页面流给予的位置(static 元素会忽略任何 top.bottom.left 或 ri ...

  9. Backbone事件机制核心源码(仅包含Events、Model模块)

    一.应用场景 为了改善酷版139邮箱的代码结构,引入backbone的事件机制,按照MVC的分层思想搭建酷版云邮局的代码框架.力求在保持酷版轻量级的基础上提高代码的可维护性.   二.遗留问题 1.b ...

  10. Cocos2d-js 开发记录:图片数据资源等的异步加载

    这里说的是在需要的使用加载图片,比如游戏中的某个关卡的图片,不用在游戏一开始就加载(万一用户玩不到那关,岂不是很冤,流量费了那么多),否则载入速度也慢.这种方式加载资源要用到cc.loader官方文档 ...