Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解相关的知识,希望对你有一定的参考价值。

Educational Codeforces Round 111 (Rated for Div. 2)-B. Maximum Cost Deletion

传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes

Problem Description

You are given a string s s s of length n n n consisting only of the characters 0 and 1.

You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 1, you will get the string 110. When you delete a substring of length l l l, you get a ⋅ l + b a \\cdot l + b al+b points.

Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 2000 1 \\le t \\le 2000 1t2000) — the number of testcases.

The first line of each testcase contains three integers n n n, a a a and b b b ( 1 ≤ n ≤ 100 ; − 100 ≤ a , b ≤ 100 1 \\le n \\le 100; -100 \\le a, b \\le 100 1n100;100a,b100) — the length of the string s s s and the parameters a a a and b b b.

The second line contains the string s s s. The string s s s consists only of the characters 0 and 1.

Output

For each testcase, print a single integer — the maximum number of points that you can score.

Sample Input

3
3 2 0
000
5 -2 5
11001
6 1 -4
100111

Sample Onput

6
15
-2

Note

In the first example, it is enough to delete the entire string, then we will get 2 ⋅ 3 + 0 = 6 2 \\cdot 3 + 0 = 6 23+0=6 points.

In the second example, if we delete characters one by one, then for each deleted character we will get ( − 2 ) ⋅ 1 + 5 = 3 (-2) \\cdot 1 + 5 = 3 (2)1+5=3 points, i. e. 15 15 15 points in total.

In the third example, we can delete the substring 00 from the string 1, we get 1 ⋅ 2 + ( − 4 ) = − 2 1 \\cdot 2 + (-4) = -2 12+(4)=2 points, and the string will be equal to 1111, removing it entirely we get 1 ⋅ 4 + ( − 4 ) = 0 1 \\cdot 4 + (-4) = 0 14+(4)=0 points. In total, we got − 2 -2 2 points for 2 2 2 operations.


题目大意

长度为 n n n 01 01 01串,每次可以消除相同且连续的一部分。每次消除获得分数 a ∗ 消 除 的 长 度 + b a*消除的长度+b a+b分。问你全部消除完毕最多能得多少分。

解题思路

不论消除顺序如何,最终都要消除完,所以最终消除的总长度就是 n n n,必定有 n ∗ a n*a na分。这个分是固定不可控的。
可控的部分就是 b b b了。每消除一次额外获得 b b b分。 b > 0 b>0 b>0时,消除次数越多越好(最多 n n n次,一次消除一个)。 b < 0 b<0 b<0时,消除次数越少约好,一次消除尽可能多个。
要消除次数最少,有种办法就是每次只消除1,1全部消除完毕后,剩下的全是0,是连续的,只用再消除1次即可。有多少段不连续的1就需要消除多少+1次。(也可能是先全部消除0剩下1)

数据范围很小,比赛为了赶时间,不需要过优的算法。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int lianxu0(string s) // 字符串s中有多少段0
{
    int ans=0;
    s='1'+s+'1'; // 前后各加上个1
    for(int i=1;i<s.size();i++)
    {
        if(s[i]=='0'&&s[i-1]!='0') // 这个是0上一个不是0,说明这是新的一段0
            ans++;
    }
    return ans;
}
int lianxu1(string s) // 字符串s中有多少段1
{
    int ans=0;
    s='0'+s+'0'; // 参考lianxu1即可
    for(int i=1;i<s.size();i++)
    {
        if(s[i]=='1'&&s[i-1]!='1')
            ans++;
    }
    return ans;
}
bool same(string s) // 字符串s中是不是所有的元素都相同(只需要消除一次就行了
{
    for(int i=1;i<s.size();i++)
    {
        if(s[i]!=s[0]) // 1个不同就不是都一样
            return false;
    }
    return true;
}
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n,a,b;
        cin>>n>>a>>b;
        string s;
        cin>>s;
        int score = n*a; // a产生的分数是固定不可控的
        if(b>=0)score+=n*b; // b大于等于0,就消除尽量多次,一次只消除一个
        else // 否则消除尽量少的次数
        {
            int times; // 最少消除多少次
            if(same(s)) // s中所有元素均相同
            {
                times=1; // 消除一次就够了
            }
            else // 有不同的元素
            {
                int ling=lianxu0(s); // 有ling段0
                int yi=lianxu1(s); // 有yi段1
                times=min(ling,yi)+1; // 消除次数就是只消除0后再消除1次1  或 只消除1后再消除一次0
            }
            score += times*b; // 一次获得b分,乘以消除次数,加上a产生的不可控分数
        }
        cout<<score<<endl; // 输出即可。
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/118771621

以上是关于Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #717 (Div. 2)-A. Tit for Tat-题解

区间倍增dpD. Cut——Codeforces Round #717 (Div. 2)

Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解

Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解

Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解

Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解