[CF] B. Cost of the Array - Codeforces Round 1002 (Div. 2)
Solutions Codeforces 贪心 构造 Easy
Lastmod: 2025-02-06 周四 22:24:24

https://codeforces.com/contest/2059/problem/B

题目大意

给出 $n$ 长的数组 $a$。给出偶数 $k$,要求将 $a$ 分为连续的 $k$ 个非空子段。将第偶数段($1$-based)按照原来的顺序链接起来并在结尾追加一个 $1$。定义这个新数组 $b$ 的分值为,最小的下标 $i$ 使得 $b[i] \neq i$。问这个最小值最小是多少。

$2 \le k \le n \le 2 \times 10^5$。$1 \le a_i \le 10^9$。

简要题解

首先 $n = k$ 的情况只有唯一的分段情况,直接算即可。

否则我们注意到我们尽量让第一个分到 $b$ 中的值就不是 $1$。第一个值能选的下标范围是 $[2, n - k + 2]$。

当我们在这个区间无法选出非 $1$ 元素时,证明最小答案至少为 $2$。由于此时 $k < n$ 则该区间至少有两个元素,且都为 $1$,我们把这一组都选入 $b$ 的第二段,则答案为 $2$。

(最开始以为是求最大值,还写了个复杂的贪心。不确定对不对)

void solve() {
  int n, k; cin >> n >> k;
  vector<int> a(n);
  for (int& i : a) cin >> i;

  vector<int> b;
  k--;
  int sel = 0;
  int pre = 0;
  for (int i = 1; i < n; i++) {
    cerr << i << ' ' << (n - i) << ' ' << k << endl;
    if (k == 1) {
      for (int j = i; j < n; j++) {
        b.push_back(a[j]);
        k--;
      }
      break;
    }
    if (n - i == k) {
      cerr << "here " << endl;
      for (int j = i; j < n; j++) {
        if (k & 1) b.push_back(a[j]);
        k--;
      }
      break;
    }

    if (a[i] == pre + 1) {
      b.push_back(a[i]);
      pre++;

      if (!sel) {
        k--;
        sel = 1;
      }
    } else {
      if (sel) {
        k--;
        sel = 0;
      }
    }
  }
  b.push_back(0);

  for (int i : b) cerr << i << ' ';
  cerr << endl;

  int m = b.size();
  for (int i = 0; i < m; i++) {
    if (b[i] != i + 1) {
      cout << (i + 1) << '\n';
      return;
    }
  }
}

复杂度

$T$:$O(n)$

$S$:$O(n)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n, k; cin >> n >> k;
  vector<int> a(n);
  for (int& i : a) cin >> i;

  if (n == k) {
    int ans = 1;
    for (int i = 1; i < n; i += 2) {
      if (a[i] != ans) {
        break;
      }
      ans++;
    }
    cout << ans << '\n';
  } else {
    int r = n - k + 1;
    for (int i = 1; i <= r; i++) {
      if (a[i] != 1) {
        cout << "1\n";
        return;
      }
    }
    cout << "2\n";
  }
}

int main() {
  int t = 1; 
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] E. Turtle vs. Rabbit Race: Optimal Trainings - Codeforces Round 929 (Div. 3)
Next: [CF] C. Customer Service - Codeforces Round 1002 (Div. 2)