Tiling Terrace CodeForces - 1252J (dp, greedy)

Tiling Terrace

\[ Time Limit: 1000 ms\quad Memory Limit: 262144 kB \]

The meaning of problems

Given a string \ (S \) , each of three types can be selected to obtain the value
\ (Type1: \ "." ) To obtain \ (W_1 \) Element
\ (Type2: ".." \ ) to obtain \ ( w_2 \) yuan
\ (Type3: ".. \ #" \) to obtain \ (w_3 \) yuan
addition, there are two constraints
\ (Limti1: Type1 \) at most only choose \ (K \) a
\ ( Limit2: \) each character can only be selected once
asked a maximum value that can be obtained.

Thinking

First of all can be found for two adjacent \ (\ # \) , if we identified these two states, which is not, or as \ (Type3 \) to use, then we can know that the two \ ( # \) \ between \ (. \) number. If this number is odd, it means that one of them \ (. \) Used to be used as \ (Type1 \) is not necessarily a loss, that is, the \ (. \) Is a white prostitute.

Order \ (dp [i] [j ] [k] [0/1] \) represents the section \ (I \) a \ (\ # \) until the number of white whore \ (J \) a \ (Type1 \) , selected from the \ (K \) a \ (Type3 \) , and the first \ (I \) a \ (\ # \) whether as \ (Type3 \) to use.
In order to facilitate the calculation, we can add at the beginning of a whole string of \ (\ # \) , the end of the entire string to add a \ (\ # \) , then the entire state of necessity from \ (dp [0] [0 ] [0] [0] \) begins recursive, bound to \ (dp [\ #_ {number }] [j] [k] [0] \) at the end.

After this launch, we will know the white prostitute \ (a \) a \ (Type1 \) , choose \ (c \) a \ (Type3 \) in the case of, can get up to number \ (b \) . Finally, \ (a, b, c \ ) greedy seeking answers, try \ (Type1 \) does not exceed \ (K \) in the case of the \ (Type2 \) into \ (Type1 \) .

We can see that because the \ (\ # \) number up to it \ (50 \) months, then the white of prostitution \ (Type1 \) must not exceed \ (51 \) , so the whole \ (dp \) of complexity is \ (O \ left (50 ^ 3 \ times4 \ right) \) of

/*************************************************************** 
    > File Name     : J.cpp
    > Author        : Jiaaaaaaaqi
    > Created Time  : Tue 05 Nov 2019 10:00:31 PM CST
 ***************************************************************/

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pb         push_back
#define  pii        pair<int, int>

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int n, m;
int cas, tol, T;

ll g1, g2, g3;
char s[maxn];
ll dp[60][60][60][2];
vector<int> vv;

ll calc(ll a, ll b, ll c) {
    if(a > m)   a = m;
    ll ans = a*g1 + c*g3;
    ll tmp = min(b, (m-a)/2);
    ll res = max(b*g2, (b-tmp)*g2 + tmp*2ll*g1);
    if(a+tmp*2<m && b-tmp>0)    res = max(res, res-g2+g1);
    return ans+res;
}

int main() {
    // freopen("in", "r", stdin);
    scanf("%d%d%lld%lld%lld", &n, &m, &g1, &g2, &g3);   
    scanf("%s", s+1);
    vv.clear();
    vv.pb(0);
    for(int i=1; i<=n; i++) {
        if(s[i]=='#')   vv.pb(i);
    }
    vv.pb(n+1);
    for(int i=0; i<60; i++) for(int j=0; j<60; j++)
        for(int k=0; k<60; k++) for(int z=0; z<2; z++)
            dp[i][j][k][z] = -INF;
    dp[0][0][0][0] = 0;
    int sz = vv.size()-1;
    for(int i=0; i<sz; i++) {
        int s = vv[i+1]-vv[i]-1;
        for(int j=0; j<60; j++) {
            for(int k=0; k<60; k++) {
                if(dp[i][j][k][0] >= 0) {
                    if(s>=0) dp[i+1][j+s%2][k][0] = max(dp[i+1][j+s%2][k][0], dp[i][j][k][0] + s/2);
                    if(s>=1) dp[i+1][j+(s-1)%2][k][1] = max(dp[i+1][j+(s-1)%2][k][1], dp[i][j][k][0] + (s-1)/2);
                }
                if(dp[i][j][k][1] >= 0) {
                    if(s>=1) dp[i+1][j+(s-1)%2][k+1][0] = max(dp[i+1][j+(s-1)%2][k+1][0], dp[i][j][k][1] + (s-1)/2);
                    if(s>=2) dp[i+1][j+(s-2)%2][k+1][1] = max(dp[i+1][j+(s-2)%2][k+1][1], dp[i][j][k][1] + (s-2)/2);
                }
            }
        }
    }
    ll ans = 0;
    for(int i=0; i<60; i++) for(int j=0; j<60; j++) if(dp[sz][i][j][0]>=0)  
        ans = max(ans, calc(i, dp[sz][i][j][0], j));
    printf("%lld\n", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Jiaaaaaaaqi/p/11978759.html