雅礼cch杯 round 3总结

· · 个人记录

T3MLE了我心态炸了😭😭😭😭😭😡😡😡😡😡

T1

记录

100pts

思路

竟然还有小学数学。

第一眼看到这个式子:

\sum_{i=2}^{n}\frac{1}{u(i)v(i)}

其中 u(i) 是大于 i 的最小质数,v(i) 是不大于 i 的最大质数。看到这个式子很容易想到质数筛筛出所有小于等于 n 的质数和大于 n 的第一个质数。然后往下翻,$n\le10^9

$$\sum_{i=2}^{n}\frac{1}{u(i)-v(i)}(\frac{1}{v(i)}-\frac{1}{u(i)})$$ 然后发现在一定区间内,这个柿子的值相同,于是我们可以对于所有 $u(i)\le n$ 的 $i$,也就是 $v(i)$ 的值进行合并: $$\sum_{i\in 质数}^{n}(u(i)-v(i))\frac{1}{u(i)-v(i)}(\frac{1}{v(i)}-\frac{1}{u(i)})=\sum_{i\in 质数}^{n}(\frac{1}{v(i)}-\frac{1}{u(i)})$$ 于是就变成了 $\frac{1}{2}-\frac{1}{v(n)}$,然后再加上不完整的一坨,即为 $\frac{1}{2}-\frac{1}{v(i)}+\frac{n-v(i)+1}{u(i)v(i)}=\frac{u(i)v(i)-2u(i)-2v(i)+2n+2}{2u(i)v(i)}$。然后因为质数的密度在 $10^9$ 之内还是很高的,所以可以暴力往 $n$ 两边搜,找到最近的质数,复杂度 $O(大常数\times\sqrt{n})$,总时间复杂度 $O(能过)$。 ```cpp #include <bits/stdc++.h> #define int long long using namespace std; int T, n; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } int U(int x) { while(1) { x++; bool flag = true; for(int i = 2; i * i <= x; i++) { if(x % i == 0) { flag = false; break; } } if(flag) { return x; } } } int V(int x) { while(1) { bool flag = true; for(int i = 2; i * i <= x; i++) { if(x % i == 0) { flag = false; break; } } if(flag) { return x; } x--; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while(T--) { cin >> n; int u = U(n), v = V(n); int ans1 = u * v - u * 2 - v * 2 + n * 2 + 2, ans2 = u * v * 2, w = gcd(ans1, ans2); ans1 /= w; ans2 /= w; cout << ans1 << "/" << ans2 << "\n"; } return 0; } ``` ## T2 ### 记录 $100pts

思路

首先对于往四周跑,直接弄就行了,没技术含量。然后考虑打枪瞬移(鉴定为把自己打出去了),直接往四个方向延伸,然后碰到墙就退出,并把所有的即将碰到墙但还没碰到墙的位置存下来,至于为什么不直接存墙的位置,是因为不然你也不知道你会从哪个方向出来。然后对于这四个位置,两两枚举,然后让当前坐标于其中一个连边,边权为到达另外一个位置的距离加一(你还要瞬移)。边建完了,就直接跑最短路即可。

但是,因为通道会保留两次,难道这么弄不会导致答案错误?其实不会,分两种情况考虑:新打了一个通道,然后往新/旧里面钻。先考虑往旧的里面钻,因为打枪没有代价,所以可以直接打出一个新通道(即朝旧通道再开一枪),不需要保留之前的,因为这样效果不变。然后如果往新的里面钻,那么你会回到旧的通道外,还不如不进去。复杂度 O(nm(n+m)+nm\log nm),可以通过。

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

const int N = 2010;
int n, m, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}, sx, sy, tx, ty, vis[N * N], dis[N * N];
string s[N];
vector<pair<int, int> > E[N * N];

int id(int i, int j) {
  return (i - 1) * m + j;
}

