20250725yl总结

· · 个人记录

A

题面

水果店里有散装苹果和苹果礼盒,其中散装苹果 a 元一个,苹果礼盒 b 元一盒,每盒包含 m 个苹果,问买至少 n 个苹果最少需要多少钱。

思路

枚举买几个礼盒,计算即可。

错因

没看见“至少”

感言

出题人WCNM,我是大SB。

代码

#include<bits/stdc++.h>

#define int long long

using namespace std;

int n, m, a, b;

signed main(){
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> a >> b;
  int ans = 1e18;
  for(int i = 0; i <= n / m + 1; i++){
    ans = min(ans, i * b + max(0ll, n - i * m) * a);
  }
  cout << ans;
  return 0;
}

B

题面

平方年是指年份为完全平方数的年,如 2025, 现给定一个日期,问该日期离下一个平方年的 1 月 1 日还有多少天。

思路

照题意模拟即可。

错因

没错

感言

出题人你真大

代码

#include<bits/stdc++.h>

#define int long long

using namespace std;

int d[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int a, b, c;

int f(int op){
  return 365 * op + op / 4 - op / 100 + op / 400;
}

signed main(){
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> a >> b >> c;
  int op = 0, ans = 0;
  for(; op * op <= a; op++);
  op *= op;
  if(b != 1 || c != 1){
    if(a % 4 != 0 || a % 100 == 0 && a % 400 != 0) {
      ans += d[b] - c + 1;
      for(int i = b + 1; i <= 12; i++){
        ans += d[i];
      }
    }else{
      d[2]++;
      ans += d[b] - c + 1;
      for(int i = b + 1; i <= 12; i++){
        ans += d[i];
      }
    }
    a++; 
  }
  ans += f(op - 1) - f(a - 1);
  cout << ans;
  return 0;
}

C

题面

做一桌满汉全席需要 n 种食材,其中第 i 种食材需要 a_i 克。现在蜗蜗手上第 i 种食材有 b_i 克,并且他有 k 元钱可以购买食材,已知每种食材的价格都是一元一克,蜗蜗想知道他最多能做多少桌满汉全席。

思路

二分答案即可

错因

二分范围开小了

感言

不给大数据出题人WCNM,我是大SB。

C

题面

做一桌满汉全席需要 n 种食材,其中第 i 种食材需要 a_i 克。现在蜗蜗手上第 i 种食材有 b_i 克,并且他有 k 元钱可以购买食材,已知每种食材的价格都是一元一克,蜗蜗想知道他最多能做多少桌满汉全席。

思路

二分答案即可

错因

二分范围开小了

感言

出题人WCNM

代码

#include<bits/stdc++.h>

#define int unsigned long long

using namespace std;

const int N = 1e5 + 10;

int n, k, a[N], b[N];

bool check(int x){
  __int128_t op = 0;
  for(int i = 1; i <= n; i++){
    if((__int128_t)a[i] * x > b[i]){
      op += (__int128_t)a[i] * x - b[i];
    }
  }
  return op <= k;
}

signed main(){
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for(int i = 1; i <= n; i++){
    cin >> a[i] ;
  } 
  for(int i = 1; i <= n; i++){
    cin >> b[i];
  }
  int l = 0, r = 2e18, ans = 0;
  while(l <= r){
    int mid = (l + r) >> 1;
    if(check(mid)){
      l = mid + 1, ans = mid;
    }else{
      r = mid - 1;
    }
  }
  cout << ans;
  return 0;
}

D

题面

给定节点个数为 n 的树,你需要回答 m 次询问,每次询问给定两个树上的节点,你需要回答树上有多少节点离这两个点的距离一样。

思路

lca求答案即可

错因

continue写成return0,还有一些小细节

感言

出题人WCNM,我是大SB。

代码

#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e5 + 10, L = 21;

int n, m, x, y, sum, dep[N], f[L][N], sz[N];
vector<int> v[N];

void dfs(int x, int fa){
  dep[x] = dep[fa] + 1;
  f[0][x] = fa;
  for(int i : v[x]){
    if(i != fa){
      dfs(i, x);
      sz[x] += sz[i];
    }
  }
  sz[x]++;
}

int lca(int x, int y){
  if(dep[x] > dep[y]){
    swap(x, y);
  }
  for(int i = L - 1; i >= 0; i--){
    if(dep[f[i][y]] >= dep[x]){
      y = f[i][y];
    }
  }
  if(x == y){
    return x;
  }
  for(int i = L - 1; i >= 0; i--){
    if(f[i][x] != f[i][y]){
      x = f[i][x], y = f[i][y];
    }
  }
  return f[0][x];
}

int dist(int x, int y) {
  return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}

signed main(){
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for(int i = 1, x, y; i < n; i++){
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  cin >> m;
  dfs(1, 0);
  for(int i = 1; i < L; i++){
    for(int j = 1; j <= n; j++){
      f[i][j] = f[i - 1][f[i - 1][j]];
    }
  }
  while(m--){
    cin >> x >> y;
    int k = lca(x, y), lx = dep[x] - dep[k], ly = dep[y] - dep[k], d = lx + ly;
    if(d % 2){
      cout << "0\n";
      continue;
    }
    if(x == y){
      cout << n << '\n';
      continue;
    }
    if(lx > ly){
      swap(x, y);
      swap(lx, ly);
    }
    int op = y, op1 = y, h = d / 2, id = 0;
    for(int i = L - 1; i >= 0; i--){
      if(h >> i & 1){
        op = f[i][op];
      }
    }
    h = d / 2 - 1;
    for(int i = L - 1; i >= 0; i--){
      if(h >> i & 1){
        op1 = f[i][op1];
      }
    }
    if(k == x || k != op){
      cout << sz[op] - sz[op1] << '\n';
    }else{
      int op2 = x;
      h = lx - 1, id = 0;
      for(int i = L - 1; i >= 0; i--){
        if(h >> i & 1){
          op2 = f[i][op2];
        }
      }
      cout << n - sz[op1] - sz[op2] << '\n';
    }
  }
  return 0;
}