c_cpp https://arena.topcoder.com/#/u/practiceCode/15419/24973/11835/2/314633

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp https://arena.topcoder.com/#/u/practiceCode/15419/24973/11835/2/314633相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>

#define ll long long
#define sz(v) ((int) ((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define lp(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep(i, v) for(int i = 0; i < sz(v); ++i)

using namespace std;

const int MAX = 55;
const int OO = 1e9;

int l, stampCost, pushCost, brushSize;
string s;
int cache[MAX][MAX];

int minPaint(int i, int last, char lastColor) {
    //cout << "DEBUG state: ( " << i << ", " << last << " " << lastColor << " ) "<< endl;
    if(i == l) {
        if(i == last)
            return 0;
        return OO;
    }

    int &ret = cache[i][last];
    if(ret != -1)
        return ret;
    ret = OO;
    if(s[i]!='*')
        lastColor = s[i];
    if(i != l-1 && (s[i+1] == s[i] || s[i+1] == '*' || (s[i] == '*' && (s[i+1] == lastColor || lastColor == '*') ))) {
        //cout << "DEBUG state 1: ( " << i+1 << ", " << last<< " ) " <<endl;
        ret = min(ret, minPaint(i+1, last, lastColor));
    } else {

        //cout << "DEBUG tmp: " << i-last+1<< endl;
        if(i-last+1 < brushSize)
            return ret = OO;
        int tmp;
        double c = (i-last+1)/((double)brushSize);
        //cout << "DEBUG state 2: ( " << i+1 << ", " << i+1<< " ) +  " <<  ceil(c) <<endl;
        if(i == l-1)
            tmp = minPaint(i+1, i+1, lastColor) + ceil(c);
        else
            tmp = minPaint(i+1, i+1, s[i+1]) + ceil(c);
        ret = min(ret, tmp);
    }
    if(i != l-1 && (s[i+1] == '*' || s[i] == '*') && i-last+1 >= brushSize) {
        lastColor = '*';
        double c = (i-last+1)/((double)brushSize);
        //cout << "DEBUG state 3: ( " << i+1 << ", " << i+1<< " ) +  " <<  ceil(c) << endl;
        int tmp = minPaint(i+1, i+1, lastColor) + ceil(c);
        ret = min(ret, tmp);
    }

    return ret;
}
/*
int main() {
    cin>>s;
    cin>>stampCost>>pushCost;
    l = s.length();
    int x, mini = OO;
    for(int k = 1; k <=l; k++) {
        clr(cache, -1);
        brushSize = k;
        //cout << "DEBUG brush: " << brushSize << endl;
        x = minPaint(0, 0, s[0]);
        //cout << x << endl;
        if(x != OO) {
            x *= pushCost;
            x += (brushSize*stampCost);
            if(x < mini) {
                mini = x;
                //cout << "DEBUG : " << brushSize << endl;
            }
        }
    }

    cout << mini << endl;
    return 0;
}*/


class Stamp  {
public:
    int getMinimumCost(string s1, int x, int y) {
        s = s1;
        l = s.length();
        stampCost = x;
        pushCost = y;
        int ret, mini = OO;
        for(int k = 1; k <=l; k++) {
            clr(cache, -1);
            brushSize = k;
            //cout << "DEBUG brush: " << brushSize << endl;
            ret = minPaint(0, 0, s[0]);
            //cout << x << endl;
            if(ret != OO) {
                ret *= pushCost;
                ret += (brushSize*stampCost);
                if(ret < mini) {
                    mini = ret;
                    //cout << "DEBUG : " << brushSize << endl;
                }
            }
        }
        return mini;
    }
};

以上是关于c_cpp https://arena.topcoder.com/#/u/practiceCode/15419/24973/11835/2/314633的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *

c_cpp 54.螺旋矩阵