跳到正文

个人记录

总结

CuteGirlGC约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年9月22日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 8 分钟

版本

  1. v1当前

预计:100+100+100+20 实际:100+100+100+0

挂的比较少,不错:)

T1

签到题,秒了。

对于每一组的两个做 dp 做暴力转移即可。复杂度 O(n2)O(n^2)

C++
#include <iostream>

using namespace std;
using LL = long long;

const LL kMaxN = 1e5 + 1;

LL n, m, f[kMaxN][2];
struct P {
  LL x, y;
} p[kMaxN][2];

LL D(P a, P b) {
  return abs(a.x - b.x) + abs(a.y - b.y);
}

int main() {
  cin >> n >> m;
  for (LL i = 1; i <= n; i++) {
    cin >> p[i][0].x >> p[i][0].y >> p[i][1].x >> p[i][1].y;
  }
  for (LL i = 2; i <= n; i++) {
    f[i][0] = min(f[i - 1][0] + D(p[i - 1][0], p[i][0]) + D(p[i - 1][1], p[i][1]), f[i - 1][1] + D(p[i - 1][1], p[i][0]) + D(p[i - 1][0], p[i][1]));
    f[i][1] = min(f[i - 1][0] + D(p[i - 1][0], p[i][1]) + D(p[i - 1][1], p[i][0]), f[i - 1][1] + D(p[i - 1][1], p[i][1]) + D(p[i - 1][0], p[i][0]));
  }
  cout << min(f[n][0], f[n][1]) + D(p[n][0], p[n][1]);
  return 0;
}

T2

有点思维的 dijkstra,就是我们发现最高的山峰也不能超过 a1+ka_1+k,然后把每一个山峰的修改代价作为点权,然后再点权转边权即可。

注意代价要和 00 取 max⁡\max,

C++
#include <iostream>
#include <queue>
#include <unordered_map>

using namespace std;
using LL = long long;

const LL kMaxN = 1e5 + 1;

LL n, m, k, a[kMaxN], d[kMaxN];
bool v[kMaxN];
unordered_map<LL, LL> mp[kMaxN];
vector<pair<LL, LL>> g[kMaxN];

struct N {
  LL x, d;
  friend bool operator<(N a, N b) {
    return a.d > b.d;
  }
};
priority_queue<N> q;

int main() {
  cin >> n >> m >> k;
  for (LL i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (LL i = 1, u, v, w; i <= m; i++) {
    cin >> u >> v >> w;
    g[u].push_back({v, w + (a[v] > k + a[1] ? (a[v] - k - a[1]) * (a[v] - k - a[1]) : 0)});
    g[v].push_back({u, w + (a[u] > k + a[1] ? (a[u] - k - a[1]) * (a[u] - k - a[1]) : 0)});
  }
  for (fill(d + 1, d + n + 1, 1e9), d[1] = 0, q.push(N{1, 0}); !q.empty(); ) {
    LL x = q.top().x;
    q.pop();
    if (v[x]) {
      continue;
    }
    v[x] = 1;
    for (auto i : g[x]) {
      if (d[i.first] > d[x] + i.second) {
        d[i.first] = d[x] + i.second;
        q.push(N{i.first, d[i.first]});
      }
    }
  }
  cout << d[n] << '\n';
  return 0;
}

T3

打表找规律

打一个表子,其中第 ii 行第 jj 个代表长度为 ii,开头为 jj 答案数,然后就可以发现规律:

代码
1
1 0 = 1
1 1 0 = 2
2 2 1 0 = 5
5 5 4 2 0 = 16
16 16 14 10 5 0 = 61
61 61 56 46 32 16 = 272

61 = 61
61 = 61 - 0
56 = 61 - 5
46 = 56 - 10
...

于是就有了代码:

C++
#include <iostream>
#include <vector>

using namespace std;
using LL = long long;

const LL kMaxN = 1e4 + 1, kMod = 1e9 + 7;

LL n, f[2][kMaxN];

int main() {
  cin >> n;
  if (n <= 2) {
    cout << 1 << '\n';
    return 0;
  }
  f[0][1] = 1;
  LL s = 0;
  for (LL i = 3; i <= n; i++, s = 0) {
    for (LL j = 1; j <= i - 1; j++) {
      s = (s + f[(i - 1) & 1][j]) % kMod;
    }
    f[i & 1][1] = s;
    for (LL j = 2, k = i - 2; j <= i; j++, k--) {
      f[i & 1][j] = s;
      s = (s - f[(i - 1) & 1][k] + kMod) % kMod;
    }
  }
  for (LL i = 1; i <= n; i++) {
    s = (s + f[n & 1][i]) % kMod;
  }
  cout << s << '\n';
  return 0;
}

T4

合理猜想对于函数 f(x)f(x) 为每一个首领最多拥有 xx 时最少的金币花费数这个函数是一个单谷的,然后二分或者三分求峰值即可。

C++
#include <algorithm>
#include <iostream>
#include <set>
#include <numeric>
#include <vector>

using namespace std;
using LL = long long;

const LL kMaxN = 1e6 + 1;

LL n, m;
vector<LL> v[kMaxN], s[kMaxN];

inline LL C(LL x) {
  vector<LL> a;
  LL cnt = 0, ans = 0;
  for (LL i = 1; i <= n; i++) {
    LL l = v[i].size() - x;
    if (l >= 0) {
      ans += s[i][l];
      cnt += l + 1;
    }
    for (LL j = max(l, 0ll); j < v[i].size(); j++) {
      a.push_back(v[i][j]);
    }
  }
  if (cnt >= x) {
    return ans;
  }
  if (cnt + a.size() < x) {
    return 1e18;
  }
  sort(a.begin(), a.end());
  for (LL i = 0; i < x - cnt; i++) {
    ans += a[i];
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (LL i = 1, a, c; i <= m; i++) {
    cin >> a >> c, v[c].push_back(a);
  }
  for (LL i = 1; i <= n; i++) {
    sort(v[i].begin(), v[i].end());
    s[i].resize(v[i].size());
    partial_sum(v[i].begin(), v[i].end(), s[i].begin());
  }
  LL l = 0, r = m + 1;
  while (l + 1 < r) {
    LL m = l + r >> 1;
    if (C(m) > C(m + 1)) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << C(r);
  return 0;
}
原文