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
1≤t≤2000)?— 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
1≤n≤100;?100≤a,b≤100)?— 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)
{
int ans=0;
s='1'+s+'1';
for(int i=1;i<s.size();i++)
{
if(s[i]=='0'&&s[i-1]!='0')
ans++;
}
return ans;
}
int lianxu1(string s)
{
int ans=0;
s='0'+s+'0';
for(int i=1;i<s.size();i++)
{
if(s[i]=='1'&&s[i-1]!='1')
ans++;
}
return ans;
}
bool same(string s)
{
for(int i=1;i<s.size();i++)
{
if(s[i]!=s[0])
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;
if(b>=0)score+=n*b;
else
{
int times;
if(same(s))
{
times=1;
}
else
{
int ling=lianxu0(s);
int yi=lianxu1(s);
times=min(ling,yi)+1;
}
score += times*b;
}
cout<<score<<endl;
}
return 0;
}
原创不易,转载请附上原文链接哦~ Tisfy:https://letmefly.blog.csdn.net/article/details/118771621
|