CF 462 C. A Twisty Movement 分段想 线段树 或 dp

题意

有一个只包含1和2的序列,试翻转一个区间,使得结果中非连续非递减数列最长。

思路

一、

作出1的前缀计数和为cnt1,2的后缀计数和为cnt2, 由于要找出【1,1,1】【2,2,2】【1,1,1】【2,2,2】的四段,设中间的分割点是p,k,q,可得到

ans=cnt1[p]+cnt2[p+1]−cnt2[k+1]+cnt1[q]−cnt1[k]+cnt2[q+1]ans=cnt1[p]+cnt2[p+1]cnt2[k+1]+cnt1[q]cnt1[k]+cnt2[q+1]

化简得到

ans=cnt1[p]+cnt2[p+1]+cnt1[q]+cnt2[q+1]−cnt1[k]−cnt2[k+1]ans=cnt1[p]+cnt2[p+1]+cnt1[q]+cnt2[q+1]cnt1[k]cnt2[k+1]

所以我们可以枚举k, 用线段树维护cnt1【i】 + cnt2【i+1】 的最大值,我这样的公式,需要线段树区间为【0,n】。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

/*
        
⊂_ヽ
  \\ Λ_Λ  来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ

*/

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "
";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '
'

#define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);


const ll oo = 1ll<<17;
const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}

inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}

/*-----------------------showtime----------------------*/
            const int maxn = 2009;
            int a[maxn],cnt1[maxn],cnt2[maxn];
            int t[maxn<<2];

            void build(int l,int r,int rt){
                if(l == r){
                    t[rt] = cnt1[l] + cnt2[l+1];
                    return ;
                }
                int mid = (l + r) >> 1;
                build(l, mid, rt<<1);
                build(mid+1,r,rt<<1|1);
                t[rt] = max(t[rt<<1], t[rt<<1|1]);
            }
            int query(int L,int R,int l,int r,int rt){
                if(l >= L && r <= R){
                    return t[rt];
                }
                int mx = 0;
                int mid = (l + r) >> 1;
                if(mid >= L) mx = max(mx, query(L, R, l, mid, rt<<1));
                if(mid < R) mx = max(mx, query(L, R, mid+1, r, rt<<1|1));
                return mx;
            }
int main(){
            int n;  scanf("%d", &n);
            rep(i, 1, n) scanf("%d", &a[i]);

            rep(i, 1, n){
                if(a[i] == 1) cnt1[i] = cnt1[i-1] + 1;
                else cnt1[i] = cnt1[i-1];
            }
            for(int i=n; i>=1; i--) {
                if(a[i] == 2) cnt2[i] = cnt2[i+1] + 1;
                else cnt2[i] = cnt2[i+1];
            }

            build(0, n, 1);
            int ans = 1;
            for(int i=1; i<=n; i++){
                int tmp = query(0,i ,0,n,1) + query(i,n,0,n,1) - cnt1[i] - cnt2[i+1];
                ans = max(ans, tmp);
            }
            cout<<ans<<endl;
            return 0;
}
View Code

二、

这道题中分四段是关键,我们可以定义dp【i】【1】表示1~i个分一段的答案,dp【i】【2】表示分两段,dp【i】【3】表示分三段,dp【i】【4】表示分四段。

/*-----------------------showtime----------------------*/

            int dp[5];
int main(){
            int n;  scanf("%d", &n);
            rep(i, 1, n) {
                int x;  scanf("%d", &x);
                dp[1] = dp[1] + (x == 1);
                dp[2] = max(dp[1], dp[2] + (x==2));
                dp[3] = max(dp[2], dp[3] + (x==1));
                dp[4] = max(dp[3], dp[4] + (x==2));
            }
            cout<<dp[4]<<endl;


}
View Code

原文地址:https://www.cnblogs.com/ckxkexing/p/10367409.html