题目链接:点击查看

题目大意:给出一个长度为 n 的数列,只由 1 和 2 组成,现在允许反转一段区间,问反转后的不下降子序列最长是多少

题目分析:因为 n 只有 2000,所以考虑n2n^2n2去枚举所有的子区间进行反转然后维护答案,不过我们需要预处理出两个dp数组备用:

  1. dp1[ l ][ r ][ st ][ ed ]:区间 [ l , r ] 中首项为 st,末项为 ed 时最长不下降子序列的长度
  2. dp2[ l ][ r ][ st ][ ed ]:意义同上,只不过是反转之后的答案

在枚举子区间 [ l , r ] 时,整个数组很自然的被分成了三段:

  1. [ 1 , l - 1 ]
  2. [ l , r ]
  3. [ r + 1 , n ]

再枚举一下上面六个端点相应的取值,直接转移就好了

代码:

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2100;int a[N];int dp1[N][N][2][2];//dp[l][r][st][ed]:区间[l,r]内,起点为st,终点为ed的最长不下降int dp2[N][N][2][2];//倒着的答案int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",a+i);a[i]--;}for(int i=1;i<=n;i++)//枚举起点for(int j=i;j<=n;j++){for(int st=0;st<=1;st++)//不选a[j]for(int ed=st;ed<=1;ed++)dp1[i][j][st][ed]=dp1[i][j-1][st][ed];for(int st=0;st<=1;st++)//选a[j]for(int ed=st;ed<=a[j];ed++)dp1[i][j][st][a[j]]=max(dp1[i][j][st][a[j]],dp1[i][j-1][st][ed]+1);}for(int j=n;j>=1;j--)//枚举终点for(int i=j;i>=1;i--){for(int st=0;st<=1;st++)//不选a[i]for(int ed=st;ed<=1;ed++)dp2[i][j][st][ed]=dp2[i+1][j][st][ed];for(int st=0;st<=1;st++)for(int ed=st;ed<=a[i];ed++)dp2[i][j][st][a[i]]=max(dp2[i][j][st][a[i]],dp2[i+1][j][st][ed]+1);}int ans=0;for(int st=0;st<=1;st++)for(int ed=st;ed<=1;ed++)ans=max(ans,dp1[1][n][st][ed]);for(int l=1;l<=n;l++)for(int r=l;r<=n;r++){for(int a=0;a<=1;a++)for(int b=a;b<=1;b++)for(int c=b;c<=1;c++)for(int d=c;d<=1;d++)for(int e=d;e<=1;e++)for(int f=e;f<=1;f++)ans=max(ans,dp1[1][l-1][a][b]+dp2[l][r][c][d]+dp1[r+1][n][e][f]);}printf("%d\n",ans);return 0;
}

CodeForces - 933A A Twisty Movement(dp)相关推荐

  1. Codeforces Round #462 (Div. 2) C. A Twisty Movement dp + 思维转换

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的只包含1,21,21,2的序列aaa,你可以至多翻转一段区间,求翻转之后最长非递减子序列是多长. 思路: 考虑如果翻转的话,翻转的子区间 ...

  2. Codeforces 934C - A Twisty Movement

    934C - A Twisty Movement 思路:dp 很容易想到要预处理出1的前缀和pre[i]和2的后缀和suf[i] 然后枚举区间,对于每个区间如果能求出最长递减序列的长度,那么就能更新答 ...

  3. [2.7]【CF933A】A Twisty Movement【CF926B】Add Points【CF917A】The Monster【CF919E】Congruence Equation

    文章目录 T1:A Twisty Movement 题目 题解 code T2:Add Points 题目 题解 code T3:The Monster 题目 题解 code T4:Congruenc ...

  4. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

  5. 【CodeForces 1253C --- Sweets Eating】DP

    [CodeForces 1253C --- Sweets Eating]DP Description Tsumugi brought n delicious sweets to the Light M ...

  6. Codeforces Round #462 (Div. 1) A Twisty Movement -12序列的LIS

    题目链接:https://codeforces.com/problemset/problem/933/A 题目大意: 在一个只包含 1,2 的序列中,翻转其中任意一个区间,求此时最大的 LIS . 一 ...

  7. Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)...

    题目意思: 给长度为n(n<=2000)的数字串,数字只能为1或者2,可以将其中一段区间[l,r]翻转,求翻转后的最长非递减子序列长度. 题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最 ...

  8. codeforces Palindromic characteristics(hash或者dp)

    1.动态规划 用dp(l,r)表示子串s[l..r]的回文串阶数.对于长度len为1的有dp(l,r)=1.对于长度len等于2的,看字符串左右是否相等即可.当r-l>1时,如果s[l]不等于s ...

  9. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

最新文章

  1. Linux进程地址空间与进程内存布局详解,内核空间与用户空间
  2. java多线程同步与死锁,廖雪峰Java11多线程编程-2线程同步-3死锁
  3. java编码给出二维数组List<List<Integer>>matrix,输出每列最小的值
  4. chrome浏览器隐藏地址栏_Chrome将隐藏地址栏的网址 这么做的原因是为了防止钓鱼网址...
  5. salesforce 学习(超简介,以及传送门)
  6. python实例化次数怎么算_关于python多次实例化
  7. Windows Server 2008 Active Directory建置實務(附CD)
  8. 学习 Python 第八天
  9. 最早会外语的人,是怎么回事
  10. php处理数组函数大全
  11. scipy短时傅里叶分析STFT
  12. diff和patch工具打补丁
  13. Mybatis事务失效的几种情况
  14. 爱奇艺视频怎么下载,如何将qsv格式转为mp4格式
  15. 如何将pdf转换成jpg图片的格式
  16. android recocery模式,recovery模式怎么进入 recovery菜单翻译
  17. SIMULINK 微网控制 包括PQ控制,下垂控制,VSG控制
  18. 怎么从cad的表格弄到excel里转换成Excel表格?
  19. 高斯消去法(c/c++语言)
  20. Cef语言(Locales)本地化问题

热门文章

  1. mysql 配置文件
  2. RocketMQ消息支持的模式-消息同步发送
  3. TCP/IP的四层负载均衡
  4. CAS单点登录 - 用户登录与校验
  5. Properties和IO流相结合的方法
  6. SpringMVC的请求-获得请求参数-获得数组类型参数
  7. 切点方法的事务参数的配置
  8. request获得请求头
  9. 事件绑定中的this问题
  10. 单例设计模式-容器单例