day14(模拟赛)总结

· · 个人记录

最后一场模拟赛挂了。

tm不知道啥时候改样例了又没看见。

400 -> 300

Fall into the dark side了。

预期:100 + 100 + 100 + 50 + 50

实际:100 + 0 + 100 + 50 + 50

T1

签到题。

只要 s - a - b \ge 0 且 (s - a - b) \bmod 2 = 0 就输出 Yes,否则输出 No。

由于 a 和 b 有可能是负数,所以要提前取绝对值。

况且题目都提醒我们了。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int kMaxN = 1e5 + 5;

int t, a, b, s;

void solve() {
  cin >> a >> b >> s;
  a = abs(a), b = abs(b);
  cout << (s < (a + b) || (s - (a + b)) % 2 ? "No\n" : "Yes\n");
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (cin >> t; t; t--) 
    solve();
  return 0;
}

B

水题写成了一滩烂泥。

首先,当 s > 9 \times m 时,必然无解。

当 s = 0 时,若 m = 1,答案为 0,否则无解。

求最小值时,高位越小越好,所以填数时先假设这一位填 0,后面全填 9,如果再加上前面的数位和超过了 s,那么就填 0。

否则差多少填多少。

求最大值时,高位越大越好,所以填数时先假设这一位和后面全填 0,如果再加上前面的数位和比 s - 9 小,那么就填 9。

否则比 s 少多少填多少。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int kMaxN = 1e5 + 5;

int t, m, s, type;

void get_ans(int type) {
  int sum = 0;
  for (int i = 1; i <= m; i++) {
    int x = (type ? max(int(i == 1), s - (sum + 9 * (m - i))) : min(9, s - sum));
    sum += x;
    cout << x;
  }
  cout << " ";
}

void solve() {
  cin >> m >> s >> type;
  if (!s && m == 1) {
    cout << (type == 3 ? "0 0\n" : "0\n");
    return;
  }
  if (!s || s > 9 * m) {
    cout << (type == 3 ? "-1 -1\n" : "-1\n");
    return;
  }
  if (type != 2)
    get_ans(1);
  if (type != 1) 
    get_ans(0);
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (cin >> t; t; t--) 
    solve();
  return 0;
}

然而最大最小值全错了。

问题集中在第 13 行。

int x = (type ? max(int(i != 1), s - (sum + 9 * (m - i))) : min(9, s - (sum + m - i)));

最小值错哪了?

写错了。

== -> !=

最大值错哪了?

最小值考虑到的 0 最大值没考虑到。

100 -> 0,寄。

C

什么神机,比颠佬还颠。

首先,我们需要一个数组 mp,编号为 i 的应用在手机中是第 mp_i 个。

显然,编号为 x 的应用在第 \lceil \frac{mp_x}{k} \rceil 页。

所以操作次数是 \lceil \frac{mp_x}{k} \rceil - 1 + 1。

然后只需要将 a_{mp_x} 和 a_{mp_x - 1} 换位置,再更新 mp 即可。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int kMaxN = 1e5 + 5;

ll n, m, k, a[kMaxN], mp[kMaxN];

void solve() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[a[i]] = i;
  }
  ll ans = 0, x;
  for (int i = 1; i <= m; i++) {
    cin >> x;
    ans += (mp[x] - 1) / k + 1;
    if (mp[x] != 1) {
      swap(a[mp[x]], a[mp[x] - 1]);
      mp[x]--;
      mp[a[mp[x] + 1]]++;
    }
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

D

暴力50遗憾离场。

50pts

这个不多说,暴力搜索,三种情况:不选、选原数、选阶乘。

注意只有原数 \le 18 时才能选阶乘,否则会超过 s。

时间复杂度 O(3^n)。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll n, k, s, a[30], ans, f[20];

inline void dfs(ll x, ll y, ll sum) {
  if (sum > s)
    return;
  if (x > n) {
    ans += (sum == s);
    return;
  }
  if (a[x] <= 18 && y < k)
    dfs(x + 1, y + 1, sum + f[a[x]]);
  dfs(x + 1, y, sum + a[x]);
  dfs(x + 1, y, sum);
}

inline void solve() {
  cin >> n >> k >> s;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, greater<ll>());
  f[1] = 1;
  for (int i = 2; i <= 18; i++)
    f[i] = f[i - 1] * i;
  dfs(1, 0, 0);
  cout << ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

100pts

据说是折半搜,我还没写出来。

E

还得是暴力,两题带走100pts。

50pts

每一次加数都求一遍LIS即可,时间复杂度略低于 O(n^2\log n),可以过 10^4。

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int kMaxN = 5e4 + 5;

int n, p[kMaxN], k[kMaxN], f[kMaxN], lis[kMaxN], len;

inline void solve() {  
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> k[i];
  for (int i = 1; i <= n; i++) {
    f[k[i]] = 1;
    len = 0;
    for (int j = 1; j <= n; j++) {
      if (f[j]) {
        int pos = lower_bound(lis + 1, lis + len + 1, p[j]) - lis;
        if (pos > len)
          len++;
        lis[pos] = p[j];
      }
    }
    cout << len << " ";
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}