signed main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = " " + s[i];
  }
  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      if(s[i][j] != '#') {
        for(int k = 0; k < 4; k++) {
          int ni = i + dx[k], nj = j + dy[k];
          if(ni < 1 || ni > n || nj < 1 || nj > m) {
            continue;
          }
          if(s[ni][nj] != '#') {
            E[id(i, j)].push_back({id(ni, nj), 1});
          }
        }
        if(s[i][j] == 'C') {
          sx = i;
          sy = j;
        }
        if(s[i][j] == 'F') {
          tx = i;
          ty = j;
        }
        vector<int> node;
        for(int k = 0; k < 4; k++) {
          int si = i + dx[k], sj = j + dx[k], ni = i, nj = j;
          if(si < 1 || si > n || sj < 1 || sj > m) {
            continue;
          }
          while(1) {
            if(s[ni + dx[k]][nj + dy[k]] == '#') {
              node.push_back(id(ni, nj));
              break;
            }
            ni += dx[k];
            nj += dy[k];
          }
        }
        for(int x = 0; x < node.size(); x++) {
          for(int y = x + 1; y < node.size(); y++) {
            E[id(i, j)].push_back({node[y], abs(i - (node[x] + m - 1) / m) + abs(j - (node[x] % m ? node[x] % m : m)) + 1});
            E[id(i, j)].push_back({node[x], abs(i - (node[y] + m - 1) / m) + abs(j - (node[y] % m ? node[y] % m : m)) + 1});
          }
        }
      }
    }
  }
  for(int i = 1; i <= n * m; i++) {
    dis[i] = 1e18;
  }
  priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
  q.push({0, id(sx, sy)});
  dis[id(sx, sy)] = 0;
  while(!q.empty()) {
    int x = q.top().second;
    q.pop();
    if(vis[x]) {
      continue;
    }
    vis[x] = 1;
    for(int i = 0; i < E[x].size(); i++) {
      int y = E[x][i].first, w = E[x][i].second;
      if(dis[y] > dis[x] + w) {
        dis[y] = dis[x] + w;
        q.push({dis[y], y});
      }
    }
  }
  if(dis[id(tx, ty)] == 1e18) {
    cout << "nemoguce\n";
  }
  else {
    cout << dis[id(tx, ty)]<< "\n";
  }

  return 0;
}

T3

记录

我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了

思路

看到这么小的时间(\le 2000)和更小的 m(\le 100),直接让我们冒出一个邪恶的念头:直接区间DP。状态为 dp_{l,r,t,0/1},表示当前走过的区间为 [l,r],用时 t,当前在区间最左/最右。为什么最后一维要这么设?首先,因为你走过一个点,落在这个点上的宝可梦肯定会被抓,所以区间中是没有宝可梦可抓的,只有往两边拓展才会有新的宝可梦被抓我想知道然后会怎么样,所以可以直接抛掉中间位置,只保留两端。然后转移方程就非常好推(但是太多不好写上去)。但是,这不是 O(2000\times n^2) 的吗?我们可以发现,在两个相邻的宝可梦中间的位置的状态是没有意义的,因为它们只是单纯的拓展区间,没有任何贡献。所以我们可以通过离散化直接杀掉中间的点,只保留宝可梦所在的点。注意空间注意空间注意空间复杂度 O(2000\times m^2)

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

const int N = 2010, M = 110;
int n, k, m, dp[M][M][N][2], a[N], b[N], c[N], w[M][N], lsh[N], lshc, ans;

