2025.8.1 赛后总结

· · 个人记录

\texttt{比赛情况}

\texttt{T1} \texttt{T2} \texttt{T3} \texttt{T4}
100 pts 80 pts 40 pts 100 pts
20 min 40 min 40 min 100 min

\texttt{T1}

居然没有发现是完全背包。

求出当前牧场的声量,然后判一下无解,跑 BFS 求出用这些牛凑出 [0 , 10^5] 的最小次数最后再判无解,再输出答案。

#include <bits/stdc++.h>

using namespace std;

const int kN = 110, kV = 1e5 + 10;

struct L {
  int x, w;
};

int n, b, v[kN], a[kN], dis[kV], t[kN];
bool vis[kV];

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n >> b;
  for (int i = 1; i <= b; i++) {
    cin >> v[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  t[1] = a[1];
  for (int i = 2; i <= n; i++) {
    if (a[i] == a[i - 1] - 1) {
      t[i] = 0;
    } else {
      t[i] = a[i] - a[i - 1] + (a[i - 1] != 0);
    }
    if (t[i] < 0) {
      cout << -1;
      return 0;
    }
  }
  queue<L> q;
  q.push({0, 0});
  memset(dis, 0x3f, sizeof dis);
  dis[0] = 0, vis[0] = 1;
  for (L u; !q.empty(); q.pop()) {
    u = q.front();
    for (int i = 1; i <= b; i++) {
      int nx = u.x + v[i];
      if (nx <= 1e5 && !vis[nx]) {
        vis[nx] = 1;
        dis[nx] = u.w + 1;
        q.push({nx, u.w + 1});
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (dis[t[i]] == 0x3f3f3f3f) {
      cout << -1;
      return 0;
    }
    ans += dis[t[i]];
  }
  cout << ans;
  return 0;
}

\texttt{T2}

暴力居然能过。

看到前缀,直接将前 w 个串丢进 Trie,最后暴力查询即可。(吐槽一下洛谷过了老师的评测机 80)。

#include <bits/stdc++.h>

using namespace std;

const int kN = 3e4 + 5;

int son[kN * 27][27], cnt[kN * 27], idx, sum, w, n;
string t;
bool f = 1;
unordered_map<string, int> id;

void insert(string s) {
  int p = 0;
  for (int i = 0; i < s.size(); i++) {
    int x = s[i] - 'a';
    if (!son[p][x]) {
      son[p][x] = ++idx;
    }
    p = son[p][x];
  }
  cnt[p]++;
}

int query(string s, int k) {
  int p = 0;
  for (int i = 0; i < s.size(); i++) {
    int x = s[i] - 'a';
    if (!son[p][x]) {
      return -1;
    }
    p = son[p][x];
  }
  return p;
}

void S(int p, string s, int k) {
  sum += cnt[p];
  if (sum >= k) {
    if (f) {
      t = s;
      f = 0;
    }
    return;
  }
  for (int i = 0; i < 26; i++) {
    if (son[p][i]) {
      S(son[p][i], s + char(i + 'a'), k);
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> w >> n;
  for (int i = 1; i <= w; i++) {
    string s;
    cin >> s;
    insert(s);
    id[s] = i;
  }
  for (int i = 1, k; i <= n; i++) {
    string s;
    cin >> k >> s;
    int p = query(s, k);
    if (p == -1) {
      cout << -1 << '\n';
      continue;
    }
    sum = 0;
    f = 1;
    t = "-1";
    S(p, "", k);
    if (t == "-1") {
      cout << -1 << '\n';
      continue;
    }
    cout << id[s + t] << '\n';
  }
  return 0;
}

\texttt{T3}

特判了 n \le 1000 就暴力结果后面的双指针挂了导致只有 40,结果你告诉我纯暴力 60?

我们可以二分区间的长度,check 的时候双指针只要区间长度每到就 j 往后移,如果满足了可以平衡的条件(太简单了就不讲了)就说明长度可以。

#include <bits/stdc++.h>

using namespace std;

const int kN = 1e5 + 10;

struct C {
  int w, v;
} a[kN];

int n, p[kN];

bool Ch(int m) {
  for (int i = 1, j = 0; i <= n; i++) {
    for (; j < n && a[j].w - a[i].w < m; j++) {
    }
    if (a[j].w - a[i].w < m) {
      return false;
    }
    if (p[j] >= p[i - 1] && (p[j] - p[i - 1]) % 2 == 0) {
      return true;
    }
  }
  return false;
}

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char o;
    cin >> a[i].w >> o;
    if (o == 'W') {
      a[i].v = 1;
    } else {
      a[i].v = -1;
    }
  }
  sort(a + 1, a + n + 1, [&](C x, C y) {
    return x.w < y.w;
  });
  for (int i = 1; i <= n; i++) {
    p[i] = p[i - 1] + a[i].v;
  }
  int l = 0, r = 1e9 + 1, m;
  for (; l + 1 < r;) {
    m = l + r >> 1;
    if (Ch(m)) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << l;
  return 0;
}

\texttt{T4}

看错题虚空思考 50 min。

跑三遍 dijkstra,其中前两遍建返图从 n 分别跑边权为 p 和 q 的,然后枚举每一个边,记录从当前点到 n 有几个 GPS 会抱怨,边权就是抱怨的个数然后正向图再跑一遍 dijkstra。

#include <bits/stdc++.h>

using namespace std;
using Pii = pair<int, int>;

const int kN = 1e5 + 10;

struct Node {
  int id, w;
  bool operator<(const Node& _n) const {
    return w > _n.w;
  }
};

struct Edge {
  int a, b, p, q;
};

int n, m, dis[kN][3];
vector<Pii> e[kN][3];
bool vis[kN];
vector<Edge> edge;

void D(int s, int p, int ek) {
  for (int i = 1; i <= n; i++) {
    dis[i][p] = 1e9, vis[i] = 0;
  }
  priority_queue<Node> h;
  dis[s][p] = 0;
  h.push({s, 0});
  for (int u; !h.empty();) {
    u = h.top().id;
    h.pop();
    if (vis[u]) {
      continue;
    }
    vis[u] = 1;
    for (Pii i : e[u][ek]) {
      int v = i.first, w = i.second;
      if (!vis[v] && dis[v][p] > dis[u][p] + w) {
        dis[v][p] = dis[u][p] + w;
        h.push({v, dis[v][p]});
      }
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1, a, b, p, q; i <= m; i++) {
    cin >> a >> b >> p >> q;
    edge.push_back({a, b, p, q});
    e[b][0].emplace_back(a, p), e[b][1].emplace_back(a, q);
  }
  D(n, 0, 0), D(n, 1, 1);
  for (auto i : edge) {
    int w = 0, a = i.a, b = i.b, p = i.p, q = i.q;
    if (p + dis[b][0] != dis[a][0]) {
      w++;
    }
    if (q + dis[b][1] != dis[a][1]) {
      w++;
    }
    e[a][2].emplace_back(b, w);
  }
  D(1, 2, 2);
  cout << dis[n][2];
  return 0;
}

\texttt{比赛题目}

T1:P2214

T2:P2237

T3:P3105

T4:P3720 和 P3106