Topcoder SRM 654 Div2 1000

Problem

给一个长度为 NN[1,2000] 的数列 AnAn[100,100] ,设 ans=A1A2...An ,下面进行 MM[1,2000] 次操作,每次将 A p[i] 的值修改为 v[i] ,即 A[p[i]]=v[i] ,每次只允许加入最多2个括号进入 ans 等式,问ans的最大值可以是多少?

Solution

dp.. 设dp[i][j]表示从 1 到 i-1 已经有 j 个括弧时候的最大值,j=0时表示没有括弧,j=1时表示(这样子,j=2时表示(( 这个样子,j=3时表示(() 这个样子,j=4时表示(())这个样子。。没了。最后只需要统计 dp[n+1][j] (0<=j<=4)的最大值即可。

My Code

//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
const double pi=acos(-1.0);
//const double eps=1e-9;
#define MOD 1000000007
#define MAXN 3000100
#define M
int dp[2010][10];
class SuccessiveSubtraction2{
public:
    int solve(vector<int>&a){
        for(int i=0;i<=a.size();i++){
            for(int j=0;j<=4;j++){
                dp[i][j]=-(1<<30);
            }
        }
        dp[0][0]=0;
        for(int i=0;i<a.size();i++){
            int x=(i?-a[i]:a[i]);
            dp[i+1][0]=max(dp[i+1][0],dp[i][0]+x);
            if(i>=2){
                dp[i+1][1]=max(dp[i+1][1],dp[i][0]-x);
                dp[i+1][1]=max(dp[i+1][1],dp[i][1]-x);
                dp[i+1][2]=max(dp[i+1][2],dp[i][1]+x);
                dp[i+1][2]=max(dp[i+1][2],dp[i][2]+x);
                dp[i+1][3]=max(dp[i+1][3],dp[i][2]-x);
                dp[i+1][3]=max(dp[i+1][3],dp[i][3]-x);
                dp[i+1][4]=max(dp[i+1][4],dp[i][3]+x);
                dp[i+1][4]=max(dp[i+1][4],dp[i][4]+x);
            }
        }
        int ret=-(1<<30);
        for(int i=0;i<=4;i++){
            ret=max(ret,dp[a.size()][i]);
        }
        return ret;
    }
    vector <int> calc(vector <int> a, vector <int> p, vector <int> v){
        int lenp=gsize(p);
        vector<int>res;
        res.clear();
        rep(i,0,lenp){
            a[p[i]]=v[i];
            int ans=solve(a);
            res.push_back(ans);
        }
        return res;
    }
};

你可能感兴趣的:(dp)