跳到正文

个人记录

代码源 Day4 附加训练1

CuteGirlGC约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月7日
篇幅
约 12 分钟

版本

  1. v1当前

Rk1

Final

A

容易发现答案一定是把问号变成 00…011…100\dots 011\dots 1 的形式,然后通过只有零和一可以快速算出逆序对。

C++
#include <cstring>
#include <iostream>

using namespace std;
using ll = long long;

const ll kMaxN = 1e6 + 5;

ll t, n, a[kMaxN], c[kMaxN];

int main() {
  for (cin >> t; t; t--) {
    cin >> n;
    string s;
    cin >> s;
    for (ll i = 0; i < n; i++) {
      a[i + 1] = (s[i] == '0' ? 0 : (s[i] == '?' ? -1 : 1));
    }
    fill(c + 1, c + n + 2, 0);
    ll sm = 0;
    for (ll i = n; i >= 1; i--) {
      c[i] = c[i + 1] + (a[i] != 1);
      if (a[i] == 1) {
        sm += c[i];
      }
    }
    ll maxx = sm, o = 0;
    for (ll i = 1; i <= n; i++) {
      if (a[i] == 1) {
        o++;
      } else if (a[i] == -1) {
        o++;
        sm -= o, sm += c[i];
        maxx = max(maxx, sm);
      }
    }
    cout << maxx << '\n';
  }
  return 0;
}

B

错因是没有开 long long 还有数组开太大了。

C

诈骗题,答案是整个区间。

C++
#include <iostream>

using namespace std;

const int kMaxN = 2e5 + 1;

int n, s, l, r;

int main() {
  for (cin >> n; n; n--) {
    cin >> s >> l >> r;
    cout << r - l + 1 << '\n';
  }
  return 0;
}

D

到后面才写出来,一个找规律的题目。

C++
#include <iostream>

using namespace std;
using ll = long long;

const ll kMod = 998244353;

ll fPow(ll a, ll b)  {
  if (b < 0) {
    return 1;
  }
  ll res = 1;
  for (; b; b >>= 1) {
    if (b & 1) {
      res = res * a % kMod;
    }
    a = a * a % kMod;
  }
  return res;
}

ll t, a, b;

int main() {
  ios::sync_with_stdio(0);
  for (cin >> t; t; t--) {
    cin >> a >> b;
    if (!(a & 1) && !(b & 1)) {
      cout << 1 << '\n';
    } else if (!(a & 1)) {
      cout << fPow(2, a - 1) << '\n';
    } else if (!(b & 1)) {
      cout << fPow(2, b - 1) << '\n';
    } else {
      cout << fPow(2, a + b - 2) << '\n';
    }
  }
  return 0;
}

E

模拟题,要注意判 -1。

C++
#include <iostream>

using namespace std;
using ll = long long;

const ll kMaxN = 101;

ll t, n, q, a[kMaxN];
pair<ll, ll> p[kMaxN];

int main() {
  for (cin >> t; t; t--) {
    cin >> n >> q;
    ll c = 0;
    for (ll i = 1; i <= n; i++) {
      cin >> a[i], p[i] = {-2e9, 2e9};
    }
    bool f = 0;
    for (ll i = 1, j, l, r; i <= q; i++) {
      cin >> j >> l >> r;
      p[j] = {max(p[j].first, l), min(p[j].second, r)};
      if (p[j].first > p[j].second) {
        f = 1;
      }
      if (a[j] > r) {
        c += a[j] - r;
        a[j] = r;
      } else if (a[j] < l) {
        c += l - a[j];
        a[j] = l;
      }
    }
    cout << (f ? -1 : c) << '\n';
  }
  return 0;
}

F

可以发现行,列坐标和是不会变的,所以输出 n(n+1)(n−1)n(n+1)(n-1)。

C++
#include <iostream>

using namespace std;
using ll = long long;

const int kMaxN = 1001 + 1;

ll n, a[kMaxN][kMaxN];

int main() {
  cin >> n;
  cout << n * (n + 1) + n * (n - 1) * n << '\n';
  return 0;
}

G

如果首尾不一样,那么肯定是 empty,否则在首尾加上与原先首尾相反的字符,求加上的字符之间的最小的子串。

C++
#include <iostream>

using namespace std;

int t, n;
string s;

