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



相关文章
|
7月前
|
前端开发
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
47 0
|
人工智能
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.
144 0
Tree with Maximum Cost---CF1092F 树上DP
PAT (Advanced Level) Practice - 1145 Hashing - Average Search Time(25 分)
PAT (Advanced Level) Practice - 1145 Hashing - Average Search Time(25 分)
121 0
|
C++
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
128 0
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
PAT (Advanced Level) Practice - 1098 Insertion or Heap Sort(25 分)
108 0
|
存储 缓存 算法
Block Throttle - Low Limit
传统的 block throttle 的语义是,cgroup 不能超过用户配置的 IOPS/BPS,此时所有 cgroup 会自由竞争 IO 资源;那么其存在的问题就是,如果用户配置的 IOPS/BPS 过高,所有 cgroup 之间就会完全自由竞争 IO 资源,从而无法保证 QoS,而如果用户配置的 IOPS/BPS 过低,又无法充分发挥 block 设备的性能 Facebook 的 Shao
589 0
1104. Sum of Number Segments (20) consecutive subsequence 每个数出现的次数
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence.
971 0
|
人工智能 BI
1003.Maximum Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
793 0