[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 ...
随机推荐
- 「bzoj1925」「Sdoi2010」地精部落 (计数型dp)
「bzoj1925」「Sdoi2010」地精部落---------------------------------------------------------------------------- ...
- echarts Y轴数据类型不同怎么让折线图显示差距不大
如果希望在同一grid中展示不同数据类型的折线(1000或10%),那么展现出来的折线肯定显示差距很大,那么怎么让这两条折线显示效果差不多,在之前的项目中碰到了这个问题 每条折线对应的是不同的数据组, ...
- 阿里Tree-based Deep Match(TDM) 学习笔记
阅读文献:https://zhuanlan.zhihu.com/p/35030348 参考文献:https://www.leiphone.com/news/201803/nlG3d4sZnRvgAqg ...
- python 爬虫系列02-小说
本爬虫为网络上的.. # # -*- coding:UTF-8 -*- # from bs4 import BeautifulSoup # import requests # if __name__ ...
- unity文件 PlayerPrefs.SetInt 保存 And PlayerPrefs.GetInt读取
unity文件保存读取PlayerPrefs.SetInt And PlayerPrefs.GetInt using UnityEngine; using System.Collections; ...
- 分享:JAVA和C# 3DES加密解密
最近 一个项目.net 要调用JAVA的WEB SERVICE,数据采用3DES加密,涉及到两种语言3DES一致性的问题,下面分享一下,这里的KEY采用Base64编码,便用分发,因为Java的Byt ...
- (Frontend Newbie) Web三要素(一)
上一篇简单了解了Web发展的简要历史,本篇简单介绍前端开发的基本三要素:HTML.CSS.JavaScript中的HTML以及一些在开发.学习过程中易被忽视的知识点. HTML HTML全称是超文本标 ...
- 数据库保存session
一般情况下,php.ini里的session.save_handler默认是file,也就是用文件来保存session,这种方式有几个缺点: 1.如果单靠session自己的垃圾回收机制,时间久了,保 ...
- File upload error - unable to create a temporary file
php上传图片的时候会报错: File upload error - unable to create a temporary file 文件上传错误 - 无法创建一个临时文件 你只需要打开你的php ...
- HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】
Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & ...