Codeforces 933A Twisty Movement

题目链接http://codeforces.com/problemset/problem/933/A

题意:给你一个由12组成的序列,翻转一个区间使得这个序列的最长上升子序列的长度最长。

思路:记录四个信息:1的个数前缀和pre1[i]1的个数后缀和pre2[i]2的个数前缀和suff1[i]2的个数后缀和suff2[i],然后我们枚举区间[L,R],那么我们的答案就是max(pre1[L-1]+suff2[R+1]+pre2[i]-pre2[L-1]+suff1[i+1]-suff[R+1])。其中i为我们枚举的断点,i左边的都是1i右边的都是2,我们要使答案最大化,无疑就是找出max(pre2[i]+suff1[i+1]),这个用线段树维护即可,剩下的就是枚举所有区间更新答案了,时间复杂度O(n^2logn)

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define ALL(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
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;
}
template <class T1, class T2>inline void gmax(T1 &a,T2 b){if (b>a) a=b;}
template <class T1, class T2>inline void gmin(T1 &a,T2 b){if (b<a) a=b;}
void up(int&x,int y){x+=y;if(x>=P)x-=P;}
const int N=2000+10;
int n,i,j,a[N],pre1[N],pre2[N],suff1[N],suff2[N],mx[N<<2];
void pushup(int root){mx[root]=max(mx[root<<1],mx[root<<1|1]);}
void build(int root,int l,int r){
    if(l==r){
        mx[root]=pre2[l]+suff1[l+1];
        return;
    }
    int mid=l+((r-l)>>1);
    build(lson);
    build(rson);
    pushup(root);
}
int query(int root,int l,int r,int L,int R){
    if (L<=l&&r<=R) return mx[root];
    int mid=l+((r-l)>>1);
    int ret=0;
    if (L<=mid) ret=max(ret,query(lson,L,R));
    if (mid<R) ret=max(ret,query(rson,L,R));
    return ret;
}
int main(){
    read(n);
    for (i=1;i<=n;i++) read(a[i]);

    for (i=1;i<=n;i++) pre1[i]=pre1[i-1]+(a[i]==1);
    for (i=1;i<=n;i++) pre2[i]=pre2[i-1]+(a[i]==2);
    for (i=n;i>=1;i--) suff1[i]=suff1[i+1]+(a[i]==1);
    for (i=n;i>=1;i--) suff2[i]=suff2[i+1]+(a[i]==2);

    build(1,1,n);
    int ans=max(pre1[n],pre2[n]);
    for (i=1;i<=n;i++){
        ans=max(ans,pre1[i]+suff2[i+1]);
    }
    for (i=1;i<=n;i++){
        for (j=i+1;j<=n;j++){
            int tmp=query(1,1,n,i,j-1);
            ans=max(ans,pre1[i-1]+suff2[j+1]-pre2[i-1]-suff1[j+1]+tmp);
        }
    }
    printf("%d\n",ans);
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注