IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解 -> 正文阅读

[数据结构与算法]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 a?l+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 2?3+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 1?2+(?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 1?4+(?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 n?a分。这个分是固定不可控的。
可控的部分就是 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

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-16 11:33:17  更:2021-07-16 11:33:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 3:47:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码