跳到正文

剪贴板

剪贴板 jgu3id23

原文
剪贴板信息

剪贴板信息

收录
更新

省流:过6题罚时最高的,傻子D题根本不是人。

A

谁能告诉我这你妈为什么是图论。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int T, n, d[N];
long long ans;
int main() {
  cin >> T; while (T--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> d[i];
    sort(d + 1, d + 1 + n);
    for (int i = 2; i <= n; ++i) {
      int w = d[i] - d[i - 1];
      ans += w + (-1ll) * w * (n - i + 1) * (i - 1);
    }
    cout << ans << "\n"; ans = 0;
  }
  return 0;
}

B

板子迪杰斯特拉,做的太慢了,似乎是迪杰写错了。

C++
#include <bits/stdc++.h>
using namespace std;
int n, m, s, t, a, b, C, D1, D2, ans;
int F[1005][3], Z[1005][3], G[1005][1005];
struct node {
  int to, c;
  bool operator<(const node &r) const {
    return r.c < c;
  }
};
vector<int> g[1005];
void D(int s, int c) {
  for (int i = 1; i <= n; i++) Z[i][c] = INT_MAX;
  Z[s][c] = 0;
  priority_queue<node> q;
  q.push((node){s, 0});
  while (!q.empty()) {
    node t = q.top(); q.pop();
    int u = t.to;
    if (F[u][c]) continue;
    F[u][c] = 1;
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (!F[v][c] && Z[v][c] > Z[u][c] + 1) {
        Z[v][c] = Z[u][c] + 1;
        q.push((node){v, Z[v][c]});
      }
    }
  }
}
int main() {
  cin >> n >> m >> s >> t;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    G[b][a] = G[a][b] = 1;
    g[a].push_back(b); g[b].push_back(a);
  }
  D(s, 1); D(t, 2);
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (!G[i][j]) {
        D1 = Z[i][1] + Z[j][2] + 1;
        D2 = Z[i][2] + Z[j][1] + 1;
        if (min(D1, D2) >= Z[t][1]) ans++;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}

C

666 居然还有卡常数题目,不开std两倍时限导致的。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
int n, m, vis[N], d[N], w[N], v[N], ans[N];
vector<pair<int, int> > e[N];
void dfs (int x, int dep) {
    ans[x] = min(ans[x], dep);
    for (auto i : e[x]) {
      int u = i.first, c = i.second;
      if (d[u] != d[x] + 1) continue;
      v[c]++;
        dfs(u, w[c] * v[c] + dep);
      v[c]--;
    }
}
int main() {
  memset(ans, 0x3f, sizeof(ans));
  memset(d, -1, sizeof(d)); d[1] = 0;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) cin >> w[i];
  for (int i = 1; i <= m; i++) {
    int u, v, w; cin >> u >> v >> w;
    e[u].push_back({v, w});
    e[v].push_back({u, w});
  }
  queue<int> q; q.push(1);
  while(!q.empty()) {
    int t = q.front(); q.pop();
    for (auto i : e[t]) {
      int x = i.first;
      if (~d[x]) continue;
      q.push(x); d[x] = d[t] + 1;
    }
  }
  dfs(1, 0);
  for (int i = 2; i <= n; i++) cout << ans[i] << "\n";
  return 0;
}

D

跟个弗洛伊德一样,但是偏DP,三元组。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 9, inf = 1e9;
int n, m, q, f[35][N];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  cin >> n >> m >> q;
  vector<tuple<int, int, int> > e(m);
  for (auto &[x, y, z] : e) cin >> x >> y >> z;
  fill(f[0], f[0] + N, 1);
  for (int j = 1; j <= 30; j++) {
    for (auto [x, y, z] : e) {
      f[j][x] = max(f[j][x], min(inf, f[j - 1][y] * z));
    }
  }
  while(q--) {
    int x, p, r = 0; cin >> x >> p;
    while(f[++r][x] <= p);
    cout << r << '\n';
  }
  return 0;
}

E

难点在建边,一个 DJ 就做完了,比前面 D 题好多了。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e5 + 5;
int n, m, F[N], ans;
vector<pair<int, int> > G[N];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
signed main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w; cin >> u >> v >> w;
    G[u].push_back({v, w});
    G[v].push_back({u, w});
    G[u].push_back({v + n, 0});
    G[v].push_back({u + n, 0});
    G[u + n].push_back({v + n, w});
    G[v + n].push_back({u + n, w});
  }
  for (int i = 1; i <= n; i++) {
    int a; cin >> a;
    G[0].push_back({i, a});
  }
  for (int i = 1; i <= 2 * n; i++) F[i] = 2e18;
  F[0] = 0; q.push({F[0], 0});
  while(!q.empty()) {
    pair<int, int> S = q.top(); q.pop();
    int d = S.first, u = S.second;
    if (d > F[u]) continue;
    for (auto K : G[u]) {
      int v = K.first, w = K.second;
      if (F[v] > F[u] + w) {
        F[v] = F[u] + w; q.push({F[v], v});
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, min(F[i], F[i + n]));
  }
  cout << ans;
  return 0;
}

I

特判比主要部分还长。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, m, a[N], rd[N], u[N], v[N], vis[N];
vector<int> E1[N], E2[N], ans[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int T; cin >> T; while(T--) {
    cin >> n>> m;
    for (int i = 1; i <= n; i++) {
      rd[i] = 0; vis[i] = 0;
      E1[i].clear(); E2[i].clear();
      ans[i].clear();
    }
    for (int i = 1; i <= n; i++) {
      cin >> a[i]; vis[a[i]] = 1;
    }
    for (int i = 1; i <= m; i++) {
      cin >> u[i] >> v[i];
      if (a[u[i]] != a[v[i]]) E1[a[u[i]]].push_back(a[v[i]]);
      E2[u[i]].push_back(v[i]);
      rd[v[i]]++;
    }
    queue<int> q;
    for (int i = 1; i <= n; i++) {
      if (!rd[i]) q.push(i);
    }
    int cnt = 0;
    while(!q.empty()) {
      int x = q.front(); q.pop();
      cnt++; ans[a[x]].push_back(x);
      for (int i = 0; i < E2[x].size(); i++) {
        int y = E2[x][i];
        if (!--rd[y]) q.push(y);
      }
    }
    if (n != cnt) {
      cout << "-1\n"; continue;
    } cnt = 0;
    for (int i = 1; i <= n; i++) rd[i] = 0;
    for (int i = 1; i <= m; i++) {
      if (a[u[i]] != a[v[i]]) rd[a[v[i]]]++;
    }
    for (int i = 1; i <= n; i++) {
      cnt += vis[i];
      if (vis[i] && !rd[i]) q.push(i);
    }
    vector<int> S;
    while(!q.empty()) {
      int x = q.front(); q.pop();
      S.push_back(x);
      for (int i = 0; i < E1[x].size(); i++) {
        int y = E1[x][i];
        if (!--rd[y]) q.push(y);
      }
    }
    if (S.size() != cnt) {
      cout << "-1\n"; continue;
    }
    for (int i = 0; i < S.size(); i++) {
      for (int j = 0; j < ans[S[i]].size(); j++) {
        cout << ans[S[i]][j] << " ";
      }
    }
    cout << "\n";
  }
  return 0;
}

F & G & H & J

没看题。

总结

做得太慢了,不知道 B 题怎么调那么久。