跳到正文

个人记录

9.5总结

__LZH__约 11 分钟
原文

这是 2026年9月5日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年9月5日
收录
2026年9月5日
更新
2026年11月9日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

前言

成绩还没出,等会补。

第一题小细节没判,第四题点分树双log太慢了。

题目

T1:vis

难度:橙题

分类讨论题,发现最多标记一个点,因此只能先到 a1a_1,回标记再到 ana_n,或着反过来,注意判断 k=0k = 0 和 s<a1,s>ans < a_1, s>a_n。

收获:不要漏判情况

C++
#include<bits/stdc++.h> 

#define int long long

using namespace std;

const int N = 1e5 + 10;

int n, s, k, a[N], ans;

signed main(){
  freopen("vis.in", "r", stdin);
  freopen("vis.out", "w", stdout);
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> s >> k;
  for(int i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + 1 + n);
  if(k == 0){
    cout << min(abs(s - a[1]), abs(a[n] - s)) + a[n] - a[1];
    return 0;
  }
  if(s < a[1]) {
    cout << a[n] - s;
    return 0;
  }
  if(s > a[n]) {
    cout << s - a[1];
    return 0;
  }
  int l = lower_bound(a + 1, a + 1 + n, s) - a;
  if(a[l] == s){
    cout << a[n] - a[1];
    return 0;
  }
  ans = a[n] - a[1] + a[l] - s;
  if(l > 1) ans = min(ans, a[n] - a[1] + s - a[l - 1]);
  cout << ans;
  return 0;
}

T2

难度:绿题

发现整体平移只有 6060 种情况,直接DP,设 dpi,jdp_{i, j} 为第 ii 个位置移了 jj 次的最小代价,前缀和优化即可。

收获:无

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 5e3 + 10, M = 1e5 + 10;

int n, m, k, p, q, t, c[N], s[M], a[N][5], dp[M][65];
pair<int, int> mp[N];

signed main(){
  freopen("keyboard.in", "r", stdin);
  freopen("keyboard.out", "w", stdout);
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> p >> q >> t;
  for(int i = 1; i <= n; i++){ cin >> c[i];
    for(int j = 0; j < c[i]; j++) cin >> a[i][j], mp[a[i][j]] = {i, j};
  }
  cin >> k; 
  fill(&dp[0][0], &dp[k][60] + 1, 1e18);dp[0][0] = 0;
  for(int i = 0; i < 60; i++) dp[0][i] = min(dp[0][i], dp[0][(i + 59) % 60] + t);
  for(int i = 1; i <= k; i++) cin >> s[i];
  for(int i = 1; i <= k; i++) {
    pair<int, int> zrr = mp[s[i]];
    int x = zrr.first, y = zrr.second;
    for(int j = 0; j < 60; j++){
      for(int l = 0; l < c[x]; l++){
        int op = (j - l + 60) % 60, w = abs(j % c[x] - y);
        dp[i][j] = min(dp[i][j], dp[i - 1][op] + (!w ? p : q) + w + l * t);
      }
    }
    for(int j = 0; j < 120; j++) dp[i][j % 60] = min(dp[i][j % 60], dp[i][(j + 59) % 60] + t);
  }
  cout << *min_element(dp[k], dp[k] + 60) ;
  return 0;
}
/*
2 7 2 20 1
5 3 1 5 7 2
2 4 6
8
3 6 7 4 2 1 5 3
*/

T3

难度:青题

发现当一个点的情况只对应一条路径,那么直接哈希+倍增找第一个不相等的位置即可。

收获:无

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e5 + 10, base[] = {13331, 23333}, MOD[] = {1145917813, 1000000007};

struct node{
  int x, w;
};

int n, m, in[N], dp[N], lw[N], id[N], vis[N], mx[N], s[N], f[21][N], w[2][21][N], b[2][N];
vector<node> v[N];

bool chk(int x, int y, int t){
  for(int i = 18; i >= 0; i--){
    if(!(t >> i & 1)) continue;
    if(w[0][i][x] != w[0][i][y] || w[1][i][x] != w[1][i][y]) return 0;
    x = f[i][x], y = f[i][y];
  }
  return 1;
}