// dp[l][r][t][0/1] 代表当前访问过的区间为[l,r],用时为t,当前位置在最左侧/最右侧时的最大分数 

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k >> m;
  for(int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    lsh[++lshc] = a[i];
  }
  lsh[++lshc] = k;
  sort(lsh + 1, lsh + lshc + 1);
  lshc = unique(lsh + 1, lsh + lshc + 1) - lsh - 1;
  for(int i = 1; i <= m; i++) {
    a[i] = lower_bound(lsh + 1, lsh + lshc + 1, a[i]) - lsh;
    for(int j = 1; j <= c[i]; j++) {
      w[a[i]][j] += b[i];
    }
  }
  k = lower_bound(lsh + 1, lsh + lshc + 1, k) - lsh;
  memset(dp, -1, sizeof dp);
  dp[k][k][1][0] = dp[k][k][1][1] = w[k][1];
  for(int len = 1; len <= lshc; len++) {
    for(int l = 1; l + len - 1 <= lshc; l++) {
      int r = l + len - 1;
      for(int t = 1; t <= 2000; t++) {
        if(t + (lsh[r] - lsh[l]) <= 2000) {
          dp[l][r][t + (lsh[r] - lsh[l])][0] = max(dp[l][r][t + (lsh[r] - lsh[l])][0], dp[l][r][t][1]);
          dp[l][r][t + (lsh[r] - lsh[l])][1] = max(dp[l][r][t + (lsh[r] - lsh[l])][1], dp[l][r][t][0]);
        }
        dp[l][r][t][0] = max(dp[l][r][t][0], dp[l][r][t - 1][0]);
        dp[l][r][t][1] = max(dp[l][r][t][1], dp[l][r][t - 1][1]);
      }
      for(int t = 1; t <= 2000; t++) {
        ans = max(ans, max(dp[l][r][t][0], dp[l][r][t][1]));
        if(l > 1) {
          if(t + (lsh[l] - lsh[l - 1]) <= 2000 && dp[l][r][t][0] != -1) {
            dp[l - 1][r][t + (lsh[l] - lsh[l - 1])][0] = max(dp[l - 1][r][t + (lsh[l] - lsh[l - 1])][0], dp[l][r][t][0] + w[l - 1][t + (lsh[l] - lsh[l - 1])]);
          }
          if(t + (lsh[r] - lsh[l - 1]) <= 2000 && dp[l][r][t][1] != -1) {
            dp[l - 1][r][t + (lsh[r] - lsh[l - 1])][0] = max(dp[l - 1][r][t + (lsh[r] - lsh[l - 1])][0], dp[l][r][t][1] + w[l - 1][t + (lsh[r] - lsh[l - 1])]);
          }
        }
        if(r < lshc) {
          if(t + (lsh[r + 1] - lsh[r]) <= 2000 && dp[l][r][t][1] != -1) {
            dp[l][r + 1][t + (lsh[r + 1] - lsh[r])][1] = max(dp[l][r + 1][t + (lsh[r + 1] - lsh[r])][1], dp[l][r][t][1] + w[r + 1][t + (lsh[r + 1] - lsh[r])]);
          }
          if(t + (lsh[r + 1] - lsh[l]) <= 2000 && dp[l][r][t][0] != -1) {
            dp[l][r + 1][t + (lsh[r + 1] - lsh[l])][1] = max(dp[l][r + 1][t + (lsh[r + 1] - lsh[l])][1], dp[l][r][t][0] + w[r + 1][t + (lsh[r + 1] - lsh[l])]);
          }
        }
      }
    }
  }
  cout << ans << "\n";

  return 0;
}

T4

记录

0pts

前三题(主要第二题)用时太长了,会写也没时间了。

思路

看懂题面是这题最难的

题目的意思就是让你选择一个点作为一棵树的根节点,然后使代价最小。代价为需要消耗的总体力值,然后走一条边消耗的体力值为边权减去深度小的那个点的点权,然后第一次走一条边会被弹回根节点。

那这不就是再明显不过的换根 DP 吗?首先一条边经过的次数为以这个边连接的两点中深度大的那个点为根的子树的大小(每一个点都有一条连向父节点的边,都需要走一遍),累积起来就是整棵树的代价。然后换根时,权值发生变动的就只有连接这两个点的边,因为只有这两个点父子关系发生了变换。

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

const int N = 1e6 + 10;
int n, a[N], sz[N], dp[N], ans1 = 1e18, ans2;
vector<pair<int, int>> E[N];

void dfs1(int x, int fa) {
  sz[x] = 1;
  for(int i = 0; i < E[x].size(); i++) {
    int y = E[x][i].first, w = E[x][i].second;
    if(y == fa) {
      continue;
    }
    dfs1(y, x);
    dp[x] += dp[y] + sz[y] * max(0ll, w - a[x]);
    sz[x] += sz[y];
  } 
}

void dfs2(int x, int fa) {
  for(int i = 0; i < E[x].size(); i++) {
    int y = E[x][i].first, w = E[x][i].second;
    if(y == fa) {
      continue;
    }
    dp[y] = dp[x] - sz[y] * max(0ll, w - a[x]) + (n - sz[y]) * max(0ll, w - a[y]);
    dfs2(y, x);
  }
}

signed main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for(int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    E[u].push_back({v, w});
    E[v].push_back({u, w});
  }
  dfs1(1, 0);
  dfs2(1, 0);
  for(int i = 1; i <= n; i++) {
    if(ans1 > dp[i]) {
      ans2 = i;
      ans1 = dp[i];
    }
  }
  cout << ans2 << "\n" << ans1 << "\n";

  return 0;
}

总结

  1. 做题不够细致,把数组开大了。

  2. 还是写代码太慢了,需要改进代码的结构(比如上面的T2T3两个屎山代码)