int main() {
  for (cin >> t; t; t--) {
    cin >> n >> s;
    if (s[0] != s[n - 1]) {
      cout << "empty\n";
      continue;
    }
    char c = s[0] == '0' ? '1' : '0';
    s = c + s + c;
    s = ' ' + s;
    n += 2;
    int cnt = 0, ans = 1e9;
    for (int i = 2; i <= n; i++) {
      if (s[i] == c) {
        ans = min(ans, cnt);
        cnt = 0;
      } else {
        cnt++;
      }
    }
    if (ans == 0) {
      cout << "empty\n";
    } else {
      for (int i = 1; i <= ans; i++) {
        cout << (c == '0' ? '1' : '0');
      }
      cout << '\n';
    }
  }
  return 0;
}

H

还算有点难度,就是先把数二进制分解,然后知道、、找到到答案。

C++
#include <iostream>

using namespace std;
using ll = long long;

ll t, n, m;

ll A(ll x) { return (1 << x) - 1; }

ll B(ll x, ll y) {
  if (x % y == 0) return x / y;
  return x / y + 1;
}

int main() {
  for (cin >> t; t; t--) {
    cin >> n >> m;
    if (m <= 30 && A(m) < n) {
      cout << "-1" << '\n';
    }
    else if (m <= 30 && A(m) == n) {
      cout << (1 << (m - 1)) << '\n';
    } else {
      ll now = 0;
      while (B(n - A(now), m - now) > A(now)) {
        now++;
      }
      ll del = n - A(now);
      ll ans = 0;
      if (del == 0) {
        ans = 1 << (now - 1);
      } else {
        ans = max(1ll << (now - 1), B(del, m - now));
      }
      cout << ans << '\n';
    }
  }
  return 0;
}

I

枚举不同的字符个数,然后再双指针验证,复杂度 O(26∣S∣)O(26|S|)。

C++
#include <iostream>

using namespace std;
using ll = long long;

ll n;
string s;

ll gcd(ll x, ll y) {
  return x % y ? gcd(y, x % y) : y;
}

int main() {
  cin >> s, n = s.size(), s = ' ' + s;
  long double ans = 0;
  pair<ll, ll> p = {1, 1};
  for (ll i = 1; i <= 26; i++) {
    ll l = 0, r = 1, o[200] = {}, cnt = 0;
    o[s[1]] = 1, cnt = 1;
    while (l < r && r < n) {
      while (r < n && cnt < i) {
        r++;
        o[s[r]]++;
        if (o[s[r]] == 1) {
          cnt++;
        }
      }
      while (l < r && cnt == i) {
        long double tmp = i * i * 1.0 / (r - l);
        if (tmp > ans) {
          ans = tmp, p = {i * i, r - l};
        }
        l++;
        o[s[l]]--;
        if (o[s[l]] == 0) {
          cnt--;
        }
      }
    }
  }
  ll g = gcd(p.first, p.second);
  cout << p.first / g << ' ' << p.second / g << '\n';
  return 0;
}

J

答案就是 4444…444⏟min⁡(x−1,y−1)50000…000⏟max⁡(0,x−y)\underbrace{4444\dots444}_{\min(x-1,y-1)}5\underbrace{0000\dots 000}_{\max(0, x-y)}。

C++
#include <iostream>

using namespace std;
using ll = long long;

const ll kMod = 998244353;

ll fPow(ll a, ll b)  {
  ll res = 1;
  for (; b; b >>= 1) {
    if (b & 1) {
      res = res * a % kMod;
    }
    a = a * a % kMod;
  }
  return res;
}

int main() {
  ll minn = 0, maxx = 0, x, y;
  cin >> x >> y;
  for (int i = 1; i <= min(x - 1, y - 1); i++) {
    minn *= 10;
    minn += 4;
    minn %= kMod;
  }
  minn *= 10;
  minn += 5;
  minn %= kMod;
  for (int i = 1; i <= x - y; i++) {
    minn *= 10;
    minn %= kMod;
  }
  for (int i = 1; i <= x; i++) {
    maxx *= 10, maxx += 9, maxx %= kMod;
  }
  cout << (maxx + minn) % kMod * ((maxx - minn + 1) % kMod + kMod) % kMod * fPow(2, kMod - 2) % kMod;
  return 0;
}

总结

爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!爽!rk1!

还是要注意 long long。

原文