bool check(int x, int y, int id, int ww){
  if(!y || dp[x] > dp[y]) return 1;
  if(dp[x] < dp[y]) return 0;
  if(w[0][0][id] < ww) return 0;
  if(w[0][0][id] > ww) return 1;
  int l = 1, r = dp[x], op = 0;
  while(l <= r){
    int mid = (l + r) >> 1;
    (chk(x, y, mid) ? l = mid + 1, op = mid : r = mid - 1);
  }
  for(int i = 18; i >= 0; i--) if(op >> i & 1) x = f[i][x], y = f[i][y];
  return w[0][0][x] < w[0][0][y];
}

signed main(){
  freopen("journey.in", "r", stdin);
  freopen("journey.out", "w", stdout);
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for(int i = 1, x, y, w; i <= m; i++){
    cin >> x >> y >> w, v[y].push_back({x, w}), in[x]++;
  } queue<int> q; b[0][0] = b[1][0] = 1;
  for(int i = 1; i <= n; i++){
    for(int j : {0, 1}) b[j][i] = b[j][i - 1] * base[j] % MOD[j];
  }
  for(int i = 1; i <= n; i++) if(!in[i]) q.push(i);
  while(!q.empty()){
    int x = q.front(); q.pop();
    for(int i = 1; i <= 18; i++){
      f[i][x] = f[i - 1][f[i - 1][x]];
      for(int j : {0, 1}) w[j][i][x] = (w[j][i - 1][x] * b[j][1 << i - 1] + w[j][i - 1][f[i - 1][x]]) % MOD[j];
    }
    for(node zrr : v[x]){
      int i = zrr.x, ww = zrr.w; in[i]--;
      if(check(x, id[i], i, ww)){
        dp[i] = dp[x] + 1, s[i] = s[x] + ww, id[i] = x, w[0][0][i] = w[1][0][i] = ww, f[0][i] = x; 
      }
      if(!in[i]) q.push(i);
    }
  }
  for(int i = 1; i <= n; i++) cout << dp[i] << ' ' << s[i] << '\n';
  return 0;
}

T4

难度:紫题

被题面骗了,写点分树双log又难写又过不了,气笑了。

先将题目转化为一步步跳,最多免费走 k−1k - 1 步,我们可以先预处理每个点到最近白点的距离,那么我们就可以求出一个点还能往后免费走几步,然后直接模拟贪心做完了。

收获:不要陷入思维定式

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 5e5 + 10;

int T, n, k, d[N], ans[N];
vector<int> v[N];
string t;

void dfs(int x, int fa, int s, int s1, int s2){
  ans[x] = s1 + s2;
  for(int i : v[x]){
    if(i == fa) continue;
    int ss = s - 1, ss1 = s1, ss2 = (t[i] == '1' ? 0 : 1);
    if(d[i] <= ss) ss = max(ss, k - d[i]);
    if(!ss) ss1 += ss2, ss2 = 0, ss = k;
    dfs(i, x, ss, ss1, ss2);
  }
}

void clear(){
  for(int i = 1; i <= n; i++) v[i].clear();
}

void solve(){
  cin >> n >> k >> t; t = ' ' + t, clear(); 
  for(int i = 1, x, y; i < n; i++) cin >> x >> y, v[x].push_back(y), v[y].push_back(x);
  queue<int> q; fill(d + 1, d + 1 + n, 1e18);
  for(int i = 1; i <= n; i++) if(t[i] == '1') d[i] = 0, q.push(i);
  while(!q.empty()){ int x = q.front(); q.pop();
    for(int i : v[x]) if(d[i] > d[x] + 1) d[i] = d[x] + 1, q.push(i);
  }
  dfs(1, 0, k, 0, 0);
  for(int i = 2; i <= n; i++) cout << ans[i] << ' ';
  cout << '\n';
}

signed main(){
  freopen("jump.in", "r", stdin);
  freopen("jump.out", "w", stdout);
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> T;
  while(T--) solve();
  return 0;
}