题解:CF1346B Boot Camp
std__int64_t · · 题解
本五年级蒟蒻的第
题目传送门,CF。
不难发现,为了安排更多的讲座,我们可以压着要求排讲座。
首先,如果
如果
最后附上代码:
#include<bits/stdc++.h>
using namespace std;
int t, n, k1, k2;
string s;
void Solve(){
cin >> n >> k1 >> k2 >> s;
int ans = 0;
if(2 * k1 <= k2) for(char c : s) ans += (c == '1') * k1;
else{
int cnt = 0;
for(int i = 0; i < n; i++){
if(s[i] == '0') cnt = 0;
else ans += ((++cnt) & 1) ? k1 : k2 - k1;
}
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> t;
while(t--) Solve();
}
但你提交不了。。。
通过我的不断搜索,终于将代码转成了 Kotlin(这是什么奇葩语言):
fun main() {
val t = readLine()!!.toInt()
repeat(t) {
val (n, k1, k2) = readLine()!!.split(" ").map { it.toInt() }
val s = readLine()!!
var ans = 0
if (2 * k1 <= k2) {
ans = s.count { it == '1' } * k1
} else {
var cnt = 0
for (c in s) {
if (c == '0') {
cnt = 0
} else {
cnt++
ans += if (cnt % 2 == 1) k1 else (k2 - k1)
}
}
}
println(ans)
}
}