CF1550B Maximum Cost Deletion(分段比较)

简介: CF1550B Maximum Cost Deletion(分段比较)

You are given a string ss of length 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 111110, you will get the string 110. When you delete a substring of length ll, you get 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 tt (1≤t≤2000 ) — the number of testcases.


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


The second line contains the string ss. The string ss 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.


Example


Input

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


Output

1. 6
2. 15
3. -2


大意



有一个01字符串,每次可以删去一段连续的0或1,删除一段长为  l 的字符串可得到 a∗l+b 分,求最大分数。


由于字符串最后必须变为空串,显然  a∗l 合并同类项后为  a∗n 故只需让 b  最大化即可。

由于  +b 的次数与删除次数有关所以分类讨论


  • 当  b≥0 时,取的越多越好,操作 n 次
  • 当  b<0 时,取得越少越好,可以证明,先全部删去连续区间数量较少的,再一下把另一个全删了,操作次数最少


举个例子 10011001

最优解: 10011001⇒1111⇒ 空串

错解    :10011001⇒111001⇒001⇒1⇒ 空串

可见每个0串还是得单独删去,而1串删除的操作分为两步,故不如最优解。


具体实现看代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
      int c0=0,c1=0,ans=0; //chush
    int n,a,b;
    string s;
    cin>>n>>a>>b;
    cin>>s;
    if(b<0)
    {
      for(int i=0;i<n;i++)
      if(s[i]=='0')
      {
        int p=i;
        while(s[p]=='0')
        p++;
        i=p-1;
        c0++;
      }
     for(int i=0;i<n;i++)
      if(s[i]=='1')
      {
        int p=i;
        while(s[p]=='1')
        p++;
        i=p-1;
        c1++;
      }           
    ans=a*n+min(c1,c0)*b+b;
    }
    else
    ans=b*n+a*n;  
    cout<<ans<<endl;  
   } 
 } 



相关文章
|
存储 安全 API
oss服务器端加密(Server-Side Encryption Configuration)
阿里云OSS提供服务器端加密(SSE),确保静态数据安全。支持SSE-KMS,使用KMS托管CMK加密。数据上传时自动加密,下载时自动解密。用户可设置Bucket默认加密或在上传时指定加密选项。适用于高度保护数据场景,如敏感个人信息和企业关键信息。兼容多种部署形态,特定特性地域可用。此功能简化了加密处理,增强了云端数据安全性。
890 1
|
Linux 数据安全/隐私保护 网络协议
CentOS 7系统安装配置图解教程
操作系统:CentOS 7.3 备注: CentOS 7.x系列只有64位系统,没有32位。生产服务器建议安装CentOS-7-x86_64-Minimal-1611.iso版本 一、安装CentOS 7.
4032 0
|
存储 关系型数据库 MySQL
Mysql - 如何决定用 datetime、timestamp、int 哪种类型存储时间戳?
Mysql - 如何决定用 datetime、timestamp、int 哪种类型存储时间戳?
3823 0
|
安全 5G 网络安全
RDP:你的远程办公"任意门"使用说明书(附防黑客秘籍)
本文趣味讲解远程桌面协议(RDP)的前世今生,从1998年诞生到如今支持4K高清的Win11版本,带你了解其技术进化。文章剖析RDP工作原理,如数据加密传输、省流模式等,并警示安全风险,提供防护建议。此外,还对比了SSH、VNC等协议特点,展望RDP在量子计算与5G时代的未来,提醒用户平衡虚拟与现实生活。内容轻松幽默,适合各路“社畜”与技术爱好者阅读。
985 2
|
5月前
|
Linux API 云计算
【最新】2026年阿里云计算巢部署OpenClaw保姆级图文教程
2026年,OpenClaw(原Clawdbot)作为开源AI代理平台,凭借任务自动化执行、多模型兼容、跨场景适配的核心优势,成为个人与轻量团队提升工作效率的重要工具。阿里云计算巢针对OpenClaw推出专属一键部署方案,依托平台预置镜像与可视化管理能力,跳过复杂的依赖配置与参数调试流程,大幅降低部署门槛,同时支持云端稳定运行与本地多系统部署双向适配,满足不同用户的使用场景需求。
647 1
|
8月前
|
人工智能 供应链 安全
AI 时代下,开源如何共筑安全防线?龙蜥大会安全分论坛精彩回顾一览
共同探讨了如何以安全为底座、以 AI 为引擎,打造面向未来的操作系统安全生态。
QT应用编程: 使用QEventLoop实现事件循环
QT应用编程: 使用QEventLoop实现事件循环
1225 0
QT应用编程: 使用QEventLoop实现事件循环
|
弹性计算 监控 网络协议
深入解读云场景下的网络抖动 | 龙蜥技术
网络抖动三剑客(Pingtrace、Rtrace、Netinfo),助你快速找到系统的抖动点和原因。
深入解读云场景下的网络抖动 | 龙蜥技术
|
存储 固态存储 大数据
你知道企业级SSD与消费级SSD的区别吗?
【引语】 前两天,一位从事大数据工作的好朋友问我: “企业级SSD与消费级SSD的区别是什么?” “大数据中心能否采用消费级SSD?” 我想,大部分朋友应该都知道SSD分为企业级和消费级,但是这两者之间的区别,也许并不十分清楚。
|
前端开发
html 空白汉字占位符
前端开发中,大家可能会遇到这样的问题:标题存在字数不一样的情况,但是产品大哥,让你要对齐。还必须对齐。他说他有强迫症
1709 0

热门文章

最新文章