跳到正文

题解

题解:CF1346B Boot Camp

std__int64_t约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
CF1346B

版本

  1. v1当前

本五年级蒟蒻的第 1010 篇题解,望管理员通过。

题目传送门,CF。

不难发现,为了安排更多的讲座,我们可以压着要求排讲座。

首先,如果 2k1≤k22k_1 \le k_2,那么 k2k_2 的限制根本不起作用,只需要在每个非游览日安排 k1k_1 场讲座即可。

如果 2k1>k22k_1 > k_2,可以将字符串分段,遇到 00 就开启下一段。对于每一段,我们考虑把每个连续两天都共安排 k2k_2 场。如果长度是奇数,那么最后一天可以留 k1k_1 场。按照同样的思路构造,可以 k1k_1,k2−k1k_2 - k_1 交替安排。因为 2k1>k22k_1 > k_2,故 k1>k2−k1k_1 > k_2 - k_1,安排合理。

最后附上代码:

C++
#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(这是什么奇葩语言):

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)
  }
}
原文