首页 > 其他分享 >AcWing 3549. 最长非递减子序列

AcWing 3549. 最长非递减子序列

时间:2023-05-06 13:22:52浏览次数:45  
标签:int s2 s1 3549 读入 序列 递减 转移 AcWing

\(AcWing\) \(3549\). 最长非递减子序列

一、题目描述

给定一个长度为 \(n\) 的数字序列 \(a_1,a_2,…,a_n\),序列中只包含数字 \(1\) 和 \(2\)。

现在,你要选取一个区间 \([l,r](1≤l≤r≤n)\),将 \(a_l,a_{l+1},…,a_r\) 进行翻转,并且使得到的新数字序列 \(a\) 的最长非递减子序列的长度尽可能长。

请问,这个最大可能长度是多少?

一个非递减子序列是指一个索引为 \(p_1,p_2,…,p_k\)的序列,满足 \(p_1<p_2<…<p_k\) 并且 \(a_{p1}≤a_{p2}≤…≤a_{pk}\),其长度为 \(k\)。

输入格式

第一行一个整数 \(n\)。

第二行 \(n\)个空格隔开的数字 \(1\) 或 \(2\),表示 \(a_1,…,a_n\)。

输出格式

输出一个整数,表示得到的新数字序列 \(a\) 的最长非递减子序列的最大可能长度。

数据范围

对于 \(30\%\) 的数据,\(1≤n≤100\)。
对于 \(100\%\) 的数据,\(1≤n≤106\)。

本题读入数据规模较大,需注意优化读入。
\(C++\) 尽量使用 \(scanf\) 读入,\(Java\) 尽量使用 \(BufferedReader\) 读入。

输入样例\(1\):

4
1 2 1 2

输出样例1

4

输入样例2

10
1 1 2 2 2 1 1 2 2 1

输出样例2

9

二、从简化题目出发(求只含12的序列长度)

首先我是考虑的求出该序列的最长非递减子序列

其实只有 \(2\) 种状态

  • \(1111111\)…
    只可能由 \(111111\)… 这种状态转移而来

  • \(111111112222222222\)…
    可能由 \(11111111\)… 转移而来
    也可能由 \(111112222\)… 转移而来

\(Code\)

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int s1 = 0, s2 = 0;
    while (n--) {
        int x;
        cin >> x;
        if (x == 1) {
            s1++; // 如果当前数字是 1,则状态1的长度加1
        } else {
            s2 = max(s1 + 1, s2 + 1); // 如果当前数字为2,可能由两种状态转移而来
        }
    }
    cout << max(s1, s2) << endl;
    return 0;
}

三、回归题目(包括反转)

对于本题目而言,比上述的简单模型多加了一个条件:可以进行反转操作。

这就意味着,我们可以求一个形如:\(111111222222111111222222\)的子序列,然后进行反转操作让其变成 \(1111111112222222\)…的子序列

现在我们开始枚举状态:

  • \(1111111\)....
    只能通过 \(111111\)…转移

  • \((111111)2…\)
    可以通过 \(1111111\)… 转移(仅限于转移到 \(111111111111..2\) 这种状态)
    也可通过 \((11111)2222222\)…转移

  • \((111111122222)11111\)…
    可以通过 \(11111111222222\)… 转移(仅限于转移到 \(11111112222222..1\) 状态)
    也可以通过 \((111111122222)11111\)…转移

  • \((111111122222221111111)22222\)…

    • 可以通过 \((111112222)111\)… 转移(仅限于转移到 \(1111222222111111..2\) 状态)
    • 也可以通过 \((111111122222221111111)22222\)… 转移

\(Code\)

#include <bits/stdc++.h>
using namespace std;

int main() {
    // 加快读入
    ios::sync_with_stdio(false), cin.tie(0);
    int s1 = 0, s2 = 0, s3 = 0, s4 = 0;
    int n;
    cin >> n;
    while (n--) {
        int x;
        cin >> x;
        if (x == 1) {
            s1++;
            s3 = max(s2 + 1, s3 + 1);
        } else {
            s2 = max(s1 + 1, s2 + 1);
            s4 = max(s3 + 1, s4 + 1);
        }
    }
    cout << max({s1, s2, s3, s4}) << endl;
    return 0;
}

标签:int,s2,s1,3549,读入,序列,递减,转移,AcWing
From: https://www.cnblogs.com/littlehb/p/17376967.html

相关文章

  • AcWing 1209. 带分数
    1-暴力解法思考1:暴力列举出1~9的全排列,之后再将这些数字按照一定规则相加,最后将结果与n比较。全排列好写,但相加的规则不好写,而且太暴力了,估计会超时。/*AcWing1209.带分数00.最暴力的写法1.枚举全排列2.枚举位数(枚举a和b,可算出c)3.直接算出n,判断等......
  • AcWing 754. 平方矩阵 II
    AcWing754.平方矩阵II1.地址https://www.acwing.com/problem/content/756/2.题解#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;//每个元素的值为:各个元素下标相减的绝对值+1intmain(){intmatrix[102][102];intn;......
  • AcWing 753. 平方矩阵 I
    AcWing753.平方矩阵I1.地址https://www.acwing.com/problem/content/755/2.题解#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;intmain(){intm[101][101];//该题的思路://判断每个点距离上下左右四条边的最小值,......
  • AcWing 727. 菱形
    AcWing727.菱形1.地址https://www.acwing.com/problem/content/description/729/2.题解#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;//这道题需要用到曼哈顿距离//通过找规律发现,如果某一点跟中心点(n/2,n/2)的曼哈顿距离<......
  • AcWing 726. 质数
    AcWing726.质数1.地址https://www.acwing.com/problem/content/728/2.题解//此题跟完全数这道题差不多#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;intmain(){intcount;intnumber;intflag=1;sc......
  • AcWing 725. 完全数
    AcWing725.完全数1.地址https://www.acwing.com/problem/content/description/727/2.题解#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;//注意:这道题如果暴力解法一定TLE//因此,我们需要对其进行优化intmain(){intn;......
  • AcWing 4086 分糖果
    关于这道题我当时大意了https://www.acwing.com/problem/content/description/4089/关于我的某个变量没有初始化这件事,唯一想法,敲死得了,谁懂?其实就是一道简简单单的数学分析题,和大佬们不一样,萌新只会简简单单的小学数学(本人初二!)分析走起! 一道典型的数学问题() 虽然我WA了,......
  • AcWing 656. 钞票和硬币
    AcWing656.钞票和硬币1.地址https://www.acwing.com/problem/content/658/2.解答#include<iostream>#include<cstdio>usingnamespacestd;intmain(){intmoney[6]={100,50,20,10,5,2};doublecoins[6]={1.0,0.50,0.25,0.10,0.05,0.01};......
  • 证明 一个 和 三角函数 有关 的 函数 单调递减
    吧主 @黎合胜  只会出物理题,  数学题还得我来,  嘿嘿 。 数学吧  《三角函数问题》     https://tieba.baidu.com/p/8384790600   。  ......
  • AcWing 242. 一个简单的整数问题 / 树状数组区间修改区间查询模板题
    AcWing242.一个简单的整数问题//实例化是抽象的天敌,是抽象的克星//通过公式sn=(i从1~n求积)di*(1+n)-(i从1~n求积)i*di//来计算前缀和,又(i从1~n求积)i*di不能由(i从1~n求积)di*(1+n)推出//所以除了维护d数组,还需维护......