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;  
   } 
 } 



相关文章
|
4月前
|
存储 算法 Java
Minimum Transport Cost(ZOJ - 1456)
Minimum Transport Cost(ZOJ - 1456)
|
11月前
|
Java 应用服务中间件
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
134 0
|
调度 网络架构 索引
NR PDSCH(三) TB size determination
谈TB size前,首先了解下PDSCH resource mapping,基站会通过RRC层配置的参数告知UE有一些时频资源(RB级/RE级)不能作为UE的PDSCH 资源使用,网络侧会对这些资源可能有特定用途,例如DSS场景中LTE作为NR 的inband部署时。用于发送RAR/OSI/Paging/MSG4/MSGB/SIB1 的PDSCH资源如果与SSB 的PRB overlap,overlap的PDSCH不能用于PDSCH 传输。
NR PDSCH(三) TB size determination
|
Java 应用服务中间件
The field file exceeds its maximum permitted size of 1048576 bytes.
The field file exceeds its maximum permitted size of 1048576 bytes.
|
人工智能
Tree with Maximum Cost---CF1092F 树上DP
You are given a tree consisting exactly of n vertices. Tree is a connected undirected graph with n−1 edges. Each vertex v of this tree has a value av assigned to it. Let dist(x,y) be the distance between the vertices x and y.
134 0
Tree with Maximum Cost---CF1092F 树上DP
|
前端开发 Java 关系型数据库
记录:The field files exceeds its maximum permitted size of 1048576 bytes...【亲测有效】
记录:The field files exceeds its maximum permitted size of 1048576 bytes...【亲测有效】
1102 0
|
SQL Oracle 算法
Adaptive and Big Data Scale Parallel Execution in Oracle
在上篇文章中,主要讨论了SQL Server的MPP数仓系统PDW的分布式优化过程,PolarDB的并行优化从中有所借鉴,本篇文章主要看下这篇介绍Oracle并行执行策略的paper,因为在PolarDB的分布式执行策略中,有很多与其有所重叠。
209 0
Adaptive and Big Data Scale Parallel Execution in Oracle
|
存储 缓存 算法
Block Throttle - Low Limit
传统的 block throttle 的语义是,cgroup 不能超过用户配置的 IOPS/BPS,此时所有 cgroup 会自由竞争 IO 资源;那么其存在的问题就是,如果用户配置的 IOPS/BPS 过高,所有 cgroup 之间就会完全自由竞争 IO 资源,从而无法保证 QoS,而如果用户配置的 IOPS/BPS 过低,又无法充分发挥 block 设备的性能 Facebook 的 Shao
528 0
Partition 1 does not start on physical sector boundary
Partition 1 does not start on physical sector boundary
866 0
Partition 1 does not start on physical sector boundary