题解:P6713 [CCO 2018] Geese vs. Hawks

· · 题解

一道 LCS 的练手题。

思路

$valh_i$ 表示第 $i$ 场比赛**劳里埃金鹰队**的分数。 我们定义一个函数 check 判断这场比赛是不是**同城德比**。 手动模拟样例,容易发现这题很像 LCS 的模板,可以得到以下转移方程: - 继承上一个状态:$dp_{i,j}=\max(dp_{i-1,j},dp_{i,j-1})$。 - 如果可以是**同城德比**,$dp_{i,j}=\max(dp_{i,j},dp_{i-1,j-1}+valg_i+valh_j)$。 ### check 函数的实现 - 按照题意判断即可。 - 代码例子: ```cpp inline bool check(int gi,int hi){ if(valg[gi]>valh[hi]&&g[gi-1]=='W'&&h[hi-1]=='L') return 1; if(valh[hi]>valg[gi]&&g[gi-1]=='L'&&h[hi-1]=='W') return 1; return 0; } ``` 时间复杂度:$O(n^2)

代码

//#pragma GCC optimize("Ofast,unroll-loops")
#pragma g++ optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define F(i,j,k,l) for(int i=(j);i<=(k);i+=(l))
#define f(i,j,k,l) for(int i=(j);i>=(k);i-=(l))
// #ifdef __unix__
// #define gc getchar_unlocked
// #define pc putchar_unlocked
// #else
// #define gc _getchar_nolock
// #define pc _putchar_nolock
// #endif
#define W(i) while(i)
#define A auto
#define ot float
#define FO(x,a) for(int x:a)
#define Fo(x,a) for(A x:a)
#define el "\n"
#define ull unsigned long long
#define ll long long
#define in __int128
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define dle double
#define ld long double
#define imax INT_MAX
#define imin INT_MIN
#define llmax LONG_LONG_MAX
#define llmin LONG_LONG_MIN
#define debug(x) cerr<<#x<<" "<<x<<"\n";
const int N=1e3+10,M=1<<20;
char buf[M],*p1=buf,*p2=buf,out[M],*p3=out;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,M,stdin)),*p1++)
#define gc() (p1==p2?(p2=(p1=buf)+fread(buf,1,M,stdin),p1==p2?EOF:*p1++):*p1++)
//#define flush() do{fwrite(out,1,p3-out,stdout);p3=out;}while(0)
//#define pc(c) do{if(p3==(out+M)) flush();*p3++=(c);}while(0)
#define flush() (fwrite(out,1,p3-out,stdout),p3=out)
#define pc(c) (p3==(out+M)?(flush(),*p3++=(c)):*p3++=(c))
inline long long read(){
    long long x=0,f=1;
    char ch=gc();
    W(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=gc();
    }
    W(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=gc();
    }
    return x*f;
}
inline string rsline(){
    string s;
    char ch=gc();
    W(ch!='\n'&&ch!='\r'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline string rs(){
    string s;
    char ch=gc();
    W(ch==' '||ch=='\n'||ch=='\r'||ch=='\t') ch=gc();
    W(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline void write(long long x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x==0){
        pc('0');
        return;
    }
    char s[25];
    int len=0;
    W(x){
        s[len++]=char('0'+x%10);
        x/=10;
    }
    W(len) pc(s[--len]);
    return;
}
inline void ws(string &s){
    for(auto x:s) pc(x);
    return;
}
int dp[N][N];
int n,valg[N],valh[N];
string h,g;
inline bool check(int gi,int hi){
    if(valg[gi]>valh[hi]&&g[gi-1]=='W'&&h[hi-1]=='L') return 1;
    if(valh[hi]>valg[gi]&&g[gi-1]=='L'&&h[hi-1]=='W') return 1;
    return 0;
}
inline void pre(){

}
inline void input(){
    n=read();
    g=rs();
    F(i,1,n,1) valg[i]=read();
    h=rs();
    F(i,1,n,1) valh[i]=read();
}
inline void output(){
    F(i,1,n,1){
        F(j,1,n,1){
            dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            if(check(i,j)) dp[i][j]=max(dp[i][j],dp[i-1][j-1]+valg[i]+valh[j]);
        }
    }
    write(dp[n][n]);
}
inline void solve(){
    pre();
    input();
    output();
    return;
}
int main(){
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    //ios::sync_with_stdio(0);
    //cin.tie(0);cin.tie(0);
    int _=1;//_=read();
    while(_--) solve();
    flush();
    return 0;
}